Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1 | /* |
Mike Frysinger | 26fdc1f | 2008-02-06 01:38:21 -0800 | [diff] [blame] | 2 | * Blackfin On-Chip SPI Driver |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 3 | * |
Mike Frysinger | 9c0a788 | 2010-10-18 02:45:22 -0400 | [diff] [blame] | 4 | * Copyright 2004-2010 Analog Devices Inc. |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 5 | * |
Mike Frysinger | 26fdc1f | 2008-02-06 01:38:21 -0800 | [diff] [blame] | 6 | * Enter bugs at http://blackfin.uclinux.org/ |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 7 | * |
Mike Frysinger | 26fdc1f | 2008-02-06 01:38:21 -0800 | [diff] [blame] | 8 | * Licensed under the GPL-2 or later. |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | #include <linux/init.h> |
| 12 | #include <linux/module.h> |
Bryan Wu | 131b17d | 2007-12-04 23:45:12 -0800 | [diff] [blame] | 13 | #include <linux/delay.h> |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 14 | #include <linux/device.h> |
Steven Miao | e8304d0 | 2014-04-12 09:23:24 +0800 | [diff] [blame] | 15 | #include <linux/gpio.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 16 | #include <linux/slab.h> |
Bryan Wu | 131b17d | 2007-12-04 23:45:12 -0800 | [diff] [blame] | 17 | #include <linux/io.h> |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 18 | #include <linux/ioport.h> |
Bryan Wu | 131b17d | 2007-12-04 23:45:12 -0800 | [diff] [blame] | 19 | #include <linux/irq.h> |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 20 | #include <linux/errno.h> |
| 21 | #include <linux/interrupt.h> |
| 22 | #include <linux/platform_device.h> |
| 23 | #include <linux/dma-mapping.h> |
| 24 | #include <linux/spi/spi.h> |
| 25 | #include <linux/workqueue.h> |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 26 | |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 27 | #include <asm/dma.h> |
Bryan Wu | 131b17d | 2007-12-04 23:45:12 -0800 | [diff] [blame] | 28 | #include <asm/portmux.h> |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 29 | #include <asm/bfin5xx_spi.h> |
Vitja Makarov | 8cf5858 | 2009-04-06 19:00:31 -0700 | [diff] [blame] | 30 | #include <asm/cacheflush.h> |
| 31 | |
Bryan Wu | a32c691 | 2007-12-04 23:45:15 -0800 | [diff] [blame] | 32 | #define DRV_NAME "bfin-spi" |
| 33 | #define DRV_AUTHOR "Bryan Wu, Luke Yang" |
Mike Frysinger | 138f97c | 2009-04-06 19:00:50 -0700 | [diff] [blame] | 34 | #define DRV_DESC "Blackfin on-chip SPI Controller Driver" |
Bryan Wu | a32c691 | 2007-12-04 23:45:15 -0800 | [diff] [blame] | 35 | #define DRV_VERSION "1.0" |
| 36 | |
| 37 | MODULE_AUTHOR(DRV_AUTHOR); |
| 38 | MODULE_DESCRIPTION(DRV_DESC); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 39 | MODULE_LICENSE("GPL"); |
| 40 | |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 41 | #define START_STATE ((void *)0) |
| 42 | #define RUNNING_STATE ((void *)1) |
| 43 | #define DONE_STATE ((void *)2) |
| 44 | #define ERROR_STATE ((void *)-1) |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 45 | |
Mike Frysinger | 9c0a788 | 2010-10-18 02:45:22 -0400 | [diff] [blame] | 46 | struct bfin_spi_master_data; |
Mike Frysinger | 9c4542c | 2009-09-24 01:04:04 +0000 | [diff] [blame] | 47 | |
Mike Frysinger | 9c0a788 | 2010-10-18 02:45:22 -0400 | [diff] [blame] | 48 | struct bfin_spi_transfer_ops { |
| 49 | void (*write) (struct bfin_spi_master_data *); |
| 50 | void (*read) (struct bfin_spi_master_data *); |
| 51 | void (*duplex) (struct bfin_spi_master_data *); |
Mike Frysinger | 9c4542c | 2009-09-24 01:04:04 +0000 | [diff] [blame] | 52 | }; |
| 53 | |
Mike Frysinger | 9c0a788 | 2010-10-18 02:45:22 -0400 | [diff] [blame] | 54 | struct bfin_spi_master_data { |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 55 | /* Driver model hookup */ |
| 56 | struct platform_device *pdev; |
| 57 | |
| 58 | /* SPI framework hookup */ |
| 59 | struct spi_master *master; |
| 60 | |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 61 | /* Regs base of SPI controller */ |
Mike Frysinger | 47885ce | 2011-06-17 04:16:56 -0400 | [diff] [blame] | 62 | struct bfin_spi_regs __iomem *regs; |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 63 | |
Bryan Wu | 003d922 | 2007-12-04 23:45:22 -0800 | [diff] [blame] | 64 | /* Pin request list */ |
| 65 | u16 *pin_req; |
| 66 | |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 67 | /* BFIN hookup */ |
| 68 | struct bfin5xx_spi_master *master_info; |
| 69 | |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 70 | struct work_struct pump_messages; |
| 71 | spinlock_t lock; |
| 72 | struct list_head queue; |
| 73 | int busy; |
Mike Frysinger | f4f50c3 | 2009-09-24 00:41:49 +0000 | [diff] [blame] | 74 | bool running; |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 75 | |
| 76 | /* Message Transfer pump */ |
| 77 | struct tasklet_struct pump_transfers; |
| 78 | |
| 79 | /* Current message transfer state info */ |
| 80 | struct spi_message *cur_msg; |
| 81 | struct spi_transfer *cur_transfer; |
Mike Frysinger | 9c0a788 | 2010-10-18 02:45:22 -0400 | [diff] [blame] | 82 | struct bfin_spi_slave_data *cur_chip; |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 83 | size_t len_in_bytes; |
| 84 | size_t len; |
| 85 | void *tx; |
| 86 | void *tx_end; |
| 87 | void *rx; |
| 88 | void *rx_end; |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 89 | |
| 90 | /* DMA stuffs */ |
| 91 | int dma_channel; |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 92 | int dma_mapped; |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 93 | int dma_requested; |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 94 | dma_addr_t rx_dma; |
| 95 | dma_addr_t tx_dma; |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 96 | |
Yi Li | f6a6d96 | 2009-06-03 09:46:22 +0000 | [diff] [blame] | 97 | int irq_requested; |
| 98 | int spi_irq; |
| 99 | |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 100 | size_t rx_map_len; |
| 101 | size_t tx_map_len; |
| 102 | u8 n_bytes; |
Barry Song | b052fd0 | 2009-11-18 09:43:21 +0000 | [diff] [blame] | 103 | u16 ctrl_reg; |
| 104 | u16 flag_reg; |
| 105 | |
Bryan Wu | fad91c8 | 2007-12-04 23:45:14 -0800 | [diff] [blame] | 106 | int cs_change; |
Mike Frysinger | 9c0a788 | 2010-10-18 02:45:22 -0400 | [diff] [blame] | 107 | const struct bfin_spi_transfer_ops *ops; |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 108 | }; |
| 109 | |
Mike Frysinger | 9c0a788 | 2010-10-18 02:45:22 -0400 | [diff] [blame] | 110 | struct bfin_spi_slave_data { |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 111 | u16 ctl_reg; |
| 112 | u16 baud; |
| 113 | u16 flag; |
| 114 | |
| 115 | u8 chip_select_num; |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 116 | u8 enable_dma; |
Bryan Wu | 62310e5 | 2007-12-04 23:45:20 -0800 | [diff] [blame] | 117 | u16 cs_chg_udelay; /* Some devices require > 255usec delay */ |
Michael Hennerich | 42c78b2 | 2009-04-06 19:00:51 -0700 | [diff] [blame] | 118 | u32 cs_gpio; |
Wolfgang Muees | 93b61bd | 2009-04-06 19:00:53 -0700 | [diff] [blame] | 119 | u16 idle_tx_val; |
Yi Li | f6a6d96 | 2009-06-03 09:46:22 +0000 | [diff] [blame] | 120 | u8 pio_interrupt; /* use spi data irq */ |
Mike Frysinger | 9c0a788 | 2010-10-18 02:45:22 -0400 | [diff] [blame] | 121 | const struct bfin_spi_transfer_ops *ops; |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 122 | }; |
| 123 | |
Mike Frysinger | 9c0a788 | 2010-10-18 02:45:22 -0400 | [diff] [blame] | 124 | static void bfin_spi_enable(struct bfin_spi_master_data *drv_data) |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 125 | { |
Mike Frysinger | 47885ce | 2011-06-17 04:16:56 -0400 | [diff] [blame] | 126 | bfin_write_or(&drv_data->regs->ctl, BIT_CTL_ENABLE); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 127 | } |
| 128 | |
Mike Frysinger | 9c0a788 | 2010-10-18 02:45:22 -0400 | [diff] [blame] | 129 | static void bfin_spi_disable(struct bfin_spi_master_data *drv_data) |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 130 | { |
Mike Frysinger | 47885ce | 2011-06-17 04:16:56 -0400 | [diff] [blame] | 131 | bfin_write_and(&drv_data->regs->ctl, ~BIT_CTL_ENABLE); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | /* Caculate the SPI_BAUD register value based on input HZ */ |
| 135 | static u16 hz_to_spi_baud(u32 speed_hz) |
| 136 | { |
| 137 | u_long sclk = get_sclk(); |
| 138 | u16 spi_baud = (sclk / (2 * speed_hz)); |
| 139 | |
| 140 | if ((sclk % (2 * speed_hz)) > 0) |
| 141 | spi_baud++; |
| 142 | |
Michael Hennerich | 7513e00 | 2009-04-06 19:00:32 -0700 | [diff] [blame] | 143 | if (spi_baud < MIN_SPI_BAUD_VAL) |
| 144 | spi_baud = MIN_SPI_BAUD_VAL; |
| 145 | |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 146 | return spi_baud; |
| 147 | } |
| 148 | |
Mike Frysinger | 9c0a788 | 2010-10-18 02:45:22 -0400 | [diff] [blame] | 149 | static int bfin_spi_flush(struct bfin_spi_master_data *drv_data) |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 150 | { |
| 151 | unsigned long limit = loops_per_jiffy << 1; |
| 152 | |
| 153 | /* wait for stop and clear stat */ |
Mike Frysinger | 47885ce | 2011-06-17 04:16:56 -0400 | [diff] [blame] | 154 | while (!(bfin_read(&drv_data->regs->stat) & BIT_STAT_SPIF) && --limit) |
Bryan Wu | d8c0500 | 2007-12-04 23:45:21 -0800 | [diff] [blame] | 155 | cpu_relax(); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 156 | |
Mike Frysinger | 47885ce | 2011-06-17 04:16:56 -0400 | [diff] [blame] | 157 | bfin_write(&drv_data->regs->stat, BIT_STAT_CLR); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 158 | |
| 159 | return limit; |
| 160 | } |
| 161 | |
Bryan Wu | fad91c8 | 2007-12-04 23:45:14 -0800 | [diff] [blame] | 162 | /* Chip select operation functions for cs_change flag */ |
Mike Frysinger | 9c0a788 | 2010-10-18 02:45:22 -0400 | [diff] [blame] | 163 | static void bfin_spi_cs_active(struct bfin_spi_master_data *drv_data, struct bfin_spi_slave_data *chip) |
Bryan Wu | fad91c8 | 2007-12-04 23:45:14 -0800 | [diff] [blame] | 164 | { |
Mike Frysinger | 47885ce | 2011-06-17 04:16:56 -0400 | [diff] [blame] | 165 | if (likely(chip->chip_select_num < MAX_CTRL_CS)) |
| 166 | bfin_write_and(&drv_data->regs->flg, ~chip->flag); |
| 167 | else |
Michael Hennerich | 42c78b2 | 2009-04-06 19:00:51 -0700 | [diff] [blame] | 168 | gpio_set_value(chip->cs_gpio, 0); |
Bryan Wu | fad91c8 | 2007-12-04 23:45:14 -0800 | [diff] [blame] | 169 | } |
| 170 | |
Mike Frysinger | 9c0a788 | 2010-10-18 02:45:22 -0400 | [diff] [blame] | 171 | static void bfin_spi_cs_deactive(struct bfin_spi_master_data *drv_data, |
| 172 | struct bfin_spi_slave_data *chip) |
Bryan Wu | fad91c8 | 2007-12-04 23:45:14 -0800 | [diff] [blame] | 173 | { |
Mike Frysinger | 47885ce | 2011-06-17 04:16:56 -0400 | [diff] [blame] | 174 | if (likely(chip->chip_select_num < MAX_CTRL_CS)) |
| 175 | bfin_write_or(&drv_data->regs->flg, chip->flag); |
| 176 | else |
Michael Hennerich | 42c78b2 | 2009-04-06 19:00:51 -0700 | [diff] [blame] | 177 | gpio_set_value(chip->cs_gpio, 1); |
Bryan Wu | 62310e5 | 2007-12-04 23:45:20 -0800 | [diff] [blame] | 178 | |
| 179 | /* Move delay here for consistency */ |
| 180 | if (chip->cs_chg_udelay) |
| 181 | udelay(chip->cs_chg_udelay); |
Bryan Wu | fad91c8 | 2007-12-04 23:45:14 -0800 | [diff] [blame] | 182 | } |
| 183 | |
Barry Song | 8221610 | 2009-06-17 10:10:53 +0000 | [diff] [blame] | 184 | /* enable or disable the pin muxed by GPIO and SPI CS to work as SPI CS */ |
Mike Frysinger | 9c0a788 | 2010-10-18 02:45:22 -0400 | [diff] [blame] | 185 | static inline void bfin_spi_cs_enable(struct bfin_spi_master_data *drv_data, |
| 186 | struct bfin_spi_slave_data *chip) |
Barry Song | 8221610 | 2009-06-17 10:10:53 +0000 | [diff] [blame] | 187 | { |
Mike Frysinger | 47885ce | 2011-06-17 04:16:56 -0400 | [diff] [blame] | 188 | if (chip->chip_select_num < MAX_CTRL_CS) |
| 189 | bfin_write_or(&drv_data->regs->flg, chip->flag >> 8); |
Barry Song | 8221610 | 2009-06-17 10:10:53 +0000 | [diff] [blame] | 190 | } |
| 191 | |
Mike Frysinger | 9c0a788 | 2010-10-18 02:45:22 -0400 | [diff] [blame] | 192 | static inline void bfin_spi_cs_disable(struct bfin_spi_master_data *drv_data, |
| 193 | struct bfin_spi_slave_data *chip) |
Barry Song | 8221610 | 2009-06-17 10:10:53 +0000 | [diff] [blame] | 194 | { |
Mike Frysinger | 47885ce | 2011-06-17 04:16:56 -0400 | [diff] [blame] | 195 | if (chip->chip_select_num < MAX_CTRL_CS) |
| 196 | bfin_write_and(&drv_data->regs->flg, ~(chip->flag >> 8)); |
Barry Song | 8221610 | 2009-06-17 10:10:53 +0000 | [diff] [blame] | 197 | } |
| 198 | |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 199 | /* stop controller and re-config current chip*/ |
Mike Frysinger | 9c0a788 | 2010-10-18 02:45:22 -0400 | [diff] [blame] | 200 | static void bfin_spi_restore_state(struct bfin_spi_master_data *drv_data) |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 201 | { |
Mike Frysinger | 9c0a788 | 2010-10-18 02:45:22 -0400 | [diff] [blame] | 202 | struct bfin_spi_slave_data *chip = drv_data->cur_chip; |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 203 | |
| 204 | /* Clear status and disable clock */ |
Mike Frysinger | 47885ce | 2011-06-17 04:16:56 -0400 | [diff] [blame] | 205 | bfin_write(&drv_data->regs->stat, BIT_STAT_CLR); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 206 | bfin_spi_disable(drv_data); |
Bryan Wu | 88b4036 | 2007-05-21 18:32:16 +0800 | [diff] [blame] | 207 | dev_dbg(&drv_data->pdev->dev, "restoring spi ctl state\n"); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 208 | |
Barry Song | 9677b0de | 2009-11-30 03:49:41 +0000 | [diff] [blame] | 209 | SSYNC(); |
| 210 | |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 211 | /* Load the registers */ |
Mike Frysinger | 47885ce | 2011-06-17 04:16:56 -0400 | [diff] [blame] | 212 | bfin_write(&drv_data->regs->ctl, chip->ctl_reg); |
| 213 | bfin_write(&drv_data->regs->baud, chip->baud); |
Sonic Zhang | cc487e7 | 2007-12-04 23:45:17 -0800 | [diff] [blame] | 214 | |
| 215 | bfin_spi_enable(drv_data); |
Mike Frysinger | 138f97c | 2009-04-06 19:00:50 -0700 | [diff] [blame] | 216 | bfin_spi_cs_active(drv_data, chip); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 217 | } |
| 218 | |
Wolfgang Muees | 93b61bd | 2009-04-06 19:00:53 -0700 | [diff] [blame] | 219 | /* used to kick off transfer in rx mode and read unwanted RX data */ |
Mike Frysinger | 9c0a788 | 2010-10-18 02:45:22 -0400 | [diff] [blame] | 220 | static inline void bfin_spi_dummy_read(struct bfin_spi_master_data *drv_data) |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 221 | { |
Mike Frysinger | 47885ce | 2011-06-17 04:16:56 -0400 | [diff] [blame] | 222 | (void) bfin_read(&drv_data->regs->rdbr); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 223 | } |
| 224 | |
Mike Frysinger | 9c0a788 | 2010-10-18 02:45:22 -0400 | [diff] [blame] | 225 | static void bfin_spi_u8_writer(struct bfin_spi_master_data *drv_data) |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 226 | { |
Wolfgang Muees | 93b61bd | 2009-04-06 19:00:53 -0700 | [diff] [blame] | 227 | /* clear RXS (we check for RXS inside the loop) */ |
| 228 | bfin_spi_dummy_read(drv_data); |
Sonic Zhang | cc487e7 | 2007-12-04 23:45:17 -0800 | [diff] [blame] | 229 | |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 230 | while (drv_data->tx < drv_data->tx_end) { |
Mike Frysinger | 47885ce | 2011-06-17 04:16:56 -0400 | [diff] [blame] | 231 | bfin_write(&drv_data->regs->tdbr, (*(u8 *) (drv_data->tx++))); |
Wolfgang Muees | 93b61bd | 2009-04-06 19:00:53 -0700 | [diff] [blame] | 232 | /* wait until transfer finished. |
| 233 | checking SPIF or TXS may not guarantee transfer completion */ |
Mike Frysinger | 47885ce | 2011-06-17 04:16:56 -0400 | [diff] [blame] | 234 | while (!(bfin_read(&drv_data->regs->stat) & BIT_STAT_RXS)) |
Bryan Wu | d8c0500 | 2007-12-04 23:45:21 -0800 | [diff] [blame] | 235 | cpu_relax(); |
Wolfgang Muees | 93b61bd | 2009-04-06 19:00:53 -0700 | [diff] [blame] | 236 | /* discard RX data and clear RXS */ |
| 237 | bfin_spi_dummy_read(drv_data); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 238 | } |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 239 | } |
| 240 | |
Mike Frysinger | 9c0a788 | 2010-10-18 02:45:22 -0400 | [diff] [blame] | 241 | static void bfin_spi_u8_reader(struct bfin_spi_master_data *drv_data) |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 242 | { |
Wolfgang Muees | 93b61bd | 2009-04-06 19:00:53 -0700 | [diff] [blame] | 243 | u16 tx_val = drv_data->cur_chip->idle_tx_val; |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 244 | |
Wolfgang Muees | 93b61bd | 2009-04-06 19:00:53 -0700 | [diff] [blame] | 245 | /* discard old RX data and clear RXS */ |
Mike Frysinger | 138f97c | 2009-04-06 19:00:50 -0700 | [diff] [blame] | 246 | bfin_spi_dummy_read(drv_data); |
Sonic Zhang | cc487e7 | 2007-12-04 23:45:17 -0800 | [diff] [blame] | 247 | |
Wolfgang Muees | 93b61bd | 2009-04-06 19:00:53 -0700 | [diff] [blame] | 248 | while (drv_data->rx < drv_data->rx_end) { |
Mike Frysinger | 47885ce | 2011-06-17 04:16:56 -0400 | [diff] [blame] | 249 | bfin_write(&drv_data->regs->tdbr, tx_val); |
| 250 | while (!(bfin_read(&drv_data->regs->stat) & BIT_STAT_RXS)) |
Bryan Wu | d8c0500 | 2007-12-04 23:45:21 -0800 | [diff] [blame] | 251 | cpu_relax(); |
Mike Frysinger | 47885ce | 2011-06-17 04:16:56 -0400 | [diff] [blame] | 252 | *(u8 *) (drv_data->rx++) = bfin_read(&drv_data->regs->rdbr); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 253 | } |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 254 | } |
| 255 | |
Mike Frysinger | 9c0a788 | 2010-10-18 02:45:22 -0400 | [diff] [blame] | 256 | static void bfin_spi_u8_duplex(struct bfin_spi_master_data *drv_data) |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 257 | { |
Wolfgang Muees | 93b61bd | 2009-04-06 19:00:53 -0700 | [diff] [blame] | 258 | /* discard old RX data and clear RXS */ |
| 259 | bfin_spi_dummy_read(drv_data); |
| 260 | |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 261 | while (drv_data->rx < drv_data->rx_end) { |
Mike Frysinger | 47885ce | 2011-06-17 04:16:56 -0400 | [diff] [blame] | 262 | bfin_write(&drv_data->regs->tdbr, (*(u8 *) (drv_data->tx++))); |
| 263 | while (!(bfin_read(&drv_data->regs->stat) & BIT_STAT_RXS)) |
Bryan Wu | d8c0500 | 2007-12-04 23:45:21 -0800 | [diff] [blame] | 264 | cpu_relax(); |
Mike Frysinger | 47885ce | 2011-06-17 04:16:56 -0400 | [diff] [blame] | 265 | *(u8 *) (drv_data->rx++) = bfin_read(&drv_data->regs->rdbr); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 266 | } |
| 267 | } |
| 268 | |
Mike Frysinger | 9c0a788 | 2010-10-18 02:45:22 -0400 | [diff] [blame] | 269 | static const struct bfin_spi_transfer_ops bfin_bfin_spi_transfer_ops_u8 = { |
Mike Frysinger | 9c4542c | 2009-09-24 01:04:04 +0000 | [diff] [blame] | 270 | .write = bfin_spi_u8_writer, |
| 271 | .read = bfin_spi_u8_reader, |
| 272 | .duplex = bfin_spi_u8_duplex, |
| 273 | }; |
| 274 | |
Mike Frysinger | 9c0a788 | 2010-10-18 02:45:22 -0400 | [diff] [blame] | 275 | static void bfin_spi_u16_writer(struct bfin_spi_master_data *drv_data) |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 276 | { |
Wolfgang Muees | 93b61bd | 2009-04-06 19:00:53 -0700 | [diff] [blame] | 277 | /* clear RXS (we check for RXS inside the loop) */ |
| 278 | bfin_spi_dummy_read(drv_data); |
Bryan Wu | 88b4036 | 2007-05-21 18:32:16 +0800 | [diff] [blame] | 279 | |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 280 | while (drv_data->tx < drv_data->tx_end) { |
Mike Frysinger | 47885ce | 2011-06-17 04:16:56 -0400 | [diff] [blame] | 281 | bfin_write(&drv_data->regs->tdbr, (*(u16 *) (drv_data->tx))); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 282 | drv_data->tx += 2; |
Wolfgang Muees | 93b61bd | 2009-04-06 19:00:53 -0700 | [diff] [blame] | 283 | /* wait until transfer finished. |
| 284 | checking SPIF or TXS may not guarantee transfer completion */ |
Mike Frysinger | 47885ce | 2011-06-17 04:16:56 -0400 | [diff] [blame] | 285 | while (!(bfin_read(&drv_data->regs->stat) & BIT_STAT_RXS)) |
Wolfgang Muees | 93b61bd | 2009-04-06 19:00:53 -0700 | [diff] [blame] | 286 | cpu_relax(); |
| 287 | /* discard RX data and clear RXS */ |
| 288 | bfin_spi_dummy_read(drv_data); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 289 | } |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 290 | } |
| 291 | |
Mike Frysinger | 9c0a788 | 2010-10-18 02:45:22 -0400 | [diff] [blame] | 292 | static void bfin_spi_u16_reader(struct bfin_spi_master_data *drv_data) |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 293 | { |
Wolfgang Muees | 93b61bd | 2009-04-06 19:00:53 -0700 | [diff] [blame] | 294 | u16 tx_val = drv_data->cur_chip->idle_tx_val; |
Sonic Zhang | cc487e7 | 2007-12-04 23:45:17 -0800 | [diff] [blame] | 295 | |
Wolfgang Muees | 93b61bd | 2009-04-06 19:00:53 -0700 | [diff] [blame] | 296 | /* discard old RX data and clear RXS */ |
Mike Frysinger | 138f97c | 2009-04-06 19:00:50 -0700 | [diff] [blame] | 297 | bfin_spi_dummy_read(drv_data); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 298 | |
Wolfgang Muees | 93b61bd | 2009-04-06 19:00:53 -0700 | [diff] [blame] | 299 | while (drv_data->rx < drv_data->rx_end) { |
Mike Frysinger | 47885ce | 2011-06-17 04:16:56 -0400 | [diff] [blame] | 300 | bfin_write(&drv_data->regs->tdbr, tx_val); |
| 301 | while (!(bfin_read(&drv_data->regs->stat) & BIT_STAT_RXS)) |
Bryan Wu | d8c0500 | 2007-12-04 23:45:21 -0800 | [diff] [blame] | 302 | cpu_relax(); |
Mike Frysinger | 47885ce | 2011-06-17 04:16:56 -0400 | [diff] [blame] | 303 | *(u16 *) (drv_data->rx) = bfin_read(&drv_data->regs->rdbr); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 304 | drv_data->rx += 2; |
| 305 | } |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 306 | } |
| 307 | |
Mike Frysinger | 9c0a788 | 2010-10-18 02:45:22 -0400 | [diff] [blame] | 308 | static void bfin_spi_u16_duplex(struct bfin_spi_master_data *drv_data) |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 309 | { |
Wolfgang Muees | 93b61bd | 2009-04-06 19:00:53 -0700 | [diff] [blame] | 310 | /* discard old RX data and clear RXS */ |
| 311 | bfin_spi_dummy_read(drv_data); |
| 312 | |
| 313 | while (drv_data->rx < drv_data->rx_end) { |
Mike Frysinger | 47885ce | 2011-06-17 04:16:56 -0400 | [diff] [blame] | 314 | bfin_write(&drv_data->regs->tdbr, (*(u16 *) (drv_data->tx))); |
Wolfgang Muees | 93b61bd | 2009-04-06 19:00:53 -0700 | [diff] [blame] | 315 | drv_data->tx += 2; |
Mike Frysinger | 47885ce | 2011-06-17 04:16:56 -0400 | [diff] [blame] | 316 | while (!(bfin_read(&drv_data->regs->stat) & BIT_STAT_RXS)) |
Bryan Wu | d8c0500 | 2007-12-04 23:45:21 -0800 | [diff] [blame] | 317 | cpu_relax(); |
Mike Frysinger | 47885ce | 2011-06-17 04:16:56 -0400 | [diff] [blame] | 318 | *(u16 *) (drv_data->rx) = bfin_read(&drv_data->regs->rdbr); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 319 | drv_data->rx += 2; |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 320 | } |
| 321 | } |
| 322 | |
Mike Frysinger | 9c0a788 | 2010-10-18 02:45:22 -0400 | [diff] [blame] | 323 | static const struct bfin_spi_transfer_ops bfin_bfin_spi_transfer_ops_u16 = { |
Mike Frysinger | 9c4542c | 2009-09-24 01:04:04 +0000 | [diff] [blame] | 324 | .write = bfin_spi_u16_writer, |
| 325 | .read = bfin_spi_u16_reader, |
| 326 | .duplex = bfin_spi_u16_duplex, |
| 327 | }; |
| 328 | |
Rob Maris | e359540 | 2010-04-06 04:12:00 +0000 | [diff] [blame] | 329 | /* test if there is more transfer to be done */ |
Mike Frysinger | 9c0a788 | 2010-10-18 02:45:22 -0400 | [diff] [blame] | 330 | static void *bfin_spi_next_transfer(struct bfin_spi_master_data *drv_data) |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 331 | { |
| 332 | struct spi_message *msg = drv_data->cur_msg; |
| 333 | struct spi_transfer *trans = drv_data->cur_transfer; |
| 334 | |
| 335 | /* Move to next transfer */ |
| 336 | if (trans->transfer_list.next != &msg->transfers) { |
| 337 | drv_data->cur_transfer = |
| 338 | list_entry(trans->transfer_list.next, |
| 339 | struct spi_transfer, transfer_list); |
| 340 | return RUNNING_STATE; |
| 341 | } else |
| 342 | return DONE_STATE; |
| 343 | } |
| 344 | |
| 345 | /* |
| 346 | * caller already set message->status; |
| 347 | * dma and pio irqs are blocked give finished message back |
| 348 | */ |
Mike Frysinger | 9c0a788 | 2010-10-18 02:45:22 -0400 | [diff] [blame] | 349 | static void bfin_spi_giveback(struct bfin_spi_master_data *drv_data) |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 350 | { |
Mike Frysinger | 9c0a788 | 2010-10-18 02:45:22 -0400 | [diff] [blame] | 351 | struct bfin_spi_slave_data *chip = drv_data->cur_chip; |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 352 | unsigned long flags; |
| 353 | struct spi_message *msg; |
| 354 | |
| 355 | spin_lock_irqsave(&drv_data->lock, flags); |
| 356 | msg = drv_data->cur_msg; |
| 357 | drv_data->cur_msg = NULL; |
| 358 | drv_data->cur_transfer = NULL; |
| 359 | drv_data->cur_chip = NULL; |
Bhaktipriya Shridhar | 9b96f07 | 2016-07-02 14:12:00 +0530 | [diff] [blame] | 360 | schedule_work(&drv_data->pump_messages); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 361 | spin_unlock_irqrestore(&drv_data->lock, flags); |
| 362 | |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 363 | msg->state = NULL; |
| 364 | |
Bryan Wu | fad91c8 | 2007-12-04 23:45:14 -0800 | [diff] [blame] | 365 | if (!drv_data->cs_change) |
Mike Frysinger | 138f97c | 2009-04-06 19:00:50 -0700 | [diff] [blame] | 366 | bfin_spi_cs_deactive(drv_data, chip); |
Bryan Wu | fad91c8 | 2007-12-04 23:45:14 -0800 | [diff] [blame] | 367 | |
Yi Li | b9b2a76 | 2009-04-06 19:00:49 -0700 | [diff] [blame] | 368 | /* Not stop spi in autobuffer mode */ |
| 369 | if (drv_data->tx_dma != 0xFFFF) |
| 370 | bfin_spi_disable(drv_data); |
| 371 | |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 372 | if (msg->complete) |
| 373 | msg->complete(msg->context); |
| 374 | } |
| 375 | |
Yi Li | f6a6d96 | 2009-06-03 09:46:22 +0000 | [diff] [blame] | 376 | /* spi data irq handler */ |
| 377 | static irqreturn_t bfin_spi_pio_irq_handler(int irq, void *dev_id) |
| 378 | { |
Mike Frysinger | 9c0a788 | 2010-10-18 02:45:22 -0400 | [diff] [blame] | 379 | struct bfin_spi_master_data *drv_data = dev_id; |
| 380 | struct bfin_spi_slave_data *chip = drv_data->cur_chip; |
Yi Li | f6a6d96 | 2009-06-03 09:46:22 +0000 | [diff] [blame] | 381 | struct spi_message *msg = drv_data->cur_msg; |
| 382 | int n_bytes = drv_data->n_bytes; |
Bob Liu | 4d676fc | 2011-01-11 11:19:07 -0500 | [diff] [blame] | 383 | int loop = 0; |
Yi Li | f6a6d96 | 2009-06-03 09:46:22 +0000 | [diff] [blame] | 384 | |
| 385 | /* wait until transfer finished. */ |
Mike Frysinger | 47885ce | 2011-06-17 04:16:56 -0400 | [diff] [blame] | 386 | while (!(bfin_read(&drv_data->regs->stat) & BIT_STAT_RXS)) |
Yi Li | f6a6d96 | 2009-06-03 09:46:22 +0000 | [diff] [blame] | 387 | cpu_relax(); |
| 388 | |
| 389 | if ((drv_data->tx && drv_data->tx >= drv_data->tx_end) || |
| 390 | (drv_data->rx && drv_data->rx >= (drv_data->rx_end - n_bytes))) { |
| 391 | /* last read */ |
| 392 | if (drv_data->rx) { |
| 393 | dev_dbg(&drv_data->pdev->dev, "last read\n"); |
Scott Jiang | 128465c | 2012-04-23 18:18:12 -0400 | [diff] [blame] | 394 | if (!(n_bytes % 2)) { |
Bob Liu | 4d676fc | 2011-01-11 11:19:07 -0500 | [diff] [blame] | 395 | u16 *buf = (u16 *)drv_data->rx; |
| 396 | for (loop = 0; loop < n_bytes / 2; loop++) |
Mike Frysinger | 47885ce | 2011-06-17 04:16:56 -0400 | [diff] [blame] | 397 | *buf++ = bfin_read(&drv_data->regs->rdbr); |
Bob Liu | 4d676fc | 2011-01-11 11:19:07 -0500 | [diff] [blame] | 398 | } else { |
| 399 | u8 *buf = (u8 *)drv_data->rx; |
| 400 | for (loop = 0; loop < n_bytes; loop++) |
Mike Frysinger | 47885ce | 2011-06-17 04:16:56 -0400 | [diff] [blame] | 401 | *buf++ = bfin_read(&drv_data->regs->rdbr); |
Bob Liu | 4d676fc | 2011-01-11 11:19:07 -0500 | [diff] [blame] | 402 | } |
Yi Li | f6a6d96 | 2009-06-03 09:46:22 +0000 | [diff] [blame] | 403 | drv_data->rx += n_bytes; |
| 404 | } |
| 405 | |
| 406 | msg->actual_length += drv_data->len_in_bytes; |
| 407 | if (drv_data->cs_change) |
| 408 | bfin_spi_cs_deactive(drv_data, chip); |
| 409 | /* Move to next transfer */ |
| 410 | msg->state = bfin_spi_next_transfer(drv_data); |
| 411 | |
Yi Li | 7370ed6 | 2009-12-07 08:07:01 +0000 | [diff] [blame] | 412 | disable_irq_nosync(drv_data->spi_irq); |
Yi Li | f6a6d96 | 2009-06-03 09:46:22 +0000 | [diff] [blame] | 413 | |
| 414 | /* Schedule transfer tasklet */ |
| 415 | tasklet_schedule(&drv_data->pump_transfers); |
| 416 | return IRQ_HANDLED; |
| 417 | } |
| 418 | |
| 419 | if (drv_data->rx && drv_data->tx) { |
| 420 | /* duplex */ |
| 421 | dev_dbg(&drv_data->pdev->dev, "duplex: write_TDBR\n"); |
Scott Jiang | 128465c | 2012-04-23 18:18:12 -0400 | [diff] [blame] | 422 | if (!(n_bytes % 2)) { |
Bob Liu | 4d676fc | 2011-01-11 11:19:07 -0500 | [diff] [blame] | 423 | u16 *buf = (u16 *)drv_data->rx; |
| 424 | u16 *buf2 = (u16 *)drv_data->tx; |
| 425 | for (loop = 0; loop < n_bytes / 2; loop++) { |
Mike Frysinger | 47885ce | 2011-06-17 04:16:56 -0400 | [diff] [blame] | 426 | *buf++ = bfin_read(&drv_data->regs->rdbr); |
| 427 | bfin_write(&drv_data->regs->tdbr, *buf2++); |
Bob Liu | 4d676fc | 2011-01-11 11:19:07 -0500 | [diff] [blame] | 428 | } |
| 429 | } else { |
| 430 | u8 *buf = (u8 *)drv_data->rx; |
| 431 | u8 *buf2 = (u8 *)drv_data->tx; |
| 432 | for (loop = 0; loop < n_bytes; loop++) { |
Mike Frysinger | 47885ce | 2011-06-17 04:16:56 -0400 | [diff] [blame] | 433 | *buf++ = bfin_read(&drv_data->regs->rdbr); |
| 434 | bfin_write(&drv_data->regs->tdbr, *buf2++); |
Bob Liu | 4d676fc | 2011-01-11 11:19:07 -0500 | [diff] [blame] | 435 | } |
Yi Li | f6a6d96 | 2009-06-03 09:46:22 +0000 | [diff] [blame] | 436 | } |
| 437 | } else if (drv_data->rx) { |
| 438 | /* read */ |
| 439 | dev_dbg(&drv_data->pdev->dev, "read: write_TDBR\n"); |
Scott Jiang | 128465c | 2012-04-23 18:18:12 -0400 | [diff] [blame] | 440 | if (!(n_bytes % 2)) { |
Bob Liu | 4d676fc | 2011-01-11 11:19:07 -0500 | [diff] [blame] | 441 | u16 *buf = (u16 *)drv_data->rx; |
| 442 | for (loop = 0; loop < n_bytes / 2; loop++) { |
Mike Frysinger | 47885ce | 2011-06-17 04:16:56 -0400 | [diff] [blame] | 443 | *buf++ = bfin_read(&drv_data->regs->rdbr); |
| 444 | bfin_write(&drv_data->regs->tdbr, chip->idle_tx_val); |
Bob Liu | 4d676fc | 2011-01-11 11:19:07 -0500 | [diff] [blame] | 445 | } |
| 446 | } else { |
| 447 | u8 *buf = (u8 *)drv_data->rx; |
| 448 | for (loop = 0; loop < n_bytes; loop++) { |
Mike Frysinger | 47885ce | 2011-06-17 04:16:56 -0400 | [diff] [blame] | 449 | *buf++ = bfin_read(&drv_data->regs->rdbr); |
| 450 | bfin_write(&drv_data->regs->tdbr, chip->idle_tx_val); |
Bob Liu | 4d676fc | 2011-01-11 11:19:07 -0500 | [diff] [blame] | 451 | } |
| 452 | } |
Yi Li | f6a6d96 | 2009-06-03 09:46:22 +0000 | [diff] [blame] | 453 | } else if (drv_data->tx) { |
| 454 | /* write */ |
| 455 | dev_dbg(&drv_data->pdev->dev, "write: write_TDBR\n"); |
Scott Jiang | 128465c | 2012-04-23 18:18:12 -0400 | [diff] [blame] | 456 | if (!(n_bytes % 2)) { |
Bob Liu | 4d676fc | 2011-01-11 11:19:07 -0500 | [diff] [blame] | 457 | u16 *buf = (u16 *)drv_data->tx; |
| 458 | for (loop = 0; loop < n_bytes / 2; loop++) { |
Mike Frysinger | 47885ce | 2011-06-17 04:16:56 -0400 | [diff] [blame] | 459 | bfin_read(&drv_data->regs->rdbr); |
| 460 | bfin_write(&drv_data->regs->tdbr, *buf++); |
Bob Liu | 4d676fc | 2011-01-11 11:19:07 -0500 | [diff] [blame] | 461 | } |
| 462 | } else { |
| 463 | u8 *buf = (u8 *)drv_data->tx; |
| 464 | for (loop = 0; loop < n_bytes; loop++) { |
Mike Frysinger | 47885ce | 2011-06-17 04:16:56 -0400 | [diff] [blame] | 465 | bfin_read(&drv_data->regs->rdbr); |
| 466 | bfin_write(&drv_data->regs->tdbr, *buf++); |
Bob Liu | 4d676fc | 2011-01-11 11:19:07 -0500 | [diff] [blame] | 467 | } |
| 468 | } |
Yi Li | f6a6d96 | 2009-06-03 09:46:22 +0000 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | if (drv_data->tx) |
| 472 | drv_data->tx += n_bytes; |
| 473 | if (drv_data->rx) |
| 474 | drv_data->rx += n_bytes; |
| 475 | |
| 476 | return IRQ_HANDLED; |
| 477 | } |
| 478 | |
Mike Frysinger | 138f97c | 2009-04-06 19:00:50 -0700 | [diff] [blame] | 479 | static irqreturn_t bfin_spi_dma_irq_handler(int irq, void *dev_id) |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 480 | { |
Mike Frysinger | 9c0a788 | 2010-10-18 02:45:22 -0400 | [diff] [blame] | 481 | struct bfin_spi_master_data *drv_data = dev_id; |
| 482 | struct bfin_spi_slave_data *chip = drv_data->cur_chip; |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 483 | struct spi_message *msg = drv_data->cur_msg; |
Mike Frysinger | aaaf939 | 2009-04-06 19:00:42 -0700 | [diff] [blame] | 484 | unsigned long timeout; |
Mike Frysinger | d24bd1d | 2009-04-06 19:00:38 -0700 | [diff] [blame] | 485 | unsigned short dmastat = get_dma_curr_irqstat(drv_data->dma_channel); |
Mike Frysinger | 47885ce | 2011-06-17 04:16:56 -0400 | [diff] [blame] | 486 | u16 spistat = bfin_read(&drv_data->regs->stat); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 487 | |
Mike Frysinger | d24bd1d | 2009-04-06 19:00:38 -0700 | [diff] [blame] | 488 | dev_dbg(&drv_data->pdev->dev, |
| 489 | "in dma_irq_handler dmastat:0x%x spistat:0x%x\n", |
| 490 | dmastat, spistat); |
| 491 | |
Michael Hennerich | 782a895 | 2010-10-22 02:01:48 -0400 | [diff] [blame] | 492 | if (drv_data->rx != NULL) { |
Mike Frysinger | 47885ce | 2011-06-17 04:16:56 -0400 | [diff] [blame] | 493 | u16 cr = bfin_read(&drv_data->regs->ctl); |
Michael Hennerich | 782a895 | 2010-10-22 02:01:48 -0400 | [diff] [blame] | 494 | /* discard old RX data and clear RXS */ |
| 495 | bfin_spi_dummy_read(drv_data); |
Mike Frysinger | 47885ce | 2011-06-17 04:16:56 -0400 | [diff] [blame] | 496 | bfin_write(&drv_data->regs->ctl, cr & ~BIT_CTL_ENABLE); /* Disable SPI */ |
| 497 | bfin_write(&drv_data->regs->ctl, cr & ~BIT_CTL_TIMOD); /* Restore State */ |
| 498 | bfin_write(&drv_data->regs->stat, BIT_STAT_CLR); /* Clear Status */ |
Michael Hennerich | 782a895 | 2010-10-22 02:01:48 -0400 | [diff] [blame] | 499 | } |
| 500 | |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 501 | clear_dma_irqstat(drv_data->dma_channel); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 502 | |
| 503 | /* |
Bryan Wu | d6fe89b | 2007-06-11 17:34:17 +0800 | [diff] [blame] | 504 | * wait for the last transaction shifted out. HRM states: |
| 505 | * at this point there may still be data in the SPI DMA FIFO waiting |
| 506 | * to be transmitted ... software needs to poll TXS in the SPI_STAT |
| 507 | * register until it goes low for 2 successive reads |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 508 | */ |
| 509 | if (drv_data->tx != NULL) { |
Mike Frysinger | 47885ce | 2011-06-17 04:16:56 -0400 | [diff] [blame] | 510 | while ((bfin_read(&drv_data->regs->stat) & BIT_STAT_TXS) || |
| 511 | (bfin_read(&drv_data->regs->stat) & BIT_STAT_TXS)) |
Bryan Wu | d8c0500 | 2007-12-04 23:45:21 -0800 | [diff] [blame] | 512 | cpu_relax(); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 513 | } |
| 514 | |
Mike Frysinger | aaaf939 | 2009-04-06 19:00:42 -0700 | [diff] [blame] | 515 | dev_dbg(&drv_data->pdev->dev, |
| 516 | "in dma_irq_handler dmastat:0x%x spistat:0x%x\n", |
Mike Frysinger | 47885ce | 2011-06-17 04:16:56 -0400 | [diff] [blame] | 517 | dmastat, bfin_read(&drv_data->regs->stat)); |
Mike Frysinger | aaaf939 | 2009-04-06 19:00:42 -0700 | [diff] [blame] | 518 | |
| 519 | timeout = jiffies + HZ; |
Mike Frysinger | 47885ce | 2011-06-17 04:16:56 -0400 | [diff] [blame] | 520 | while (!(bfin_read(&drv_data->regs->stat) & BIT_STAT_SPIF)) |
Mike Frysinger | aaaf939 | 2009-04-06 19:00:42 -0700 | [diff] [blame] | 521 | if (!time_before(jiffies, timeout)) { |
Jarkko Nikula | a1829d2 | 2013-10-11 13:53:59 +0300 | [diff] [blame] | 522 | dev_warn(&drv_data->pdev->dev, "timeout waiting for SPIF\n"); |
Mike Frysinger | aaaf939 | 2009-04-06 19:00:42 -0700 | [diff] [blame] | 523 | break; |
| 524 | } else |
| 525 | cpu_relax(); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 526 | |
Mike Frysinger | 90008a6 | 2009-10-15 04:13:29 +0000 | [diff] [blame] | 527 | if ((dmastat & DMA_ERR) && (spistat & BIT_STAT_RBSY)) { |
Mike Frysinger | 04b95d2 | 2009-04-06 19:00:35 -0700 | [diff] [blame] | 528 | msg->state = ERROR_STATE; |
| 529 | dev_err(&drv_data->pdev->dev, "dma receive: fifo/buffer overflow\n"); |
| 530 | } else { |
| 531 | msg->actual_length += drv_data->len_in_bytes; |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 532 | |
Mike Frysinger | 04b95d2 | 2009-04-06 19:00:35 -0700 | [diff] [blame] | 533 | if (drv_data->cs_change) |
Mike Frysinger | 138f97c | 2009-04-06 19:00:50 -0700 | [diff] [blame] | 534 | bfin_spi_cs_deactive(drv_data, chip); |
Bryan Wu | fad91c8 | 2007-12-04 23:45:14 -0800 | [diff] [blame] | 535 | |
Mike Frysinger | 04b95d2 | 2009-04-06 19:00:35 -0700 | [diff] [blame] | 536 | /* Move to next transfer */ |
Mike Frysinger | 138f97c | 2009-04-06 19:00:50 -0700 | [diff] [blame] | 537 | msg->state = bfin_spi_next_transfer(drv_data); |
Mike Frysinger | 04b95d2 | 2009-04-06 19:00:35 -0700 | [diff] [blame] | 538 | } |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 539 | |
| 540 | /* Schedule transfer tasklet */ |
| 541 | tasklet_schedule(&drv_data->pump_transfers); |
| 542 | |
| 543 | /* free the irq handler before next transfer */ |
Bryan Wu | 88b4036 | 2007-05-21 18:32:16 +0800 | [diff] [blame] | 544 | dev_dbg(&drv_data->pdev->dev, |
| 545 | "disable dma channel irq%d\n", |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 546 | drv_data->dma_channel); |
Barry Song | a75bd65b | 2010-01-22 10:07:30 +0000 | [diff] [blame] | 547 | dma_disable_irq_nosync(drv_data->dma_channel); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 548 | |
| 549 | return IRQ_HANDLED; |
| 550 | } |
| 551 | |
Mike Frysinger | 138f97c | 2009-04-06 19:00:50 -0700 | [diff] [blame] | 552 | static void bfin_spi_pump_transfers(unsigned long data) |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 553 | { |
Mike Frysinger | 9c0a788 | 2010-10-18 02:45:22 -0400 | [diff] [blame] | 554 | struct bfin_spi_master_data *drv_data = (struct bfin_spi_master_data *)data; |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 555 | struct spi_message *message = NULL; |
| 556 | struct spi_transfer *transfer = NULL; |
| 557 | struct spi_transfer *previous = NULL; |
Mike Frysinger | 9c0a788 | 2010-10-18 02:45:22 -0400 | [diff] [blame] | 558 | struct bfin_spi_slave_data *chip = NULL; |
Mike Frysinger | 033f44b | 2009-12-18 17:38:04 +0000 | [diff] [blame] | 559 | unsigned int bits_per_word; |
Chen Gang | 057f606 | 2015-04-02 03:03:33 +0800 | [diff] [blame] | 560 | u16 cr, cr_width = 0, dma_width, dma_config; |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 561 | u32 tranf_success = 1; |
Vitja Makarov | 8eeb12e | 2008-05-01 04:35:03 -0700 | [diff] [blame] | 562 | u8 full_duplex = 0; |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 563 | |
| 564 | /* Get current state information */ |
| 565 | message = drv_data->cur_msg; |
| 566 | transfer = drv_data->cur_transfer; |
| 567 | chip = drv_data->cur_chip; |
Bryan Wu | 092e1fd | 2007-12-04 23:45:23 -0800 | [diff] [blame] | 568 | |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 569 | /* |
| 570 | * if msg is error or done, report it back using complete() callback |
| 571 | */ |
| 572 | |
| 573 | /* Handle for abort */ |
| 574 | if (message->state == ERROR_STATE) { |
Mike Frysinger | d24bd1d | 2009-04-06 19:00:38 -0700 | [diff] [blame] | 575 | dev_dbg(&drv_data->pdev->dev, "transfer: we've hit an error\n"); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 576 | message->status = -EIO; |
Mike Frysinger | 138f97c | 2009-04-06 19:00:50 -0700 | [diff] [blame] | 577 | bfin_spi_giveback(drv_data); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 578 | return; |
| 579 | } |
| 580 | |
| 581 | /* Handle end of message */ |
| 582 | if (message->state == DONE_STATE) { |
Mike Frysinger | d24bd1d | 2009-04-06 19:00:38 -0700 | [diff] [blame] | 583 | dev_dbg(&drv_data->pdev->dev, "transfer: all done!\n"); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 584 | message->status = 0; |
Scott Jiang | 2431a81 | 2012-04-23 18:18:13 -0400 | [diff] [blame] | 585 | bfin_spi_flush(drv_data); |
Mike Frysinger | 138f97c | 2009-04-06 19:00:50 -0700 | [diff] [blame] | 586 | bfin_spi_giveback(drv_data); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 587 | return; |
| 588 | } |
| 589 | |
| 590 | /* Delay if requested at end of transfer */ |
| 591 | if (message->state == RUNNING_STATE) { |
Mike Frysinger | d24bd1d | 2009-04-06 19:00:38 -0700 | [diff] [blame] | 592 | dev_dbg(&drv_data->pdev->dev, "transfer: still running ...\n"); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 593 | previous = list_entry(transfer->transfer_list.prev, |
| 594 | struct spi_transfer, transfer_list); |
| 595 | if (previous->delay_usecs) |
| 596 | udelay(previous->delay_usecs); |
| 597 | } |
| 598 | |
Mike Frysinger | ab09e04 | 2009-09-23 23:32:34 +0000 | [diff] [blame] | 599 | /* Flush any existing transfers that may be sitting in the hardware */ |
Mike Frysinger | 138f97c | 2009-04-06 19:00:50 -0700 | [diff] [blame] | 600 | if (bfin_spi_flush(drv_data) == 0) { |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 601 | dev_err(&drv_data->pdev->dev, "pump_transfers: flush failed\n"); |
| 602 | message->status = -EIO; |
Mike Frysinger | 138f97c | 2009-04-06 19:00:50 -0700 | [diff] [blame] | 603 | bfin_spi_giveback(drv_data); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 604 | return; |
| 605 | } |
| 606 | |
Wolfgang Muees | 93b61bd | 2009-04-06 19:00:53 -0700 | [diff] [blame] | 607 | if (transfer->len == 0) { |
| 608 | /* Move to next transfer of this msg */ |
| 609 | message->state = bfin_spi_next_transfer(drv_data); |
| 610 | /* Schedule next transfer tasklet */ |
| 611 | tasklet_schedule(&drv_data->pump_transfers); |
Sonic Zhang | 1974eba | 2011-01-11 11:19:08 -0500 | [diff] [blame] | 612 | return; |
Wolfgang Muees | 93b61bd | 2009-04-06 19:00:53 -0700 | [diff] [blame] | 613 | } |
| 614 | |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 615 | if (transfer->tx_buf != NULL) { |
| 616 | drv_data->tx = (void *)transfer->tx_buf; |
| 617 | drv_data->tx_end = drv_data->tx + transfer->len; |
Bryan Wu | 88b4036 | 2007-05-21 18:32:16 +0800 | [diff] [blame] | 618 | dev_dbg(&drv_data->pdev->dev, "tx_buf is %p, tx_end is %p\n", |
| 619 | transfer->tx_buf, drv_data->tx_end); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 620 | } else { |
| 621 | drv_data->tx = NULL; |
| 622 | } |
| 623 | |
| 624 | if (transfer->rx_buf != NULL) { |
Vitja Makarov | 8eeb12e | 2008-05-01 04:35:03 -0700 | [diff] [blame] | 625 | full_duplex = transfer->tx_buf != NULL; |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 626 | drv_data->rx = transfer->rx_buf; |
| 627 | drv_data->rx_end = drv_data->rx + transfer->len; |
Bryan Wu | 88b4036 | 2007-05-21 18:32:16 +0800 | [diff] [blame] | 628 | dev_dbg(&drv_data->pdev->dev, "rx_buf is %p, rx_end is %p\n", |
| 629 | transfer->rx_buf, drv_data->rx_end); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 630 | } else { |
| 631 | drv_data->rx = NULL; |
| 632 | } |
| 633 | |
| 634 | drv_data->rx_dma = transfer->rx_dma; |
| 635 | drv_data->tx_dma = transfer->tx_dma; |
| 636 | drv_data->len_in_bytes = transfer->len; |
Bryan Wu | fad91c8 | 2007-12-04 23:45:14 -0800 | [diff] [blame] | 637 | drv_data->cs_change = transfer->cs_change; |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 638 | |
Bryan Wu | 092e1fd | 2007-12-04 23:45:23 -0800 | [diff] [blame] | 639 | /* Bits per word setup */ |
Laxman Dewangan | 766ed70 | 2012-12-18 14:25:43 +0530 | [diff] [blame] | 640 | bits_per_word = transfer->bits_per_word; |
Stephen Warren | 24778be | 2013-05-21 20:36:35 -0600 | [diff] [blame] | 641 | if (bits_per_word == 16) { |
Bob Liu | 4d676fc | 2011-01-11 11:19:07 -0500 | [diff] [blame] | 642 | drv_data->n_bytes = bits_per_word/8; |
Mike Frysinger | 5e8592d | 2009-12-18 18:00:10 +0000 | [diff] [blame] | 643 | drv_data->len = (transfer->len) >> 1; |
| 644 | cr_width = BIT_CTL_WORDSIZE; |
Mike Frysinger | 9c0a788 | 2010-10-18 02:45:22 -0400 | [diff] [blame] | 645 | drv_data->ops = &bfin_bfin_spi_transfer_ops_u16; |
Stephen Warren | 24778be | 2013-05-21 20:36:35 -0600 | [diff] [blame] | 646 | } else if (bits_per_word == 8) { |
Bob Liu | 4d676fc | 2011-01-11 11:19:07 -0500 | [diff] [blame] | 647 | drv_data->n_bytes = bits_per_word/8; |
| 648 | drv_data->len = transfer->len; |
Bob Liu | 4d676fc | 2011-01-11 11:19:07 -0500 | [diff] [blame] | 649 | drv_data->ops = &bfin_bfin_spi_transfer_ops_u8; |
Bryan Wu | 092e1fd | 2007-12-04 23:45:23 -0800 | [diff] [blame] | 650 | } |
Mike Frysinger | 47885ce | 2011-06-17 04:16:56 -0400 | [diff] [blame] | 651 | cr = bfin_read(&drv_data->regs->ctl) & ~(BIT_CTL_TIMOD | BIT_CTL_WORDSIZE); |
Mike Frysinger | 5e8592d | 2009-12-18 18:00:10 +0000 | [diff] [blame] | 652 | cr |= cr_width; |
Mike Frysinger | 47885ce | 2011-06-17 04:16:56 -0400 | [diff] [blame] | 653 | bfin_write(&drv_data->regs->ctl, cr); |
Bryan Wu | 092e1fd | 2007-12-04 23:45:23 -0800 | [diff] [blame] | 654 | |
Mike Frysinger | 4fb98ef | 2008-04-08 17:41:57 -0700 | [diff] [blame] | 655 | dev_dbg(&drv_data->pdev->dev, |
Mike Frysinger | 9c4542c | 2009-09-24 01:04:04 +0000 | [diff] [blame] | 656 | "transfer: drv_data->ops is %p, chip->ops is %p, u8_ops is %p\n", |
Mike Frysinger | 9c0a788 | 2010-10-18 02:45:22 -0400 | [diff] [blame] | 657 | drv_data->ops, chip->ops, &bfin_bfin_spi_transfer_ops_u8); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 658 | |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 659 | message->state = RUNNING_STATE; |
| 660 | dma_config = 0; |
| 661 | |
Jarkko Nikula | 95a8fde | 2015-09-15 16:26:17 +0300 | [diff] [blame] | 662 | bfin_write(&drv_data->regs->baud, hz_to_spi_baud(transfer->speed_hz)); |
Bryan Wu | 092e1fd | 2007-12-04 23:45:23 -0800 | [diff] [blame] | 663 | |
Mike Frysinger | 47885ce | 2011-06-17 04:16:56 -0400 | [diff] [blame] | 664 | bfin_write(&drv_data->regs->stat, BIT_STAT_CLR); |
Rob Maris | e72dcde | 2010-04-06 04:17:08 +0000 | [diff] [blame] | 665 | bfin_spi_cs_active(drv_data, chip); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 666 | |
Bryan Wu | 88b4036 | 2007-05-21 18:32:16 +0800 | [diff] [blame] | 667 | dev_dbg(&drv_data->pdev->dev, |
| 668 | "now pumping a transfer: width is %d, len is %d\n", |
Mike Frysinger | 5e8592d | 2009-12-18 18:00:10 +0000 | [diff] [blame] | 669 | cr_width, transfer->len); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 670 | |
| 671 | /* |
Vitja Makarov | 8cf5858 | 2009-04-06 19:00:31 -0700 | [diff] [blame] | 672 | * Try to map dma buffer and do a dma transfer. If successful use, |
| 673 | * different way to r/w according to the enable_dma settings and if |
| 674 | * we are not doing a full duplex transfer (since the hardware does |
| 675 | * not support full duplex DMA transfers). |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 676 | */ |
Vitja Makarov | 8eeb12e | 2008-05-01 04:35:03 -0700 | [diff] [blame] | 677 | if (!full_duplex && drv_data->cur_chip->enable_dma |
| 678 | && drv_data->len > 6) { |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 679 | |
Mike Frysinger | 11d6f59 | 2009-04-06 19:00:41 -0700 | [diff] [blame] | 680 | unsigned long dma_start_addr, flags; |
Mike Frysinger | 7aec356 | 2009-04-06 19:00:36 -0700 | [diff] [blame] | 681 | |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 682 | disable_dma(drv_data->dma_channel); |
| 683 | clear_dma_irqstat(drv_data->dma_channel); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 684 | |
| 685 | /* config dma channel */ |
Bryan Wu | 88b4036 | 2007-05-21 18:32:16 +0800 | [diff] [blame] | 686 | dev_dbg(&drv_data->pdev->dev, "doing dma transfer\n"); |
Mike Frysinger | 7aec356 | 2009-04-06 19:00:36 -0700 | [diff] [blame] | 687 | set_dma_x_count(drv_data->dma_channel, drv_data->len); |
Mike Frysinger | 5e8592d | 2009-12-18 18:00:10 +0000 | [diff] [blame] | 688 | if (cr_width == BIT_CTL_WORDSIZE) { |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 689 | set_dma_x_modify(drv_data->dma_channel, 2); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 690 | dma_width = WDSIZE_16; |
| 691 | } else { |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 692 | set_dma_x_modify(drv_data->dma_channel, 1); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 693 | dma_width = WDSIZE_8; |
| 694 | } |
| 695 | |
Sonic Zhang | 3f479a6 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 696 | /* poll for SPI completion before start */ |
Mike Frysinger | 47885ce | 2011-06-17 04:16:56 -0400 | [diff] [blame] | 697 | while (!(bfin_read(&drv_data->regs->stat) & BIT_STAT_SPIF)) |
Bryan Wu | d8c0500 | 2007-12-04 23:45:21 -0800 | [diff] [blame] | 698 | cpu_relax(); |
Sonic Zhang | 3f479a6 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 699 | |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 700 | /* dirty hack for autobuffer DMA mode */ |
| 701 | if (drv_data->tx_dma == 0xFFFF) { |
Bryan Wu | 88b4036 | 2007-05-21 18:32:16 +0800 | [diff] [blame] | 702 | dev_dbg(&drv_data->pdev->dev, |
| 703 | "doing autobuffer DMA out.\n"); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 704 | |
| 705 | /* no irq in autobuffer mode */ |
| 706 | dma_config = |
| 707 | (DMAFLOW_AUTO | RESTART | dma_width | DI_EN); |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 708 | set_dma_config(drv_data->dma_channel, dma_config); |
| 709 | set_dma_start_addr(drv_data->dma_channel, |
Bryan Wu | a32c691 | 2007-12-04 23:45:15 -0800 | [diff] [blame] | 710 | (unsigned long)drv_data->tx); |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 711 | enable_dma(drv_data->dma_channel); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 712 | |
Sonic Zhang | 07612e5 | 2007-12-04 23:45:21 -0800 | [diff] [blame] | 713 | /* start SPI transfer */ |
Mike Frysinger | 47885ce | 2011-06-17 04:16:56 -0400 | [diff] [blame] | 714 | bfin_write(&drv_data->regs->ctl, cr | BIT_CTL_TIMOD_DMA_TX); |
Sonic Zhang | 07612e5 | 2007-12-04 23:45:21 -0800 | [diff] [blame] | 715 | |
| 716 | /* just return here, there can only be one transfer |
| 717 | * in this mode |
| 718 | */ |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 719 | message->status = 0; |
Mike Frysinger | 138f97c | 2009-04-06 19:00:50 -0700 | [diff] [blame] | 720 | bfin_spi_giveback(drv_data); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 721 | return; |
| 722 | } |
| 723 | |
| 724 | /* In dma mode, rx or tx must be NULL in one transfer */ |
Mike Frysinger | 7aec356 | 2009-04-06 19:00:36 -0700 | [diff] [blame] | 725 | dma_config = (RESTART | dma_width | DI_EN); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 726 | if (drv_data->rx != NULL) { |
| 727 | /* set transfer mode, and enable SPI */ |
Mike Frysinger | d24bd1d | 2009-04-06 19:00:38 -0700 | [diff] [blame] | 728 | dev_dbg(&drv_data->pdev->dev, "doing DMA in to %p (size %zx)\n", |
| 729 | drv_data->rx, drv_data->len_in_bytes); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 730 | |
Vitja Makarov | 8cf5858 | 2009-04-06 19:00:31 -0700 | [diff] [blame] | 731 | /* invalidate caches, if needed */ |
Jie Zhang | 67834fa | 2009-06-10 06:26:26 +0000 | [diff] [blame] | 732 | if (bfin_addr_dcacheable((unsigned long) drv_data->rx)) |
Vitja Makarov | 8cf5858 | 2009-04-06 19:00:31 -0700 | [diff] [blame] | 733 | invalidate_dcache_range((unsigned long) drv_data->rx, |
| 734 | (unsigned long) (drv_data->rx + |
Mike Frysinger | ace3286 | 2009-04-06 19:00:34 -0700 | [diff] [blame] | 735 | drv_data->len_in_bytes)); |
Vitja Makarov | 8cf5858 | 2009-04-06 19:00:31 -0700 | [diff] [blame] | 736 | |
Mike Frysinger | 7aec356 | 2009-04-06 19:00:36 -0700 | [diff] [blame] | 737 | dma_config |= WNR; |
| 738 | dma_start_addr = (unsigned long)drv_data->rx; |
Mike Frysinger | b31e27a | 2009-04-06 19:00:39 -0700 | [diff] [blame] | 739 | cr |= BIT_CTL_TIMOD_DMA_RX | BIT_CTL_SENDOPT; |
Sonic Zhang | 07612e5 | 2007-12-04 23:45:21 -0800 | [diff] [blame] | 740 | |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 741 | } else if (drv_data->tx != NULL) { |
Bryan Wu | 88b4036 | 2007-05-21 18:32:16 +0800 | [diff] [blame] | 742 | dev_dbg(&drv_data->pdev->dev, "doing DMA out.\n"); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 743 | |
Vitja Makarov | 8cf5858 | 2009-04-06 19:00:31 -0700 | [diff] [blame] | 744 | /* flush caches, if needed */ |
Jie Zhang | 67834fa | 2009-06-10 06:26:26 +0000 | [diff] [blame] | 745 | if (bfin_addr_dcacheable((unsigned long) drv_data->tx)) |
Vitja Makarov | 8cf5858 | 2009-04-06 19:00:31 -0700 | [diff] [blame] | 746 | flush_dcache_range((unsigned long) drv_data->tx, |
| 747 | (unsigned long) (drv_data->tx + |
Mike Frysinger | ace3286 | 2009-04-06 19:00:34 -0700 | [diff] [blame] | 748 | drv_data->len_in_bytes)); |
Vitja Makarov | 8cf5858 | 2009-04-06 19:00:31 -0700 | [diff] [blame] | 749 | |
Mike Frysinger | 7aec356 | 2009-04-06 19:00:36 -0700 | [diff] [blame] | 750 | dma_start_addr = (unsigned long)drv_data->tx; |
Mike Frysinger | b31e27a | 2009-04-06 19:00:39 -0700 | [diff] [blame] | 751 | cr |= BIT_CTL_TIMOD_DMA_TX; |
Sonic Zhang | 07612e5 | 2007-12-04 23:45:21 -0800 | [diff] [blame] | 752 | |
Mike Frysinger | 7aec356 | 2009-04-06 19:00:36 -0700 | [diff] [blame] | 753 | } else |
| 754 | BUG(); |
| 755 | |
Mike Frysinger | 11d6f59 | 2009-04-06 19:00:41 -0700 | [diff] [blame] | 756 | /* oh man, here there be monsters ... and i dont mean the |
| 757 | * fluffy cute ones from pixar, i mean the kind that'll eat |
| 758 | * your data, kick your dog, and love it all. do *not* try |
| 759 | * and change these lines unless you (1) heavily test DMA |
| 760 | * with SPI flashes on a loaded system (e.g. ping floods), |
| 761 | * (2) know just how broken the DMA engine interaction with |
| 762 | * the SPI peripheral is, and (3) have someone else to blame |
| 763 | * when you screw it all up anyways. |
| 764 | */ |
Mike Frysinger | 7aec356 | 2009-04-06 19:00:36 -0700 | [diff] [blame] | 765 | set_dma_start_addr(drv_data->dma_channel, dma_start_addr); |
Mike Frysinger | 11d6f59 | 2009-04-06 19:00:41 -0700 | [diff] [blame] | 766 | set_dma_config(drv_data->dma_channel, dma_config); |
| 767 | local_irq_save(flags); |
Mike Frysinger | a963ea8 | 2009-04-06 19:00:43 -0700 | [diff] [blame] | 768 | SSYNC(); |
Mike Frysinger | 47885ce | 2011-06-17 04:16:56 -0400 | [diff] [blame] | 769 | bfin_write(&drv_data->regs->ctl, cr); |
Mike Frysinger | a963ea8 | 2009-04-06 19:00:43 -0700 | [diff] [blame] | 770 | enable_dma(drv_data->dma_channel); |
Mike Frysinger | 11d6f59 | 2009-04-06 19:00:41 -0700 | [diff] [blame] | 771 | dma_enable_irq(drv_data->dma_channel); |
| 772 | local_irq_restore(flags); |
Mike Frysinger | 7aec356 | 2009-04-06 19:00:36 -0700 | [diff] [blame] | 773 | |
Yi Li | f6a6d96 | 2009-06-03 09:46:22 +0000 | [diff] [blame] | 774 | return; |
| 775 | } |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 776 | |
Mike Frysinger | 5e8592d | 2009-12-18 18:00:10 +0000 | [diff] [blame] | 777 | /* |
| 778 | * We always use SPI_WRITE mode (transfer starts with TDBR write). |
| 779 | * SPI_READ mode (transfer starts with RDBR read) seems to have |
| 780 | * problems with setting up the output value in TDBR prior to the |
| 781 | * start of the transfer. |
| 782 | */ |
Mike Frysinger | 47885ce | 2011-06-17 04:16:56 -0400 | [diff] [blame] | 783 | bfin_write(&drv_data->regs->ctl, cr | BIT_CTL_TXMOD); |
Mike Frysinger | 5e8592d | 2009-12-18 18:00:10 +0000 | [diff] [blame] | 784 | |
Yi Li | f6a6d96 | 2009-06-03 09:46:22 +0000 | [diff] [blame] | 785 | if (chip->pio_interrupt) { |
Mike Frysinger | 5e8592d | 2009-12-18 18:00:10 +0000 | [diff] [blame] | 786 | /* SPI irq should have been disabled by now */ |
Wolfgang Muees | 93b61bd | 2009-04-06 19:00:53 -0700 | [diff] [blame] | 787 | |
Yi Li | f6a6d96 | 2009-06-03 09:46:22 +0000 | [diff] [blame] | 788 | /* discard old RX data and clear RXS */ |
| 789 | bfin_spi_dummy_read(drv_data); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 790 | |
Yi Li | f6a6d96 | 2009-06-03 09:46:22 +0000 | [diff] [blame] | 791 | /* start transfer */ |
| 792 | if (drv_data->tx == NULL) |
Mike Frysinger | 47885ce | 2011-06-17 04:16:56 -0400 | [diff] [blame] | 793 | bfin_write(&drv_data->regs->tdbr, chip->idle_tx_val); |
Yi Li | f6a6d96 | 2009-06-03 09:46:22 +0000 | [diff] [blame] | 794 | else { |
Bob Liu | 4d676fc | 2011-01-11 11:19:07 -0500 | [diff] [blame] | 795 | int loop; |
Stephen Warren | 24778be | 2013-05-21 20:36:35 -0600 | [diff] [blame] | 796 | if (bits_per_word == 16) { |
Bob Liu | 4d676fc | 2011-01-11 11:19:07 -0500 | [diff] [blame] | 797 | u16 *buf = (u16 *)drv_data->tx; |
| 798 | for (loop = 0; loop < bits_per_word / 16; |
| 799 | loop++) { |
Mike Frysinger | 47885ce | 2011-06-17 04:16:56 -0400 | [diff] [blame] | 800 | bfin_write(&drv_data->regs->tdbr, *buf++); |
Bob Liu | 4d676fc | 2011-01-11 11:19:07 -0500 | [diff] [blame] | 801 | } |
Stephen Warren | 24778be | 2013-05-21 20:36:35 -0600 | [diff] [blame] | 802 | } else if (bits_per_word == 8) { |
Bob Liu | 4d676fc | 2011-01-11 11:19:07 -0500 | [diff] [blame] | 803 | u8 *buf = (u8 *)drv_data->tx; |
| 804 | for (loop = 0; loop < bits_per_word / 8; loop++) |
Mike Frysinger | 47885ce | 2011-06-17 04:16:56 -0400 | [diff] [blame] | 805 | bfin_write(&drv_data->regs->tdbr, *buf++); |
Bob Liu | 4d676fc | 2011-01-11 11:19:07 -0500 | [diff] [blame] | 806 | } |
| 807 | |
Yi Li | f6a6d96 | 2009-06-03 09:46:22 +0000 | [diff] [blame] | 808 | drv_data->tx += drv_data->n_bytes; |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 809 | } |
| 810 | |
Yi Li | f6a6d96 | 2009-06-03 09:46:22 +0000 | [diff] [blame] | 811 | /* once TDBR is empty, interrupt is triggered */ |
| 812 | enable_irq(drv_data->spi_irq); |
| 813 | return; |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 814 | } |
Yi Li | f6a6d96 | 2009-06-03 09:46:22 +0000 | [diff] [blame] | 815 | |
| 816 | /* IO mode */ |
| 817 | dev_dbg(&drv_data->pdev->dev, "doing IO transfer\n"); |
| 818 | |
Yi Li | f6a6d96 | 2009-06-03 09:46:22 +0000 | [diff] [blame] | 819 | if (full_duplex) { |
| 820 | /* full duplex mode */ |
| 821 | BUG_ON((drv_data->tx_end - drv_data->tx) != |
| 822 | (drv_data->rx_end - drv_data->rx)); |
| 823 | dev_dbg(&drv_data->pdev->dev, |
| 824 | "IO duplex: cr is 0x%x\n", cr); |
| 825 | |
Mike Frysinger | 9c4542c | 2009-09-24 01:04:04 +0000 | [diff] [blame] | 826 | drv_data->ops->duplex(drv_data); |
Yi Li | f6a6d96 | 2009-06-03 09:46:22 +0000 | [diff] [blame] | 827 | |
| 828 | if (drv_data->tx != drv_data->tx_end) |
| 829 | tranf_success = 0; |
| 830 | } else if (drv_data->tx != NULL) { |
| 831 | /* write only half duplex */ |
| 832 | dev_dbg(&drv_data->pdev->dev, |
| 833 | "IO write: cr is 0x%x\n", cr); |
| 834 | |
Mike Frysinger | 9c4542c | 2009-09-24 01:04:04 +0000 | [diff] [blame] | 835 | drv_data->ops->write(drv_data); |
Yi Li | f6a6d96 | 2009-06-03 09:46:22 +0000 | [diff] [blame] | 836 | |
| 837 | if (drv_data->tx != drv_data->tx_end) |
| 838 | tranf_success = 0; |
| 839 | } else if (drv_data->rx != NULL) { |
| 840 | /* read only half duplex */ |
| 841 | dev_dbg(&drv_data->pdev->dev, |
| 842 | "IO read: cr is 0x%x\n", cr); |
| 843 | |
Mike Frysinger | 9c4542c | 2009-09-24 01:04:04 +0000 | [diff] [blame] | 844 | drv_data->ops->read(drv_data); |
Yi Li | f6a6d96 | 2009-06-03 09:46:22 +0000 | [diff] [blame] | 845 | if (drv_data->rx != drv_data->rx_end) |
| 846 | tranf_success = 0; |
| 847 | } |
| 848 | |
| 849 | if (!tranf_success) { |
| 850 | dev_dbg(&drv_data->pdev->dev, |
| 851 | "IO write error!\n"); |
| 852 | message->state = ERROR_STATE; |
| 853 | } else { |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 854 | /* Update total byte transferred */ |
Yi Li | f6a6d96 | 2009-06-03 09:46:22 +0000 | [diff] [blame] | 855 | message->actual_length += drv_data->len_in_bytes; |
| 856 | /* Move to next transfer of this msg */ |
| 857 | message->state = bfin_spi_next_transfer(drv_data); |
Scott Jiang | 2431a81 | 2012-04-23 18:18:13 -0400 | [diff] [blame] | 858 | if (drv_data->cs_change && message->state != DONE_STATE) { |
| 859 | bfin_spi_flush(drv_data); |
Yi Li | f6a6d96 | 2009-06-03 09:46:22 +0000 | [diff] [blame] | 860 | bfin_spi_cs_deactive(drv_data, chip); |
Scott Jiang | 2431a81 | 2012-04-23 18:18:13 -0400 | [diff] [blame] | 861 | } |
Yi Li | f6a6d96 | 2009-06-03 09:46:22 +0000 | [diff] [blame] | 862 | } |
| 863 | |
| 864 | /* Schedule next transfer tasklet */ |
| 865 | tasklet_schedule(&drv_data->pump_transfers); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 866 | } |
| 867 | |
| 868 | /* pop a msg from queue and kick off real transfer */ |
Mike Frysinger | 138f97c | 2009-04-06 19:00:50 -0700 | [diff] [blame] | 869 | static void bfin_spi_pump_messages(struct work_struct *work) |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 870 | { |
Mike Frysinger | 9c0a788 | 2010-10-18 02:45:22 -0400 | [diff] [blame] | 871 | struct bfin_spi_master_data *drv_data; |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 872 | unsigned long flags; |
| 873 | |
Mike Frysinger | 9c0a788 | 2010-10-18 02:45:22 -0400 | [diff] [blame] | 874 | drv_data = container_of(work, struct bfin_spi_master_data, pump_messages); |
Bryan Wu | 131b17d | 2007-12-04 23:45:12 -0800 | [diff] [blame] | 875 | |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 876 | /* Lock queue and check for queue work */ |
| 877 | spin_lock_irqsave(&drv_data->lock, flags); |
Mike Frysinger | f4f50c3 | 2009-09-24 00:41:49 +0000 | [diff] [blame] | 878 | if (list_empty(&drv_data->queue) || !drv_data->running) { |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 879 | /* pumper kicked off but no work to do */ |
| 880 | drv_data->busy = 0; |
| 881 | spin_unlock_irqrestore(&drv_data->lock, flags); |
| 882 | return; |
| 883 | } |
| 884 | |
| 885 | /* Make sure we are not already running a message */ |
| 886 | if (drv_data->cur_msg) { |
| 887 | spin_unlock_irqrestore(&drv_data->lock, flags); |
| 888 | return; |
| 889 | } |
| 890 | |
| 891 | /* Extract head of queue */ |
| 892 | drv_data->cur_msg = list_entry(drv_data->queue.next, |
| 893 | struct spi_message, queue); |
Bryan Wu | 5fec5b5 | 2007-12-04 23:45:13 -0800 | [diff] [blame] | 894 | |
| 895 | /* Setup the SSP using the per chip configuration */ |
| 896 | drv_data->cur_chip = spi_get_ctldata(drv_data->cur_msg->spi); |
Mike Frysinger | 138f97c | 2009-04-06 19:00:50 -0700 | [diff] [blame] | 897 | bfin_spi_restore_state(drv_data); |
Bryan Wu | 5fec5b5 | 2007-12-04 23:45:13 -0800 | [diff] [blame] | 898 | |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 899 | list_del_init(&drv_data->cur_msg->queue); |
| 900 | |
| 901 | /* Initial message state */ |
| 902 | drv_data->cur_msg->state = START_STATE; |
| 903 | drv_data->cur_transfer = list_entry(drv_data->cur_msg->transfers.next, |
| 904 | struct spi_transfer, transfer_list); |
| 905 | |
Jarkko Nikula | f6bd03a | 2013-10-11 13:54:00 +0300 | [diff] [blame] | 906 | dev_dbg(&drv_data->pdev->dev, |
| 907 | "got a message to pump, state is set to: baud " |
| 908 | "%d, flag 0x%x, ctl 0x%x\n", |
Bryan Wu | 5fec5b5 | 2007-12-04 23:45:13 -0800 | [diff] [blame] | 909 | drv_data->cur_chip->baud, drv_data->cur_chip->flag, |
| 910 | drv_data->cur_chip->ctl_reg); |
Bryan Wu | 131b17d | 2007-12-04 23:45:12 -0800 | [diff] [blame] | 911 | |
| 912 | dev_dbg(&drv_data->pdev->dev, |
Bryan Wu | 88b4036 | 2007-05-21 18:32:16 +0800 | [diff] [blame] | 913 | "the first transfer len is %d\n", |
| 914 | drv_data->cur_transfer->len); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 915 | |
| 916 | /* Mark as busy and launch transfers */ |
| 917 | tasklet_schedule(&drv_data->pump_transfers); |
| 918 | |
| 919 | drv_data->busy = 1; |
| 920 | spin_unlock_irqrestore(&drv_data->lock, flags); |
| 921 | } |
| 922 | |
| 923 | /* |
| 924 | * got a msg to transfer, queue it in drv_data->queue. |
| 925 | * And kick off message pumper |
| 926 | */ |
Mike Frysinger | 138f97c | 2009-04-06 19:00:50 -0700 | [diff] [blame] | 927 | static int bfin_spi_transfer(struct spi_device *spi, struct spi_message *msg) |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 928 | { |
Mike Frysinger | 9c0a788 | 2010-10-18 02:45:22 -0400 | [diff] [blame] | 929 | struct bfin_spi_master_data *drv_data = spi_master_get_devdata(spi->master); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 930 | unsigned long flags; |
| 931 | |
| 932 | spin_lock_irqsave(&drv_data->lock, flags); |
| 933 | |
Mike Frysinger | f4f50c3 | 2009-09-24 00:41:49 +0000 | [diff] [blame] | 934 | if (!drv_data->running) { |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 935 | spin_unlock_irqrestore(&drv_data->lock, flags); |
| 936 | return -ESHUTDOWN; |
| 937 | } |
| 938 | |
| 939 | msg->actual_length = 0; |
| 940 | msg->status = -EINPROGRESS; |
| 941 | msg->state = START_STATE; |
| 942 | |
Bryan Wu | 88b4036 | 2007-05-21 18:32:16 +0800 | [diff] [blame] | 943 | dev_dbg(&spi->dev, "adding an msg in transfer() \n"); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 944 | list_add_tail(&msg->queue, &drv_data->queue); |
| 945 | |
Mike Frysinger | f4f50c3 | 2009-09-24 00:41:49 +0000 | [diff] [blame] | 946 | if (drv_data->running && !drv_data->busy) |
Bhaktipriya Shridhar | 9b96f07 | 2016-07-02 14:12:00 +0530 | [diff] [blame] | 947 | schedule_work(&drv_data->pump_messages); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 948 | |
| 949 | spin_unlock_irqrestore(&drv_data->lock, flags); |
| 950 | |
| 951 | return 0; |
| 952 | } |
| 953 | |
Sonic Zhang | 12e17c4 | 2007-12-04 23:45:16 -0800 | [diff] [blame] | 954 | #define MAX_SPI_SSEL 7 |
| 955 | |
Mike Frysinger | ddc0bf1 | 2011-06-17 04:16:57 -0400 | [diff] [blame] | 956 | static const u16 ssel[][MAX_SPI_SSEL] = { |
Sonic Zhang | 12e17c4 | 2007-12-04 23:45:16 -0800 | [diff] [blame] | 957 | {P_SPI0_SSEL1, P_SPI0_SSEL2, P_SPI0_SSEL3, |
| 958 | P_SPI0_SSEL4, P_SPI0_SSEL5, |
| 959 | P_SPI0_SSEL6, P_SPI0_SSEL7}, |
| 960 | |
| 961 | {P_SPI1_SSEL1, P_SPI1_SSEL2, P_SPI1_SSEL3, |
| 962 | P_SPI1_SSEL4, P_SPI1_SSEL5, |
| 963 | P_SPI1_SSEL6, P_SPI1_SSEL7}, |
| 964 | |
| 965 | {P_SPI2_SSEL1, P_SPI2_SSEL2, P_SPI2_SSEL3, |
| 966 | P_SPI2_SSEL4, P_SPI2_SSEL5, |
| 967 | P_SPI2_SSEL6, P_SPI2_SSEL7}, |
| 968 | }; |
| 969 | |
Mike Frysinger | ab09e04 | 2009-09-23 23:32:34 +0000 | [diff] [blame] | 970 | /* setup for devices (may be called multiple times -- not just first setup) */ |
Mike Frysinger | 138f97c | 2009-04-06 19:00:50 -0700 | [diff] [blame] | 971 | static int bfin_spi_setup(struct spi_device *spi) |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 972 | { |
Daniel Mack | ac01e97 | 2009-03-25 00:18:35 +0000 | [diff] [blame] | 973 | struct bfin5xx_spi_chip *chip_info; |
Mike Frysinger | 9c0a788 | 2010-10-18 02:45:22 -0400 | [diff] [blame] | 974 | struct bfin_spi_slave_data *chip = NULL; |
| 975 | struct bfin_spi_master_data *drv_data = spi_master_get_devdata(spi->master); |
Mike Frysinger | 5b47bcd | 2009-12-18 17:43:31 +0000 | [diff] [blame] | 976 | u16 bfin_ctl_reg; |
Daniel Mack | ac01e97 | 2009-03-25 00:18:35 +0000 | [diff] [blame] | 977 | int ret = -EINVAL; |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 978 | |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 979 | /* Only alloc (or use chip_info) on first setup */ |
Daniel Mack | ac01e97 | 2009-03-25 00:18:35 +0000 | [diff] [blame] | 980 | chip_info = NULL; |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 981 | chip = spi_get_ctldata(spi); |
| 982 | if (chip == NULL) { |
Daniel Mack | ac01e97 | 2009-03-25 00:18:35 +0000 | [diff] [blame] | 983 | chip = kzalloc(sizeof(*chip), GFP_KERNEL); |
| 984 | if (!chip) { |
| 985 | dev_err(&spi->dev, "cannot allocate chip data\n"); |
| 986 | ret = -ENOMEM; |
| 987 | goto error; |
| 988 | } |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 989 | |
| 990 | chip->enable_dma = 0; |
| 991 | chip_info = spi->controller_data; |
| 992 | } |
| 993 | |
Mike Frysinger | 5b47bcd | 2009-12-18 17:43:31 +0000 | [diff] [blame] | 994 | /* Let people set non-standard bits directly */ |
| 995 | bfin_ctl_reg = BIT_CTL_OPENDRAIN | BIT_CTL_EMISO | |
| 996 | BIT_CTL_PSSE | BIT_CTL_GM | BIT_CTL_SZ; |
| 997 | |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 998 | /* chip_info isn't always needed */ |
| 999 | if (chip_info) { |
Mike Frysinger | 2ed3551 | 2007-12-04 23:45:14 -0800 | [diff] [blame] | 1000 | /* Make sure people stop trying to set fields via ctl_reg |
| 1001 | * when they should actually be using common SPI framework. |
Mike Frysinger | 90008a6 | 2009-10-15 04:13:29 +0000 | [diff] [blame] | 1002 | * Currently we let through: WOM EMISO PSSE GM SZ. |
Mike Frysinger | 2ed3551 | 2007-12-04 23:45:14 -0800 | [diff] [blame] | 1003 | * Not sure if a user actually needs/uses any of these, |
| 1004 | * but let's assume (for now) they do. |
| 1005 | */ |
Mike Frysinger | 5b47bcd | 2009-12-18 17:43:31 +0000 | [diff] [blame] | 1006 | if (chip_info->ctl_reg & ~bfin_ctl_reg) { |
Jarkko Nikula | f6bd03a | 2013-10-11 13:54:00 +0300 | [diff] [blame] | 1007 | dev_err(&spi->dev, |
| 1008 | "do not set bits in ctl_reg that the SPI framework manages\n"); |
Daniel Mack | ac01e97 | 2009-03-25 00:18:35 +0000 | [diff] [blame] | 1009 | goto error; |
Mike Frysinger | 2ed3551 | 2007-12-04 23:45:14 -0800 | [diff] [blame] | 1010 | } |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1011 | chip->enable_dma = chip_info->enable_dma != 0 |
| 1012 | && drv_data->master_info->enable_dma; |
| 1013 | chip->ctl_reg = chip_info->ctl_reg; |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1014 | chip->cs_chg_udelay = chip_info->cs_chg_udelay; |
Wolfgang Muees | 93b61bd | 2009-04-06 19:00:53 -0700 | [diff] [blame] | 1015 | chip->idle_tx_val = chip_info->idle_tx_val; |
Yi Li | f6a6d96 | 2009-06-03 09:46:22 +0000 | [diff] [blame] | 1016 | chip->pio_interrupt = chip_info->pio_interrupt; |
Mike Frysinger | 5b47bcd | 2009-12-18 17:43:31 +0000 | [diff] [blame] | 1017 | } else { |
| 1018 | /* force a default base state */ |
| 1019 | chip->ctl_reg &= bfin_ctl_reg; |
Mike Frysinger | 033f44b | 2009-12-18 17:38:04 +0000 | [diff] [blame] | 1020 | } |
| 1021 | |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1022 | /* translate common spi framework into our register */ |
| 1023 | if (spi->mode & SPI_CPOL) |
Mike Frysinger | 90008a6 | 2009-10-15 04:13:29 +0000 | [diff] [blame] | 1024 | chip->ctl_reg |= BIT_CTL_CPOL; |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1025 | if (spi->mode & SPI_CPHA) |
Mike Frysinger | 90008a6 | 2009-10-15 04:13:29 +0000 | [diff] [blame] | 1026 | chip->ctl_reg |= BIT_CTL_CPHA; |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1027 | if (spi->mode & SPI_LSB_FIRST) |
Mike Frysinger | 90008a6 | 2009-10-15 04:13:29 +0000 | [diff] [blame] | 1028 | chip->ctl_reg |= BIT_CTL_LSBF; |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1029 | /* we dont support running in slave mode (yet?) */ |
Mike Frysinger | 90008a6 | 2009-10-15 04:13:29 +0000 | [diff] [blame] | 1030 | chip->ctl_reg |= BIT_CTL_MASTER; |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1031 | |
| 1032 | /* |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1033 | * Notice: for blackfin, the speed_hz is the value of register |
| 1034 | * SPI_BAUD, not the real baudrate |
| 1035 | */ |
| 1036 | chip->baud = hz_to_spi_baud(spi->max_speed_hz); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1037 | chip->chip_select_num = spi->chip_select; |
Barry Song | 4190f6a | 2010-04-06 03:36:24 +0000 | [diff] [blame] | 1038 | if (chip->chip_select_num < MAX_CTRL_CS) { |
| 1039 | if (!(spi->mode & SPI_CPHA)) |
Jarkko Nikula | f6bd03a | 2013-10-11 13:54:00 +0300 | [diff] [blame] | 1040 | dev_warn(&spi->dev, |
| 1041 | "Warning: SPI CPHA not set: Slave Select not under software control!\n" |
| 1042 | "See Documentation/blackfin/bfin-spi-notes.txt\n"); |
Barry Song | 4190f6a | 2010-04-06 03:36:24 +0000 | [diff] [blame] | 1043 | |
Barry Song | d3cc71f | 2009-11-17 09:45:59 +0000 | [diff] [blame] | 1044 | chip->flag = (1 << spi->chip_select) << 8; |
Barry Song | 4190f6a | 2010-04-06 03:36:24 +0000 | [diff] [blame] | 1045 | } else |
Barry Song | d3cc71f | 2009-11-17 09:45:59 +0000 | [diff] [blame] | 1046 | chip->cs_gpio = chip->chip_select_num - MAX_CTRL_CS; |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1047 | |
Yi Li | f6a6d96 | 2009-06-03 09:46:22 +0000 | [diff] [blame] | 1048 | if (chip->enable_dma && chip->pio_interrupt) { |
Jarkko Nikula | f6bd03a | 2013-10-11 13:54:00 +0300 | [diff] [blame] | 1049 | dev_err(&spi->dev, |
| 1050 | "enable_dma is set, do not set pio_interrupt\n"); |
Yi Li | f6a6d96 | 2009-06-03 09:46:22 +0000 | [diff] [blame] | 1051 | goto error; |
| 1052 | } |
Daniel Mack | ac01e97 | 2009-03-25 00:18:35 +0000 | [diff] [blame] | 1053 | /* |
| 1054 | * if any one SPI chip is registered and wants DMA, request the |
| 1055 | * DMA channel for it |
| 1056 | */ |
| 1057 | if (chip->enable_dma && !drv_data->dma_requested) { |
| 1058 | /* register dma irq handler */ |
| 1059 | ret = request_dma(drv_data->dma_channel, "BFIN_SPI_DMA"); |
| 1060 | if (ret) { |
| 1061 | dev_err(&spi->dev, |
| 1062 | "Unable to request BlackFin SPI DMA channel\n"); |
| 1063 | goto error; |
| 1064 | } |
| 1065 | drv_data->dma_requested = 1; |
| 1066 | |
| 1067 | ret = set_dma_callback(drv_data->dma_channel, |
| 1068 | bfin_spi_dma_irq_handler, drv_data); |
| 1069 | if (ret) { |
| 1070 | dev_err(&spi->dev, "Unable to set dma callback\n"); |
| 1071 | goto error; |
| 1072 | } |
| 1073 | dma_disable_irq(drv_data->dma_channel); |
| 1074 | } |
| 1075 | |
Yi Li | f6a6d96 | 2009-06-03 09:46:22 +0000 | [diff] [blame] | 1076 | if (chip->pio_interrupt && !drv_data->irq_requested) { |
| 1077 | ret = request_irq(drv_data->spi_irq, bfin_spi_pio_irq_handler, |
Yong Zhang | 38ada21 | 2011-10-22 17:56:55 +0800 | [diff] [blame] | 1078 | 0, "BFIN_SPI", drv_data); |
Yi Li | f6a6d96 | 2009-06-03 09:46:22 +0000 | [diff] [blame] | 1079 | if (ret) { |
| 1080 | dev_err(&spi->dev, "Unable to register spi IRQ\n"); |
| 1081 | goto error; |
| 1082 | } |
| 1083 | drv_data->irq_requested = 1; |
| 1084 | /* we use write mode, spi irq has to be disabled here */ |
| 1085 | disable_irq(drv_data->spi_irq); |
| 1086 | } |
| 1087 | |
Barry Song | d3cc71f | 2009-11-17 09:45:59 +0000 | [diff] [blame] | 1088 | if (chip->chip_select_num >= MAX_CTRL_CS) { |
Michael Hennerich | 73e1ac1 | 2010-10-22 02:01:47 -0400 | [diff] [blame] | 1089 | /* Only request on first setup */ |
| 1090 | if (spi_get_ctldata(spi) == NULL) { |
| 1091 | ret = gpio_request(chip->cs_gpio, spi->modalias); |
| 1092 | if (ret) { |
| 1093 | dev_err(&spi->dev, "gpio_request() error\n"); |
| 1094 | goto pin_error; |
| 1095 | } |
| 1096 | gpio_direction_output(chip->cs_gpio, 1); |
Daniel Mack | ac01e97 | 2009-03-25 00:18:35 +0000 | [diff] [blame] | 1097 | } |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1098 | } |
| 1099 | |
Joe Perches | 898eb71 | 2007-10-18 03:06:30 -0700 | [diff] [blame] | 1100 | dev_dbg(&spi->dev, "setup spi chip %s, width is %d, dma is %d\n", |
Mike Frysinger | 033f44b | 2009-12-18 17:38:04 +0000 | [diff] [blame] | 1101 | spi->modalias, spi->bits_per_word, chip->enable_dma); |
Bryan Wu | 88b4036 | 2007-05-21 18:32:16 +0800 | [diff] [blame] | 1102 | dev_dbg(&spi->dev, "ctl_reg is 0x%x, flag_reg is 0x%x\n", |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1103 | chip->ctl_reg, chip->flag); |
| 1104 | |
| 1105 | spi_set_ctldata(spi, chip); |
| 1106 | |
Sonic Zhang | 12e17c4 | 2007-12-04 23:45:16 -0800 | [diff] [blame] | 1107 | dev_dbg(&spi->dev, "chip select number is %d\n", chip->chip_select_num); |
Barry Song | d3cc71f | 2009-11-17 09:45:59 +0000 | [diff] [blame] | 1108 | if (chip->chip_select_num < MAX_CTRL_CS) { |
Daniel Mack | ac01e97 | 2009-03-25 00:18:35 +0000 | [diff] [blame] | 1109 | ret = peripheral_request(ssel[spi->master->bus_num] |
| 1110 | [chip->chip_select_num-1], spi->modalias); |
| 1111 | if (ret) { |
| 1112 | dev_err(&spi->dev, "peripheral_request() error\n"); |
| 1113 | goto pin_error; |
| 1114 | } |
| 1115 | } |
Sonic Zhang | 12e17c4 | 2007-12-04 23:45:16 -0800 | [diff] [blame] | 1116 | |
Barry Song | 8221610 | 2009-06-17 10:10:53 +0000 | [diff] [blame] | 1117 | bfin_spi_cs_enable(drv_data, chip); |
Mike Frysinger | 138f97c | 2009-04-06 19:00:50 -0700 | [diff] [blame] | 1118 | bfin_spi_cs_deactive(drv_data, chip); |
Sonic Zhang | 07612e5 | 2007-12-04 23:45:21 -0800 | [diff] [blame] | 1119 | |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1120 | return 0; |
Daniel Mack | ac01e97 | 2009-03-25 00:18:35 +0000 | [diff] [blame] | 1121 | |
| 1122 | pin_error: |
Barry Song | d3cc71f | 2009-11-17 09:45:59 +0000 | [diff] [blame] | 1123 | if (chip->chip_select_num >= MAX_CTRL_CS) |
Daniel Mack | ac01e97 | 2009-03-25 00:18:35 +0000 | [diff] [blame] | 1124 | gpio_free(chip->cs_gpio); |
| 1125 | else |
| 1126 | peripheral_free(ssel[spi->master->bus_num] |
| 1127 | [chip->chip_select_num - 1]); |
| 1128 | error: |
| 1129 | if (chip) { |
| 1130 | if (drv_data->dma_requested) |
| 1131 | free_dma(drv_data->dma_channel); |
| 1132 | drv_data->dma_requested = 0; |
| 1133 | |
| 1134 | kfree(chip); |
| 1135 | /* prevent free 'chip' twice */ |
| 1136 | spi_set_ctldata(spi, NULL); |
| 1137 | } |
| 1138 | |
| 1139 | return ret; |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1140 | } |
| 1141 | |
| 1142 | /* |
| 1143 | * callback for spi framework. |
| 1144 | * clean driver specific data |
| 1145 | */ |
Mike Frysinger | 138f97c | 2009-04-06 19:00:50 -0700 | [diff] [blame] | 1146 | static void bfin_spi_cleanup(struct spi_device *spi) |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1147 | { |
Mike Frysinger | 9c0a788 | 2010-10-18 02:45:22 -0400 | [diff] [blame] | 1148 | struct bfin_spi_slave_data *chip = spi_get_ctldata(spi); |
| 1149 | struct bfin_spi_master_data *drv_data = spi_master_get_devdata(spi->master); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1150 | |
Mike Frysinger | e7d02e3 | 2009-04-06 19:00:51 -0700 | [diff] [blame] | 1151 | if (!chip) |
| 1152 | return; |
| 1153 | |
Barry Song | d3cc71f | 2009-11-17 09:45:59 +0000 | [diff] [blame] | 1154 | if (chip->chip_select_num < MAX_CTRL_CS) { |
Sonic Zhang | 12e17c4 | 2007-12-04 23:45:16 -0800 | [diff] [blame] | 1155 | peripheral_free(ssel[spi->master->bus_num] |
| 1156 | [chip->chip_select_num-1]); |
Barry Song | 8221610 | 2009-06-17 10:10:53 +0000 | [diff] [blame] | 1157 | bfin_spi_cs_disable(drv_data, chip); |
Barry Song | d3cc71f | 2009-11-17 09:45:59 +0000 | [diff] [blame] | 1158 | } else |
Michael Hennerich | 42c78b2 | 2009-04-06 19:00:51 -0700 | [diff] [blame] | 1159 | gpio_free(chip->cs_gpio); |
| 1160 | |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1161 | kfree(chip); |
Daniel Mack | ac01e97 | 2009-03-25 00:18:35 +0000 | [diff] [blame] | 1162 | /* prevent free 'chip' twice */ |
| 1163 | spi_set_ctldata(spi, NULL); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1164 | } |
| 1165 | |
Mike Frysinger | c52d4e5 | 2011-06-17 04:16:58 -0400 | [diff] [blame] | 1166 | static int bfin_spi_init_queue(struct bfin_spi_master_data *drv_data) |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1167 | { |
| 1168 | INIT_LIST_HEAD(&drv_data->queue); |
| 1169 | spin_lock_init(&drv_data->lock); |
| 1170 | |
Mike Frysinger | f4f50c3 | 2009-09-24 00:41:49 +0000 | [diff] [blame] | 1171 | drv_data->running = false; |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1172 | drv_data->busy = 0; |
| 1173 | |
| 1174 | /* init transfer tasklet */ |
| 1175 | tasklet_init(&drv_data->pump_transfers, |
Mike Frysinger | 138f97c | 2009-04-06 19:00:50 -0700 | [diff] [blame] | 1176 | bfin_spi_pump_transfers, (unsigned long)drv_data); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1177 | |
Mike Frysinger | 138f97c | 2009-04-06 19:00:50 -0700 | [diff] [blame] | 1178 | INIT_WORK(&drv_data->pump_messages, bfin_spi_pump_messages); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1179 | |
| 1180 | return 0; |
| 1181 | } |
| 1182 | |
Mike Frysinger | c52d4e5 | 2011-06-17 04:16:58 -0400 | [diff] [blame] | 1183 | static int bfin_spi_start_queue(struct bfin_spi_master_data *drv_data) |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1184 | { |
| 1185 | unsigned long flags; |
| 1186 | |
| 1187 | spin_lock_irqsave(&drv_data->lock, flags); |
| 1188 | |
Mike Frysinger | f4f50c3 | 2009-09-24 00:41:49 +0000 | [diff] [blame] | 1189 | if (drv_data->running || drv_data->busy) { |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1190 | spin_unlock_irqrestore(&drv_data->lock, flags); |
| 1191 | return -EBUSY; |
| 1192 | } |
| 1193 | |
Mike Frysinger | f4f50c3 | 2009-09-24 00:41:49 +0000 | [diff] [blame] | 1194 | drv_data->running = true; |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1195 | drv_data->cur_msg = NULL; |
| 1196 | drv_data->cur_transfer = NULL; |
| 1197 | drv_data->cur_chip = NULL; |
| 1198 | spin_unlock_irqrestore(&drv_data->lock, flags); |
| 1199 | |
Bhaktipriya Shridhar | 9b96f07 | 2016-07-02 14:12:00 +0530 | [diff] [blame] | 1200 | schedule_work(&drv_data->pump_messages); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1201 | |
| 1202 | return 0; |
| 1203 | } |
| 1204 | |
Mike Frysinger | c52d4e5 | 2011-06-17 04:16:58 -0400 | [diff] [blame] | 1205 | static int bfin_spi_stop_queue(struct bfin_spi_master_data *drv_data) |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1206 | { |
| 1207 | unsigned long flags; |
| 1208 | unsigned limit = 500; |
| 1209 | int status = 0; |
| 1210 | |
| 1211 | spin_lock_irqsave(&drv_data->lock, flags); |
| 1212 | |
| 1213 | /* |
| 1214 | * This is a bit lame, but is optimized for the common execution path. |
| 1215 | * A wait_queue on the drv_data->busy could be used, but then the common |
| 1216 | * execution path (pump_messages) would be required to call wake_up or |
| 1217 | * friends on every SPI message. Do this instead |
| 1218 | */ |
Mike Frysinger | f4f50c3 | 2009-09-24 00:41:49 +0000 | [diff] [blame] | 1219 | drv_data->running = false; |
Vasily Khoruzhick | 850a28e | 2011-04-06 17:49:15 +0300 | [diff] [blame] | 1220 | while ((!list_empty(&drv_data->queue) || drv_data->busy) && limit--) { |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1221 | spin_unlock_irqrestore(&drv_data->lock, flags); |
| 1222 | msleep(10); |
| 1223 | spin_lock_irqsave(&drv_data->lock, flags); |
| 1224 | } |
| 1225 | |
| 1226 | if (!list_empty(&drv_data->queue) || drv_data->busy) |
| 1227 | status = -EBUSY; |
| 1228 | |
| 1229 | spin_unlock_irqrestore(&drv_data->lock, flags); |
| 1230 | |
| 1231 | return status; |
| 1232 | } |
| 1233 | |
Mike Frysinger | c52d4e5 | 2011-06-17 04:16:58 -0400 | [diff] [blame] | 1234 | static int bfin_spi_destroy_queue(struct bfin_spi_master_data *drv_data) |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1235 | { |
| 1236 | int status; |
| 1237 | |
Mike Frysinger | 138f97c | 2009-04-06 19:00:50 -0700 | [diff] [blame] | 1238 | status = bfin_spi_stop_queue(drv_data); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1239 | if (status != 0) |
| 1240 | return status; |
| 1241 | |
Bhaktipriya Shridhar | 9b96f07 | 2016-07-02 14:12:00 +0530 | [diff] [blame] | 1242 | flush_work(&drv_data->pump_messages); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1243 | |
| 1244 | return 0; |
| 1245 | } |
| 1246 | |
Grant Likely | 2deff8d | 2013-02-05 13:27:35 +0000 | [diff] [blame] | 1247 | static int bfin_spi_probe(struct platform_device *pdev) |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1248 | { |
| 1249 | struct device *dev = &pdev->dev; |
| 1250 | struct bfin5xx_spi_master *platform_info; |
| 1251 | struct spi_master *master; |
Mike Frysinger | 9c0a788 | 2010-10-18 02:45:22 -0400 | [diff] [blame] | 1252 | struct bfin_spi_master_data *drv_data; |
Bryan Wu | a32c691 | 2007-12-04 23:45:15 -0800 | [diff] [blame] | 1253 | struct resource *res; |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1254 | int status = 0; |
| 1255 | |
Jingoo Han | 8074cf0 | 2013-07-30 16:58:59 +0900 | [diff] [blame] | 1256 | platform_info = dev_get_platdata(dev); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1257 | |
| 1258 | /* Allocate master with space for drv_data */ |
Mike Frysinger | 2a04513 | 2009-09-24 01:28:54 +0000 | [diff] [blame] | 1259 | master = spi_alloc_master(dev, sizeof(*drv_data)); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1260 | if (!master) { |
| 1261 | dev_err(&pdev->dev, "can not alloc spi_master\n"); |
| 1262 | return -ENOMEM; |
| 1263 | } |
Bryan Wu | 131b17d | 2007-12-04 23:45:12 -0800 | [diff] [blame] | 1264 | |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1265 | drv_data = spi_master_get_devdata(master); |
| 1266 | drv_data->master = master; |
| 1267 | drv_data->master_info = platform_info; |
| 1268 | drv_data->pdev = pdev; |
Bryan Wu | 003d922 | 2007-12-04 23:45:22 -0800 | [diff] [blame] | 1269 | drv_data->pin_req = platform_info->pin_req; |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1270 | |
David Brownell | e7db06b | 2009-06-17 16:26:04 -0700 | [diff] [blame] | 1271 | /* the spi->mode bits supported by this driver: */ |
| 1272 | master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_LSB_FIRST; |
Stephen Warren | 24778be | 2013-05-21 20:36:35 -0600 | [diff] [blame] | 1273 | master->bits_per_word_mask = SPI_BPW_MASK(8) | SPI_BPW_MASK(16); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1274 | master->bus_num = pdev->id; |
| 1275 | master->num_chipselect = platform_info->num_chipselect; |
Mike Frysinger | 138f97c | 2009-04-06 19:00:50 -0700 | [diff] [blame] | 1276 | master->cleanup = bfin_spi_cleanup; |
| 1277 | master->setup = bfin_spi_setup; |
| 1278 | master->transfer = bfin_spi_transfer; |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1279 | |
Bryan Wu | a32c691 | 2007-12-04 23:45:15 -0800 | [diff] [blame] | 1280 | /* Find and map our resources */ |
| 1281 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 1282 | if (res == NULL) { |
| 1283 | dev_err(dev, "Cannot get IORESOURCE_MEM\n"); |
| 1284 | status = -ENOENT; |
| 1285 | goto out_error_get_res; |
| 1286 | } |
| 1287 | |
Mike Frysinger | 47885ce | 2011-06-17 04:16:56 -0400 | [diff] [blame] | 1288 | drv_data->regs = ioremap(res->start, resource_size(res)); |
| 1289 | if (drv_data->regs == NULL) { |
Bryan Wu | a32c691 | 2007-12-04 23:45:15 -0800 | [diff] [blame] | 1290 | dev_err(dev, "Cannot map IO\n"); |
| 1291 | status = -ENXIO; |
| 1292 | goto out_error_ioremap; |
| 1293 | } |
| 1294 | |
Yi Li | f6a6d96 | 2009-06-03 09:46:22 +0000 | [diff] [blame] | 1295 | res = platform_get_resource(pdev, IORESOURCE_DMA, 0); |
| 1296 | if (res == NULL) { |
Bryan Wu | a32c691 | 2007-12-04 23:45:15 -0800 | [diff] [blame] | 1297 | dev_err(dev, "No DMA channel specified\n"); |
| 1298 | status = -ENOENT; |
Yi Li | f6a6d96 | 2009-06-03 09:46:22 +0000 | [diff] [blame] | 1299 | goto out_error_free_io; |
| 1300 | } |
| 1301 | drv_data->dma_channel = res->start; |
| 1302 | |
| 1303 | drv_data->spi_irq = platform_get_irq(pdev, 0); |
| 1304 | if (drv_data->spi_irq < 0) { |
| 1305 | dev_err(dev, "No spi pio irq specified\n"); |
| 1306 | status = -ENOENT; |
| 1307 | goto out_error_free_io; |
Bryan Wu | a32c691 | 2007-12-04 23:45:15 -0800 | [diff] [blame] | 1308 | } |
| 1309 | |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1310 | /* Initial and start queue */ |
Mike Frysinger | 138f97c | 2009-04-06 19:00:50 -0700 | [diff] [blame] | 1311 | status = bfin_spi_init_queue(drv_data); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1312 | if (status != 0) { |
Bryan Wu | a32c691 | 2007-12-04 23:45:15 -0800 | [diff] [blame] | 1313 | dev_err(dev, "problem initializing queue\n"); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1314 | goto out_error_queue_alloc; |
| 1315 | } |
Bryan Wu | a32c691 | 2007-12-04 23:45:15 -0800 | [diff] [blame] | 1316 | |
Mike Frysinger | 138f97c | 2009-04-06 19:00:50 -0700 | [diff] [blame] | 1317 | status = bfin_spi_start_queue(drv_data); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1318 | if (status != 0) { |
Bryan Wu | a32c691 | 2007-12-04 23:45:15 -0800 | [diff] [blame] | 1319 | dev_err(dev, "problem starting queue\n"); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1320 | goto out_error_queue_alloc; |
| 1321 | } |
| 1322 | |
Vitja Makarov | f9e522c | 2008-04-08 17:41:57 -0700 | [diff] [blame] | 1323 | status = peripheral_request_list(drv_data->pin_req, DRV_NAME); |
| 1324 | if (status != 0) { |
| 1325 | dev_err(&pdev->dev, ": Requesting Peripherals failed\n"); |
| 1326 | goto out_error_queue_alloc; |
| 1327 | } |
| 1328 | |
Wolfgang Muees | bb8beecd | 2009-05-22 01:11:02 +0000 | [diff] [blame] | 1329 | /* Reset SPI registers. If these registers were used by the boot loader, |
| 1330 | * the sky may fall on your head if you enable the dma controller. |
| 1331 | */ |
Mike Frysinger | 47885ce | 2011-06-17 04:16:56 -0400 | [diff] [blame] | 1332 | bfin_write(&drv_data->regs->ctl, BIT_CTL_CPHA | BIT_CTL_MASTER); |
| 1333 | bfin_write(&drv_data->regs->flg, 0xFF00); |
Wolfgang Muees | bb8beecd | 2009-05-22 01:11:02 +0000 | [diff] [blame] | 1334 | |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1335 | /* Register with the SPI framework */ |
| 1336 | platform_set_drvdata(pdev, drv_data); |
| 1337 | status = spi_register_master(master); |
| 1338 | if (status != 0) { |
Bryan Wu | a32c691 | 2007-12-04 23:45:15 -0800 | [diff] [blame] | 1339 | dev_err(dev, "problem registering spi master\n"); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1340 | goto out_error_queue_alloc; |
| 1341 | } |
Bryan Wu | a32c691 | 2007-12-04 23:45:15 -0800 | [diff] [blame] | 1342 | |
Mike Frysinger | 47885ce | 2011-06-17 04:16:56 -0400 | [diff] [blame] | 1343 | dev_info(dev, "%s, Version %s, regs@%p, dma channel@%d\n", |
| 1344 | DRV_DESC, DRV_VERSION, drv_data->regs, |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 1345 | drv_data->dma_channel); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1346 | return status; |
| 1347 | |
Michael Hennerich | cc2f81a | 2007-12-04 23:45:13 -0800 | [diff] [blame] | 1348 | out_error_queue_alloc: |
Mike Frysinger | 138f97c | 2009-04-06 19:00:50 -0700 | [diff] [blame] | 1349 | bfin_spi_destroy_queue(drv_data); |
Yi Li | f6a6d96 | 2009-06-03 09:46:22 +0000 | [diff] [blame] | 1350 | out_error_free_io: |
Mike Frysinger | 47885ce | 2011-06-17 04:16:56 -0400 | [diff] [blame] | 1351 | iounmap(drv_data->regs); |
Bryan Wu | a32c691 | 2007-12-04 23:45:15 -0800 | [diff] [blame] | 1352 | out_error_ioremap: |
| 1353 | out_error_get_res: |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1354 | spi_master_put(master); |
Michael Hennerich | cc2f81a | 2007-12-04 23:45:13 -0800 | [diff] [blame] | 1355 | |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1356 | return status; |
| 1357 | } |
| 1358 | |
| 1359 | /* stop hardware and remove the driver */ |
Grant Likely | fd4a319 | 2012-12-07 16:57:14 +0000 | [diff] [blame] | 1360 | static int bfin_spi_remove(struct platform_device *pdev) |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1361 | { |
Mike Frysinger | 9c0a788 | 2010-10-18 02:45:22 -0400 | [diff] [blame] | 1362 | struct bfin_spi_master_data *drv_data = platform_get_drvdata(pdev); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1363 | int status = 0; |
| 1364 | |
| 1365 | if (!drv_data) |
| 1366 | return 0; |
| 1367 | |
| 1368 | /* Remove the queue */ |
Mike Frysinger | 138f97c | 2009-04-06 19:00:50 -0700 | [diff] [blame] | 1369 | status = bfin_spi_destroy_queue(drv_data); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1370 | if (status != 0) |
| 1371 | return status; |
| 1372 | |
| 1373 | /* Disable the SSP at the peripheral and SOC level */ |
| 1374 | bfin_spi_disable(drv_data); |
| 1375 | |
| 1376 | /* Release DMA */ |
| 1377 | if (drv_data->master_info->enable_dma) { |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 1378 | if (dma_channel_active(drv_data->dma_channel)) |
| 1379 | free_dma(drv_data->dma_channel); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1380 | } |
| 1381 | |
Yi Li | f6a6d96 | 2009-06-03 09:46:22 +0000 | [diff] [blame] | 1382 | if (drv_data->irq_requested) { |
| 1383 | free_irq(drv_data->spi_irq, drv_data); |
| 1384 | drv_data->irq_requested = 0; |
| 1385 | } |
| 1386 | |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1387 | /* Disconnect from the SPI framework */ |
| 1388 | spi_unregister_master(drv_data->master); |
| 1389 | |
Bryan Wu | 003d922 | 2007-12-04 23:45:22 -0800 | [diff] [blame] | 1390 | peripheral_free_list(drv_data->pin_req); |
Michael Hennerich | cc2f81a | 2007-12-04 23:45:13 -0800 | [diff] [blame] | 1391 | |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1392 | return 0; |
| 1393 | } |
| 1394 | |
Jingoo Han | fbbfd68 | 2013-09-09 17:55:32 +0900 | [diff] [blame] | 1395 | #ifdef CONFIG_PM_SLEEP |
| 1396 | static int bfin_spi_suspend(struct device *dev) |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1397 | { |
Jingoo Han | fbbfd68 | 2013-09-09 17:55:32 +0900 | [diff] [blame] | 1398 | struct bfin_spi_master_data *drv_data = dev_get_drvdata(dev); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1399 | int status = 0; |
| 1400 | |
Mike Frysinger | 138f97c | 2009-04-06 19:00:50 -0700 | [diff] [blame] | 1401 | status = bfin_spi_stop_queue(drv_data); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1402 | if (status != 0) |
| 1403 | return status; |
| 1404 | |
Mike Frysinger | 47885ce | 2011-06-17 04:16:56 -0400 | [diff] [blame] | 1405 | drv_data->ctrl_reg = bfin_read(&drv_data->regs->ctl); |
| 1406 | drv_data->flag_reg = bfin_read(&drv_data->regs->flg); |
Barry Song | b052fd0 | 2009-11-18 09:43:21 +0000 | [diff] [blame] | 1407 | |
| 1408 | /* |
| 1409 | * reset SPI_CTL and SPI_FLG registers |
| 1410 | */ |
Mike Frysinger | 47885ce | 2011-06-17 04:16:56 -0400 | [diff] [blame] | 1411 | bfin_write(&drv_data->regs->ctl, BIT_CTL_CPHA | BIT_CTL_MASTER); |
| 1412 | bfin_write(&drv_data->regs->flg, 0xFF00); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1413 | |
| 1414 | return 0; |
| 1415 | } |
| 1416 | |
Jingoo Han | fbbfd68 | 2013-09-09 17:55:32 +0900 | [diff] [blame] | 1417 | static int bfin_spi_resume(struct device *dev) |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1418 | { |
Jingoo Han | fbbfd68 | 2013-09-09 17:55:32 +0900 | [diff] [blame] | 1419 | struct bfin_spi_master_data *drv_data = dev_get_drvdata(dev); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1420 | int status = 0; |
| 1421 | |
Mike Frysinger | 47885ce | 2011-06-17 04:16:56 -0400 | [diff] [blame] | 1422 | bfin_write(&drv_data->regs->ctl, drv_data->ctrl_reg); |
| 1423 | bfin_write(&drv_data->regs->flg, drv_data->flag_reg); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1424 | |
| 1425 | /* Start the queue running */ |
Mike Frysinger | 138f97c | 2009-04-06 19:00:50 -0700 | [diff] [blame] | 1426 | status = bfin_spi_start_queue(drv_data); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1427 | if (status != 0) { |
Jingoo Han | fbbfd68 | 2013-09-09 17:55:32 +0900 | [diff] [blame] | 1428 | dev_err(dev, "problem starting queue (%d)\n", status); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1429 | return status; |
| 1430 | } |
| 1431 | |
| 1432 | return 0; |
| 1433 | } |
Jingoo Han | fbbfd68 | 2013-09-09 17:55:32 +0900 | [diff] [blame] | 1434 | |
| 1435 | static SIMPLE_DEV_PM_OPS(bfin_spi_pm_ops, bfin_spi_suspend, bfin_spi_resume); |
| 1436 | |
| 1437 | #define BFIN_SPI_PM_OPS (&bfin_spi_pm_ops) |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1438 | #else |
Jingoo Han | fbbfd68 | 2013-09-09 17:55:32 +0900 | [diff] [blame] | 1439 | #define BFIN_SPI_PM_OPS NULL |
| 1440 | #endif |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1441 | |
Kay Sievers | 7e38c3c | 2008-04-10 21:29:20 -0700 | [diff] [blame] | 1442 | MODULE_ALIAS("platform:bfin-spi"); |
Mike Frysinger | 138f97c | 2009-04-06 19:00:50 -0700 | [diff] [blame] | 1443 | static struct platform_driver bfin_spi_driver = { |
David Brownell | fc3ba95 | 2007-08-30 23:56:24 -0700 | [diff] [blame] | 1444 | .driver = { |
Bryan Wu | a32c691 | 2007-12-04 23:45:15 -0800 | [diff] [blame] | 1445 | .name = DRV_NAME, |
Jingoo Han | fbbfd68 | 2013-09-09 17:55:32 +0900 | [diff] [blame] | 1446 | .pm = BFIN_SPI_PM_OPS, |
Bryan Wu | 88b4036 | 2007-05-21 18:32:16 +0800 | [diff] [blame] | 1447 | }, |
Wolfram Sang | db9371b | 2013-10-08 22:35:38 +0200 | [diff] [blame] | 1448 | .probe = bfin_spi_probe, |
Grant Likely | fd4a319 | 2012-12-07 16:57:14 +0000 | [diff] [blame] | 1449 | .remove = bfin_spi_remove, |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1450 | }; |
| 1451 | |
Mike Frysinger | 138f97c | 2009-04-06 19:00:50 -0700 | [diff] [blame] | 1452 | static int __init bfin_spi_init(void) |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1453 | { |
Wolfram Sang | db9371b | 2013-10-08 22:35:38 +0200 | [diff] [blame] | 1454 | return platform_driver_register(&bfin_spi_driver); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1455 | } |
Michael Hennerich | 6f7c17f | 2010-07-01 14:34:10 +0000 | [diff] [blame] | 1456 | subsys_initcall(bfin_spi_init); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1457 | |
Mike Frysinger | 138f97c | 2009-04-06 19:00:50 -0700 | [diff] [blame] | 1458 | static void __exit bfin_spi_exit(void) |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1459 | { |
Mike Frysinger | 138f97c | 2009-04-06 19:00:50 -0700 | [diff] [blame] | 1460 | platform_driver_unregister(&bfin_spi_driver); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1461 | } |
Mike Frysinger | 138f97c | 2009-04-06 19:00:50 -0700 | [diff] [blame] | 1462 | module_exit(bfin_spi_exit); |