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