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