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