| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
#define DEBUG |
| 24 |
|
| 25 |
#include <linux/kernel.h> |
| 26 |
#include <linux/list.h> |
| 27 |
#include <linux/device.h> |
| 28 |
#include <linux/completion.h> |
| 29 |
#include <linux/sched.h> |
| 30 |
#include <linux/delay.h> |
| 31 |
#include <linux/slab.h> |
| 32 |
|
| 33 |
#include <linux/io.h> |
| 34 |
|
| 35 |
#include <dahdi/kernel.h> |
| 36 |
|
| 37 |
#include "wcxb_spi.h" |
| 38 |
|
| 39 |
#define BBIT(n) (1UL << (31UL - (n))) |
| 40 |
|
| 41 |
#define SPISRR 0x40 |
| 42 |
#define SPISRR_RESET 0x0000000a |
| 43 |
|
| 44 |
#define SPICR 0x60 |
| 45 |
#define SPICR_LSB_FIRST BBIT(22) |
| 46 |
#define SPICR_MASTER_INHIBIT BBIT(23) |
| 47 |
#define SPICR_SLAVE_SELECT BBIT(24) |
| 48 |
#define SPICR_RX_FIFO_RESET BBIT(25) |
| 49 |
#define SPICR_TX_FIFO_RESET BBIT(26) |
| 50 |
#define SPICR_CPHA BBIT(27) |
| 51 |
#define SPICR_CPOL BBIT(28) |
| 52 |
#define SPICR_MASTER BBIT(29) |
| 53 |
#define SPICR_SPE BBIT(30) |
| 54 |
#define SPICR_LOOP BBIT(31) |
| 55 |
|
| 56 |
#define SPICR_START_TRANSFER (SPICR_CPHA | SPICR_CPOL | \ |
| 57 |
SPICR_MASTER | SPICR_SPE) |
| 58 |
#define SPICR_READY_TRANSFER (SPICR_MASTER_INHIBIT | SPICR_START_TRANSFER) |
| 59 |
|
| 60 |
#define SPISR 0x64 |
| 61 |
#define SPISR_SLAVE_MODE_SEL BBIT(26) |
| 62 |
#define SPISR_MODF BBIT(27) |
| 63 |
#define SPISR_TX_FULL BBIT(28) |
| 64 |
#define SPISR_TX_EMPTY BBIT(29) |
| 65 |
#define SPISR_RX_FULL BBIT(30) |
| 66 |
#define SPISR_RX_EMPTY BBIT(31) |
| 67 |
|
| 68 |
#define SPIDTR 0x68 |
| 69 |
#define SPIDRR 0x6c |
| 70 |
|
| 71 |
#define SPISSR 0x70 |
| 72 |
|
| 73 |
#undef SUCCESS |
| 74 |
#define SUCCESS 0 |
| 75 |
|
| 76 |
struct wcxb_spi_master { |
| 77 |
struct device *parent; |
| 78 |
struct list_head message_queue; |
| 79 |
spinlock_t lock; |
| 80 |
void __iomem *base; |
| 81 |
struct wcxb_spi_message *cur_msg; |
| 82 |
struct wcxb_spi_transfer *cur_transfer; |
| 83 |
u16 bytes_left; |
| 84 |
u16 bytes_in_fifo; |
| 85 |
const u8 *cur_tx_byte; |
| 86 |
u8 *cur_rx_byte; |
| 87 |
u16 auto_cs:1; |
| 88 |
}; |
| 89 |
|
| 90 |
static inline void _wcxb_assert_chip_select(struct wcxb_spi_master *master, |
| 91 |
unsigned int cs) |
| 92 |
{ |
| 93 |
const int cs_mask = ~(1UL << cs); |
| 94 |
iowrite32be(cs_mask, master->base + SPISSR); |
| 95 |
ioread32be(master->base + SPISSR); |
| 96 |
} |
| 97 |
|
| 98 |
static inline void _wcxb_clear_chip_select(struct wcxb_spi_master *master) |
| 99 |
{ |
| 100 |
iowrite32be(~(0), master->base + SPISSR); |
| 101 |
ioread32(master->base + SPISSR); |
| 102 |
} |
| 103 |
|
| 104 |
static inline void wcxb_spi_reset_controller(struct wcxb_spi_master *master) |
| 105 |
{ |
| 106 |
u32 spicr = SPICR_READY_TRANSFER; |
| 107 |
spicr |= (master->auto_cs) ? 0 : SPICR_SLAVE_SELECT; |
| 108 |
iowrite32be(SPISRR_RESET, master->base + SPISRR); |
| 109 |
iowrite32be(spicr, master->base + SPICR); |
| 110 |
iowrite32be(0xffffffff, master->base + SPISSR); |
| 111 |
} |
| 112 |
|
| 113 |
struct wcxb_spi_master *wcxb_spi_master_create(struct device *parent, |
| 114 |
void __iomem *membase, |
| 115 |
bool auto_cs) |
| 116 |
{ |
| 117 |
struct wcxb_spi_master *master = NULL; |
| 118 |
master = kzalloc(sizeof(struct wcxb_spi_master), GFP_KERNEL); |
| 119 |
if (!master) |
| 120 |
goto error_exit; |
| 121 |
|
| 122 |
spin_lock_init(&master->lock); |
| 123 |
INIT_LIST_HEAD(&master->message_queue); |
| 124 |
master->base = membase; |
| 125 |
master->parent = parent; |
| 126 |
|
| 127 |
master->auto_cs = (auto_cs) ? 1 : 0; |
| 128 |
wcxb_spi_reset_controller(master); |
| 129 |
return master; |
| 130 |
|
| 131 |
error_exit: |
| 132 |
kfree(master); |
| 133 |
return NULL; |
| 134 |
} |
| 135 |
|
| 136 |
void wcxb_spi_master_destroy(struct wcxb_spi_master *master) |
| 137 |
{ |
| 138 |
struct wcxb_spi_message *m; |
| 139 |
if (!master) |
| 140 |
return; |
| 141 |
while (!list_empty(&master->message_queue)) { |
| 142 |
m = list_first_entry(&master->message_queue, |
| 143 |
struct wcxb_spi_message, node); |
| 144 |
list_del(&m->node); |
| 145 |
if (m->complete) |
| 146 |
m->complete(m->arg); |
| 147 |
} |
| 148 |
kfree(master); |
| 149 |
return; |
| 150 |
} |
| 151 |
|
| 152 |
static inline bool is_txfifo_empty(const struct wcxb_spi_master *master) |
| 153 |
{ |
| 154 |
return ((ioread32(master->base + SPISR) & |
| 155 |
cpu_to_be32(SPISR_TX_EMPTY)) > 0); |
| 156 |
} |
| 157 |
|
| 158 |
static const u8 DUMMY_TX = 0xff; |
| 159 |
static u8 DUMMY_RX; |
| 160 |
|
| 161 |
static void _wcxb_spi_transfer_to_fifo(struct wcxb_spi_master *master) |
| 162 |
{ |
| 163 |
const unsigned int FIFO_SIZE = 16; |
| 164 |
u32 spicr; |
| 165 |
while (master->bytes_left && master->bytes_in_fifo < FIFO_SIZE) { |
| 166 |
iowrite32be(*master->cur_tx_byte, master->base + SPIDTR); |
| 167 |
master->bytes_in_fifo++; |
| 168 |
master->bytes_left--; |
| 169 |
if (&DUMMY_TX != master->cur_tx_byte) |
| 170 |
master->cur_tx_byte++; |
| 171 |
} |
| 172 |
|
| 173 |
spicr = (master->auto_cs) ? SPICR_START_TRANSFER : |
| 174 |
SPICR_START_TRANSFER | SPICR_SLAVE_SELECT; |
| 175 |
iowrite32be(spicr, master->base + SPICR); |
| 176 |
} |
| 177 |
|
| 178 |
static void _wcxb_spi_transfer_from_fifo(struct wcxb_spi_master *master) |
| 179 |
{ |
| 180 |
u32 spicr; |
| 181 |
while (master->bytes_in_fifo) { |
| 182 |
*master->cur_rx_byte = ioread32be(master->base + SPIDRR); |
| 183 |
if (&DUMMY_RX != master->cur_rx_byte) |
| 184 |
master->cur_rx_byte++; |
| 185 |
--master->bytes_in_fifo; |
| 186 |
} |
| 187 |
|
| 188 |
spicr = SPICR_START_TRANSFER; |
| 189 |
spicr |= (master->auto_cs) ? 0 : SPICR_SLAVE_SELECT; |
| 190 |
iowrite32be(spicr | SPICR_MASTER_INHIBIT, master->base + SPICR); |
| 191 |
} |
| 192 |
|
| 193 |
static void _wcxb_spi_start_transfer(struct wcxb_spi_master *master, |
| 194 |
struct wcxb_spi_transfer *t) |
| 195 |
{ |
| 196 |
#ifdef DEBUG |
| 197 |
if (!t || !master || (!t->tx_buf && !t->rx_buf) || |
| 198 |
master->cur_transfer) { |
| 199 |
WARN_ON(1); |
| 200 |
return; |
| 201 |
} |
| 202 |
#endif |
| 203 |
|
| 204 |
master->cur_transfer = t; |
| 205 |
master->bytes_left = t->len; |
| 206 |
master->cur_tx_byte = (t->tx_buf) ?: &DUMMY_TX; |
| 207 |
master->cur_rx_byte = (t->rx_buf) ?: &DUMMY_RX; |
| 208 |
|
| 209 |
_wcxb_spi_transfer_to_fifo(master); |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
static int _wcxb_spi_start_message(struct wcxb_spi_master *master, |
| 219 |
struct wcxb_spi_message *message) |
| 220 |
{ |
| 221 |
struct wcxb_spi_transfer *t; |
| 222 |
|
| 223 |
if (master->cur_msg) { |
| 224 |
|
| 225 |
list_add_tail(&message->node, &master->message_queue); |
| 226 |
return 0; |
| 227 |
} |
| 228 |
|
| 229 |
if (!message->spi) { |
| 230 |
dev_dbg(master->parent, |
| 231 |
"Queueing message without SPI device specified?\n"); |
| 232 |
return -EINVAL; |
| 233 |
}; |
| 234 |
|
| 235 |
master->cur_msg = message; |
| 236 |
|
| 237 |
_wcxb_assert_chip_select(master, message->spi->chip_select); |
| 238 |
t = list_first_entry(&message->transfers, |
| 239 |
struct wcxb_spi_transfer, node); |
| 240 |
_wcxb_spi_start_transfer(master, t); |
| 241 |
|
| 242 |
return 0; |
| 243 |
} |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
static void _wcxb_spi_complete_cur_msg(struct wcxb_spi_master *master) |
| 256 |
{ |
| 257 |
struct wcxb_spi_message *message; |
| 258 |
if (!master->cur_msg) |
| 259 |
return; |
| 260 |
message = master->cur_msg; |
| 261 |
message->status = SUCCESS; |
| 262 |
_wcxb_clear_chip_select(master); |
| 263 |
master->cur_msg = NULL; |
| 264 |
if (!list_empty(&master->message_queue)) { |
| 265 |
message = list_first_entry(&master->message_queue, |
| 266 |
struct wcxb_spi_message, node); |
| 267 |
list_del(&message->node); |
| 268 |
_wcxb_spi_start_message(master, message); |
| 269 |
} |
| 270 |
return; |
| 271 |
} |
| 272 |
|
| 273 |
static inline bool |
| 274 |
_wcxb_spi_is_last_transfer(const struct wcxb_spi_transfer *t, |
| 275 |
const struct wcxb_spi_message *message) |
| 276 |
{ |
| 277 |
return t->node.next == &message->transfers; |
| 278 |
} |
| 279 |
|
| 280 |
static inline struct wcxb_spi_transfer * |
| 281 |
_wcxb_spi_next_transfer(struct wcxb_spi_transfer *t) |
| 282 |
{ |
| 283 |
return list_entry(t->node.next, struct wcxb_spi_transfer, node); |
| 284 |
} |
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
void wcxb_spi_handle_interrupt(struct wcxb_spi_master *master) |
| 294 |
{ |
| 295 |
struct wcxb_spi_message *msg; |
| 296 |
struct wcxb_spi_transfer *t; |
| 297 |
void (*complete)(void *arg) = NULL; |
| 298 |
unsigned long flags; |
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
spin_lock_irqsave(&master->lock, flags); |
| 303 |
|
| 304 |
t = master->cur_transfer; |
| 305 |
msg = master->cur_msg; |
| 306 |
|
| 307 |
if (!msg || !is_txfifo_empty(master)) |
| 308 |
goto done; |
| 309 |
|
| 310 |
#ifdef DEBUG |
| 311 |
if (!t) { |
| 312 |
dev_dbg(master->parent, |
| 313 |
"No current transfer in %s\n", __func__); |
| 314 |
goto done; |
| 315 |
} |
| 316 |
#endif |
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
_wcxb_spi_transfer_from_fifo(master); |
| 321 |
if (master->bytes_left) { |
| 322 |
|
| 323 |
_wcxb_spi_transfer_to_fifo(master); |
| 324 |
goto done; |
| 325 |
} |
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
master->cur_transfer = NULL; |
| 330 |
|
| 331 |
if (_wcxb_spi_is_last_transfer(t, msg)) { |
| 332 |
complete = msg->complete; |
| 333 |
_wcxb_spi_complete_cur_msg(master); |
| 334 |
} else { |
| 335 |
t = _wcxb_spi_next_transfer(t); |
| 336 |
_wcxb_spi_start_transfer(master, t); |
| 337 |
} |
| 338 |
done: |
| 339 |
spin_unlock_irqrestore(&master->lock, flags); |
| 340 |
|
| 341 |
if (complete) |
| 342 |
complete(msg->arg); |
| 343 |
return; |
| 344 |
} |
| 345 |
|
| 346 |
int wcxb_spi_async(struct wcxb_spi_device *spi, |
| 347 |
struct wcxb_spi_message *message) |
| 348 |
{ |
| 349 |
int res; |
| 350 |
unsigned long flags; |
| 351 |
WARN_ON(!spi || !message || !spi->master); |
| 352 |
|
| 353 |
if (list_empty(&message->transfers)) { |
| 354 |
|
| 355 |
if (message->complete) |
| 356 |
message->complete(message->arg); |
| 357 |
message->status = -EINVAL; |
| 358 |
return 0; |
| 359 |
} |
| 360 |
message->status = -EINPROGRESS; |
| 361 |
message->spi = spi; |
| 362 |
spin_lock_irqsave(&spi->master->lock, flags); |
| 363 |
res = _wcxb_spi_start_message(spi->master, message); |
| 364 |
spin_unlock_irqrestore(&spi->master->lock, flags); |
| 365 |
return res; |
| 366 |
} |
| 367 |
|
| 368 |
static void wcxb_spi_complete_message(void *arg) |
| 369 |
{ |
| 370 |
complete((struct completion *)arg); |
| 371 |
} |
| 372 |
|
| 373 |
int wcxb_spi_sync(struct wcxb_spi_device *spi, struct wcxb_spi_message *message) |
| 374 |
{ |
| 375 |
DECLARE_COMPLETION_ONSTACK(done); |
| 376 |
WARN_ON(!spi || !spi->master); |
| 377 |
message->complete = wcxb_spi_complete_message; |
| 378 |
message->arg = &done; |
| 379 |
wcxb_spi_async(spi, message); |
| 380 |
|
| 381 |
|
| 382 |
while (!try_wait_for_completion(&done)) { |
| 383 |
wcxb_spi_handle_interrupt(spi->master); |
| 384 |
cpu_relax(); |
| 385 |
} |
| 386 |
return message->status; |
| 387 |
} |