| 1 |
#include <linux/kernel.h> |
| 2 |
#include <linux/module.h> |
| 3 |
#include <linux/delay.h> |
| 4 |
#include <linux/proc_fs.h> |
| 5 |
#include <linux/interrupt.h> |
| 6 |
#include <linux/firmware.h> |
| 7 |
#include <linux/slab.h> |
| 8 |
#include <asm/io.h> |
| 9 |
#include <asm/irq.h> |
| 10 |
#include <asm/blackfin.h> |
| 11 |
#include <asm/dma.h> |
| 12 |
#include <asm/cacheflush.h> |
| 13 |
#include <asm/uaccess.h> |
| 14 |
#include <dahdi/kernel.h> |
| 15 |
#include "mmapbus.h" |
| 16 |
#include "xbus-core.h" |
| 17 |
#include "dahdi_debug.h" |
| 18 |
#include "xdefs.h" |
| 19 |
#include "xproto.h" |
| 20 |
#include "xframe_queue.h" |
| 21 |
|
| 22 |
|
| 23 |
typedef char |
| 24 |
sizeof_xframe_t_should_be_divisible_by_4[((sizeof(xframe_t) % 4) == |
| 25 |
0) * 2 - 1]; |
| 26 |
|
| 27 |
#define ssync() __builtin_bfin_ssync() |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#define DATA PG8 |
| 32 |
#define NCONFIG PG9 |
| 33 |
#define CONF_DONE PG10 |
| 34 |
#define DCLK PG11 |
| 35 |
#define NSTATUS PG12 |
| 36 |
|
| 37 |
#ifdef DEBUG_VIA_GPIO |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
#define DEBUG_GPIO1 CONF_DONE |
| 43 |
#define DEBUG_GPIO2 NSTATUS |
| 44 |
|
| 45 |
static int rx_intr_counter; |
| 46 |
#endif |
| 47 |
|
| 48 |
#define FPGA_RX_IRQ IRQ_PF7 |
| 49 |
#define FPGA_TX_IRQ IRQ_PF4 |
| 50 |
#define FPGA_BASE_ADDR ((volatile char __iomem *)0x203FA800) |
| 51 |
|
| 52 |
#define END_OF_FRAME 0x0001 |
| 53 |
#define GET_LEN 0x0002 |
| 54 |
#define START_RD_BURST 0x0008 |
| 55 |
|
| 56 |
#define AS_BF_MODE 0x0010 |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
#define EC_BF_MODE 0x0020 |
| 62 |
|
| 63 |
#define NO_BF_MODE 0x0040 |
| 64 |
#define SET_XA_DIR 0x0080 |
| 65 |
#define GET_XPD_STS 0x0100 |
| 66 |
#define GET_CHECKSUM 0x0200 |
| 67 |
|
| 68 |
static const char rcsid[] = "$Id$"; |
| 69 |
|
| 70 |
static DEF_PARM(int, debug, 0, 0644, "Print DBG statements"); |
| 71 |
static DEF_PARM(int, notxrx, 0, 0644, "Don't send or receive anything"); |
| 72 |
|
| 73 |
struct counter { |
| 74 |
long intr_min, intr_max; |
| 75 |
long intr_avg, intr_count; |
| 76 |
}; |
| 77 |
|
| 78 |
static xbus_t *global_xbus; |
| 79 |
static bool tx_ready = 1; |
| 80 |
static DEFINE_SPINLOCK(tx_ready_lock); |
| 81 |
static struct xframe_queue txpool; |
| 82 |
static unsigned int pcm_in_pool_count; |
| 83 |
static bool disconnecting; |
| 84 |
static struct kmem_cache *xframe_cache; |
| 85 |
static struct counter tx_counter, rx_counter; |
| 86 |
static unsigned long pcm_dropped; |
| 87 |
|
| 88 |
static void print_buffer(const char *msg, const char *buf, int len) |
| 89 |
{ |
| 90 |
int i; |
| 91 |
printk(KERN_ERR "%s", msg); |
| 92 |
for (i = 0; i < len; i++) |
| 93 |
printk("%02X ", (unsigned char)buf[i]); |
| 94 |
printk("\n"); |
| 95 |
} |
| 96 |
|
| 97 |
static void update_counter(struct counter *c, struct timeval *tv1) |
| 98 |
{ |
| 99 |
struct timeval tv2; |
| 100 |
long diff; |
| 101 |
do_gettimeofday(&tv2); |
| 102 |
diff = usec_diff(&tv2, tv1); |
| 103 |
if (c->intr_min > diff) |
| 104 |
c->intr_min = diff; |
| 105 |
if (c->intr_max < diff) |
| 106 |
c->intr_max = diff; |
| 107 |
c->intr_avg = |
| 108 |
(c->intr_avg * c->intr_count + diff) / (c->intr_count + 1); |
| 109 |
c->intr_count++; |
| 110 |
} |
| 111 |
|
| 112 |
static irqreturn_t xpp_mmap_rx_irq(int irq, void *dev_id) |
| 113 |
{ |
| 114 |
unsigned short rxcnt; |
| 115 |
xbus_t *xbus; |
| 116 |
xframe_t *xframe; |
| 117 |
__u8 *buf; |
| 118 |
bool in_use = 0; |
| 119 |
struct timeval tv1; |
| 120 |
|
| 121 |
do_gettimeofday(&tv1); |
| 122 |
if (unlikely(disconnecting)) |
| 123 |
return IRQ_HANDLED; |
| 124 |
|
| 125 |
xbus = xbus_num(global_xbus->num); |
| 126 |
BUG_ON(!xbus); |
| 127 |
if (!XBUS_GET(xbus)) { |
| 128 |
if (printk_ratelimit()) |
| 129 |
XBUS_ERR(xbus, "Dropping packet. Is shutting down.\n"); |
| 130 |
goto out; |
| 131 |
} |
| 132 |
in_use = 1; |
| 133 |
|
| 134 |
outw(GET_LEN, FPGA_BASE_ADDR + 4); |
| 135 |
rxcnt = inw(FPGA_BASE_ADDR); |
| 136 |
if (rxcnt < 3) { |
| 137 |
if (printk_ratelimit()) |
| 138 |
NOTICE("Got %d bytes\n", rxcnt); |
| 139 |
goto out; |
| 140 |
} |
| 141 |
if (rxcnt >= XFRAME_DATASIZE) { |
| 142 |
if (printk_ratelimit()) |
| 143 |
ERR("Bad rxcnt=%d\n", rxcnt); |
| 144 |
goto out; |
| 145 |
} |
| 146 |
|
| 147 |
xframe = ALLOC_RECV_XFRAME(xbus); |
| 148 |
if (!xframe) { |
| 149 |
if (printk_ratelimit()) |
| 150 |
XBUS_ERR(xbus, "Empty receive_pool\n"); |
| 151 |
goto out; |
| 152 |
} |
| 153 |
buf = xframe->packets; |
| 154 |
atomic_set(&xframe->frame_len, rxcnt); |
| 155 |
do_gettimeofday(&xframe->tv_received); |
| 156 |
#ifdef DEBUG_VIA_GPIO |
| 157 |
if (rx_intr_counter & 1) |
| 158 |
bfin_write_PORTGIO_SET(DEBUG_GPIO1); |
| 159 |
else |
| 160 |
bfin_write_PORTGIO_CLEAR(DEBUG_GPIO1); |
| 161 |
#endif |
| 162 |
outw(START_RD_BURST, FPGA_BASE_ADDR + 4); |
| 163 |
insw((unsigned long)FPGA_BASE_ADDR, buf, rxcnt / 2); |
| 164 |
#if 0 |
| 165 |
for (count = 0; count < rxcnt; count += 2) { |
| 166 |
unsigned short v = inw(FPGA_BASE_ADDR); |
| 167 |
buf[count] = v & 0xFF; |
| 168 |
buf[count + 1] = v >> 8; |
| 169 |
} |
| 170 |
#endif |
| 171 |
if (rxcnt & 1) |
| 172 |
buf[rxcnt - 1] = inw(FPGA_BASE_ADDR); |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
if (((buf[0] | (buf[1] << 8)) & 0x3FF) > rxcnt) { |
| 178 |
if (printk_ratelimit()) { |
| 179 |
ERR("Packet len=%d, frame len=%d\n", |
| 180 |
(buf[0] | (buf[1] << 8)) & 0x3FF, rxcnt); |
| 181 |
print_buffer("16 bytes of packet: ", buf, 16); |
| 182 |
} |
| 183 |
goto free; |
| 184 |
} |
| 185 |
if (debug && buf[2] != 0x12) |
| 186 |
print_buffer("Received: ", buf, rxcnt); |
| 187 |
if (!notxrx) { |
| 188 |
xbus_receive_xframe(xbus, xframe); |
| 189 |
#ifdef DEBUG_VIA_GPIO |
| 190 |
if (rx_intr_counter & 1) |
| 191 |
bfin_write_PORTGIO_SET(DEBUG_GPIO2); |
| 192 |
else |
| 193 |
bfin_write_PORTGIO_CLEAR(DEBUG_GPIO2); |
| 194 |
#endif |
| 195 |
goto out; |
| 196 |
} |
| 197 |
free: |
| 198 |
FREE_RECV_XFRAME(xbus, xframe); |
| 199 |
out: |
| 200 |
if (in_use) |
| 201 |
XBUS_PUT(xbus); |
| 202 |
#ifdef DEBUG_VIA_GPIO |
| 203 |
rx_intr_counter++; |
| 204 |
#endif |
| 205 |
update_counter(&rx_counter, &tv1); |
| 206 |
return IRQ_HANDLED; |
| 207 |
} |
| 208 |
|
| 209 |
static void send_buffer(unsigned char *buf, unsigned long len) |
| 210 |
{ |
| 211 |
if (debug && len >= 3 && buf[2] != 0x11) |
| 212 |
print_buffer("Sent: ", buf, len); |
| 213 |
outsw((unsigned long)FPGA_BASE_ADDR, buf, len / 2); |
| 214 |
if (len & 1) |
| 215 |
outw((unsigned short)buf[len - 1], FPGA_BASE_ADDR); |
| 216 |
outw(END_OF_FRAME, FPGA_BASE_ADDR + 4); |
| 217 |
} |
| 218 |
|
| 219 |
static irqreturn_t xpp_mmap_tx_irq(int irq, void *dev_id) |
| 220 |
{ |
| 221 |
unsigned long flags; |
| 222 |
xbus_t *xbus; |
| 223 |
xframe_t *xframe; |
| 224 |
struct timeval tv1; |
| 225 |
|
| 226 |
do_gettimeofday(&tv1); |
| 227 |
if (unlikely(disconnecting)) { |
| 228 |
update_counter(&tx_counter, &tv1); |
| 229 |
return IRQ_HANDLED; |
| 230 |
} |
| 231 |
spin_lock_irqsave(&tx_ready_lock, flags); |
| 232 |
xframe = xframe_dequeue(&txpool); |
| 233 |
if (!xframe) { |
| 234 |
tx_ready = 1; |
| 235 |
spin_unlock_irqrestore(&tx_ready_lock, flags); |
| 236 |
update_counter(&tx_counter, &tv1); |
| 237 |
return IRQ_HANDLED; |
| 238 |
} |
| 239 |
tx_ready = 0; |
| 240 |
if (XPACKET_IS_PCM((xpacket_t *)xframe->packets)) |
| 241 |
pcm_in_pool_count--; |
| 242 |
spin_unlock_irqrestore(&tx_ready_lock, flags); |
| 243 |
xbus = (xbus_t *)xframe->priv; |
| 244 |
BUG_ON(!xbus); |
| 245 |
xbus = xbus_num(xbus->num); |
| 246 |
BUG_ON(!xbus); |
| 247 |
send_buffer(xframe->packets, XFRAME_LEN(xframe)); |
| 248 |
FREE_SEND_XFRAME(xbus, xframe); |
| 249 |
update_counter(&tx_counter, &tv1); |
| 250 |
return IRQ_HANDLED; |
| 251 |
} |
| 252 |
|
| 253 |
static int xframe_send_common(xbus_t *xbus, xframe_t *xframe, bool pcm) |
| 254 |
{ |
| 255 |
unsigned long flags; |
| 256 |
if (unlikely(disconnecting)) { |
| 257 |
FREE_SEND_XFRAME(xbus, xframe); |
| 258 |
return -ENODEV; |
| 259 |
} |
| 260 |
if (unlikely(notxrx)) { |
| 261 |
FREE_SEND_XFRAME(xbus, xframe); |
| 262 |
return 0; |
| 263 |
} |
| 264 |
spin_lock_irqsave(&tx_ready_lock, flags); |
| 265 |
if (tx_ready) { |
| 266 |
tx_ready = 0; |
| 267 |
send_buffer(xframe->packets, XFRAME_LEN(xframe)); |
| 268 |
spin_unlock_irqrestore(&tx_ready_lock, flags); |
| 269 |
FREE_SEND_XFRAME(xbus, xframe); |
| 270 |
} else { |
| 271 |
if (pcm && pcm_in_pool_count >= 1) { |
| 272 |
static int rate_limit; |
| 273 |
if ((rate_limit++ % 1000) == 0) |
| 274 |
XBUS_ERR(xbus, |
| 275 |
"Dropped PCM xframe " |
| 276 |
"(pcm_in_pool_count=%d).\n", |
| 277 |
pcm_in_pool_count); |
| 278 |
FREE_SEND_XFRAME(xbus, xframe); |
| 279 |
pcm_dropped++; |
| 280 |
} else { |
| 281 |
if (!xframe_enqueue(&txpool, xframe)) { |
| 282 |
static int rate_limit; |
| 283 |
spin_unlock_irqrestore(&tx_ready_lock, flags); |
| 284 |
if ((rate_limit++ % 1000) == 0) |
| 285 |
XBUS_ERR(xbus, |
| 286 |
"Dropped xframe. " |
| 287 |
"Cannot enqueue.\n"); |
| 288 |
FREE_SEND_XFRAME(xbus, xframe); |
| 289 |
return -E2BIG; |
| 290 |
} |
| 291 |
if (pcm) |
| 292 |
pcm_in_pool_count++; |
| 293 |
} |
| 294 |
spin_unlock_irqrestore(&tx_ready_lock, flags); |
| 295 |
} |
| 296 |
return 0; |
| 297 |
} |
| 298 |
|
| 299 |
static xframe_t *alloc_xframe(xbus_t *xbus, gfp_t gfp_flags) |
| 300 |
{ |
| 301 |
xframe_t *xframe = kmem_cache_alloc(xframe_cache, gfp_flags); |
| 302 |
if (!xframe) { |
| 303 |
static int rate_limit; |
| 304 |
if ((rate_limit++ % 1000) < 5) |
| 305 |
XBUS_ERR(xbus, "frame allocation failed (%d)\n", |
| 306 |
rate_limit); |
| 307 |
return NULL; |
| 308 |
} |
| 309 |
xframe_init(xbus, xframe, ((__u8 *)xframe) + sizeof(xframe_t), |
| 310 |
XFRAME_DATASIZE, xbus); |
| 311 |
return xframe; |
| 312 |
} |
| 313 |
|
| 314 |
static void free_xframe(xbus_t *xbus, xframe_t *xframe) |
| 315 |
{ |
| 316 |
memset(xframe, 0, sizeof(*xframe)); |
| 317 |
kmem_cache_free(xframe_cache, xframe); |
| 318 |
} |
| 319 |
|
| 320 |
static int xframe_send_pcm(xbus_t *xbus, xframe_t *xframe) |
| 321 |
{ |
| 322 |
return xframe_send_common(xbus, xframe, 1); |
| 323 |
} |
| 324 |
|
| 325 |
static int xframe_send_cmd(xbus_t *xbus, xframe_t *xframe) |
| 326 |
{ |
| 327 |
return xframe_send_common(xbus, xframe, 0); |
| 328 |
} |
| 329 |
|
| 330 |
static struct xbus_ops xmmap_ops = { |
| 331 |
.xframe_send_pcm = xframe_send_pcm, |
| 332 |
.xframe_send_cmd = xframe_send_cmd, |
| 333 |
.alloc_xframe = alloc_xframe, |
| 334 |
.free_xframe = free_xframe, |
| 335 |
}; |
| 336 |
|
| 337 |
static int fill_proc_queue(char *p, struct xframe_queue *q) |
| 338 |
{ |
| 339 |
int len; |
| 340 |
|
| 341 |
len = sprintf(p, |
| 342 |
"%-15s: counts %3d, %3d, %3d worst %3d, overflows %3d " |
| 343 |
"worst_lag %02ld.%ld ms\n", |
| 344 |
q->name, q->steady_state_count, q->count, q->max_count, |
| 345 |
q->worst_count, q->overflows, q->worst_lag_usec / 1000, |
| 346 |
q->worst_lag_usec % 1000); |
| 347 |
xframe_queue_clearstats(q); |
| 348 |
return len; |
| 349 |
} |
| 350 |
|
| 351 |
static int fill_proc_counter(char *p, struct counter *c) |
| 352 |
{ |
| 353 |
return sprintf(p, "min=%ld\nmax=%ld\navg=%ld\ncount=%ld\n", c->intr_min, |
| 354 |
c->intr_max, c->intr_avg, c->intr_count); |
| 355 |
} |
| 356 |
|
| 357 |
static int xpp_mmap_proc_read(char *page, char **start, off_t off, int count, |
| 358 |
int *eof, void *data) |
| 359 |
{ |
| 360 |
int len = 0; |
| 361 |
len += fill_proc_queue(page + len, &txpool); |
| 362 |
len += sprintf(page + len, "pcm_in_pool_count=%d\n", pcm_in_pool_count); |
| 363 |
len += sprintf(page + len, "pcm_dropped=%lu\n", pcm_dropped); |
| 364 |
len += sprintf(page + len, "\nrx_counter:\n"); |
| 365 |
len += fill_proc_counter(page + len, &rx_counter); |
| 366 |
len += sprintf(page + len, "\ntx_counter:\n"); |
| 367 |
len += fill_proc_counter(page + len, &tx_counter); |
| 368 |
if (len <= off + count) { |
| 369 |
*eof = 1; |
| 370 |
tx_counter.intr_min = rx_counter.intr_min = INT_MAX; |
| 371 |
tx_counter.intr_max = rx_counter.intr_max = 0; |
| 372 |
tx_counter.intr_avg = rx_counter.intr_avg = 0; |
| 373 |
tx_counter.intr_count = rx_counter.intr_count = 0; |
| 374 |
} |
| 375 |
*start = page + off; |
| 376 |
len -= off; |
| 377 |
if (len > count) |
| 378 |
len = count; |
| 379 |
if (len < 0) |
| 380 |
len = 0; |
| 381 |
return len; |
| 382 |
} |
| 383 |
|
| 384 |
static int xpp_mmap_proc_write(struct file *file, const char __user *buffer, |
| 385 |
unsigned long count, void *data) |
| 386 |
{ |
| 387 |
int i = 0; |
| 388 |
char *txchunk, *p, *endp; |
| 389 |
|
| 390 |
if (count >= XFRAME_DATASIZE * 3 + 10) |
| 391 |
return -EINVAL; |
| 392 |
p = txchunk = kmalloc(count + 1, GFP_KERNEL); |
| 393 |
if (copy_from_user(txchunk, buffer, count)) { |
| 394 |
count = -EFAULT; |
| 395 |
goto out; |
| 396 |
} |
| 397 |
txchunk[count] = '\0'; |
| 398 |
|
| 399 |
while (*p) { |
| 400 |
unsigned long value; |
| 401 |
while (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r') |
| 402 |
p++; |
| 403 |
if (*p == '\0') |
| 404 |
break; |
| 405 |
value = simple_strtoul(p, &endp, 16); |
| 406 |
if (endp == p || value > 0xFF) { |
| 407 |
INFO("%s: Bad input\n", __func__); |
| 408 |
count = -EINVAL; |
| 409 |
goto out; |
| 410 |
} |
| 411 |
p = endp; |
| 412 |
txchunk[i++] = (char)value; |
| 413 |
} |
| 414 |
send_buffer(txchunk, i); |
| 415 |
out: |
| 416 |
kfree(txchunk); |
| 417 |
return count; |
| 418 |
} |
| 419 |
|
| 420 |
static struct mmap_driver astribank_driver = { |
| 421 |
.module = THIS_MODULE, |
| 422 |
.driver = { |
| 423 |
.name = "xpp_mmap", |
| 424 |
}, |
| 425 |
}; |
| 426 |
|
| 427 |
static struct mmap_device astribank_dev = { |
| 428 |
.name = "astribank0", |
| 429 |
.driver = &astribank_driver, |
| 430 |
}; |
| 431 |
|
| 432 |
static int __init xpp_mmap_load_fpga(u8 * data, size_t size) |
| 433 |
{ |
| 434 |
size_t i; |
| 435 |
|
| 436 |
bfin_write_PORTGIO_DIR(bfin_read_PORTGIO_DIR() | DATA | NCONFIG | DCLK); |
| 437 |
bfin_write_PORTG_FER(bfin_read_PORTG_FER() & ~(DATA | NCONFIG | DCLK)); |
| 438 |
|
| 439 |
bfin_write_PORTGIO_DIR( |
| 440 |
bfin_read_PORTGIO_DIR() & ~(CONF_DONE | NSTATUS)); |
| 441 |
bfin_write_PORTGIO_INEN( |
| 442 |
bfin_read_PORTGIO_INEN() & ~(DATA | NCONFIG | DCLK)); |
| 443 |
bfin_write_PORTGIO_INEN(bfin_read_PORTGIO_INEN() | CONF_DONE | NSTATUS); |
| 444 |
|
| 445 |
|
| 446 |
bfin_write_PORTGIO_CLEAR(NCONFIG); |
| 447 |
udelay(40); |
| 448 |
|
| 449 |
bfin_write_PORTGIO_SET(NCONFIG); |
| 450 |
udelay(40); |
| 451 |
if (!(bfin_read_PORTGIO() & NSTATUS)) |
| 452 |
return -EIO; |
| 453 |
|
| 454 |
#if 0 |
| 455 |
if (!(bfin_read_PORTGIO() & CONF_DONE)) |
| 456 |
return -EIO; |
| 457 |
#endif |
| 458 |
bfin_write_PORTGIO_CLEAR(DCLK); |
| 459 |
for (i = 0; i < size; i++) { |
| 460 |
int j; |
| 461 |
u8 __u8 = data[i]; |
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
for (j = 0; j < 8; j++) |
| 467 |
{ |
| 468 |
if (__u8 & 1) |
| 469 |
bfin_write_PORTGIO_SET(DATA); |
| 470 |
else |
| 471 |
bfin_write_PORTGIO_CLEAR(DATA); |
| 472 |
__u8 >>= 1; |
| 473 |
bfin_write_PORTGIO_SET(DCLK); |
| 474 |
bfin_write_PORTGIO_CLEAR(DCLK); |
| 475 |
} |
| 476 |
if (!(bfin_read_PORTGIO() & NSTATUS)) |
| 477 |
return -EIO; |
| 478 |
} |
| 479 |
bfin_write_PORTGIO_CLEAR(DATA); |
| 480 |
udelay(1); |
| 481 |
if (!(bfin_read_PORTGIO() & CONF_DONE)) |
| 482 |
return -EIO; |
| 483 |
#ifdef DEBUG_VIA_GPIO |
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
bfin_write_PORTGIO_DIR( |
| 491 |
bfin_read_PORTGIO_DIR() | DEBUG_GPIO1 | DEBUG_GPIO2); |
| 492 |
bfin_write_PORTG_FER(bfin_read_PORTG_FER() & |
| 493 |
~(DEBUG_GPIO1 | DEBUG_GPIO2)); |
| 494 |
bfin_write_PORTGIO_INEN(bfin_read_PORTGIO_INEN() & |
| 495 |
~(DEBUG_GPIO1 | DEBUG_GPIO2)); |
| 496 |
#endif |
| 497 |
udelay(40); |
| 498 |
return 0; |
| 499 |
} |
| 500 |
|
| 501 |
static void __exit xpp_mmap_unload_fpga(void) |
| 502 |
{ |
| 503 |
|
| 504 |
bfin_write_PORTGIO_CLEAR(NCONFIG); |
| 505 |
udelay(40); |
| 506 |
|
| 507 |
bfin_write_PORTGIO_DIR( |
| 508 |
bfin_read_PORTGIO_DIR() & ~(DATA | NCONFIG | DCLK)); |
| 509 |
|
| 510 |
bfin_write_PORTGIO_INEN( |
| 511 |
bfin_read_PORTGIO_INEN() & ~(CONF_DONE | NSTATUS)); |
| 512 |
INFO("FPGA Firmware unloaded\n"); |
| 513 |
} |
| 514 |
|
| 515 |
static int __init xpp_mmap_load_firmware(void) |
| 516 |
{ |
| 517 |
const struct firmware *fw; |
| 518 |
int ret; |
| 519 |
if ((ret = |
| 520 |
request_firmware(&fw, "astribank.bin", &astribank_dev.dev)) < 0) |
| 521 |
return ret; |
| 522 |
xpp_mmap_load_fpga(fw->data, fw->size); |
| 523 |
release_firmware(fw); |
| 524 |
return ret; |
| 525 |
} |
| 526 |
|
| 527 |
static int __init xpp_mmap_init(void) |
| 528 |
{ |
| 529 |
int ret; |
| 530 |
struct proc_dir_entry *proc_entry; |
| 531 |
|
| 532 |
if ((ret = register_mmap_bus()) < 0) |
| 533 |
goto bus_reg; |
| 534 |
if ((ret = register_mmap_driver(&astribank_driver)) < 0) |
| 535 |
goto driver_reg; |
| 536 |
if ((ret = register_mmap_device(&astribank_dev)) < 0) |
| 537 |
goto dev_reg; |
| 538 |
if ((ret = xpp_mmap_load_firmware()) < 0) { |
| 539 |
ERR("xpp_mmap_load_firmware() failed, errno=%d\n", ret); |
| 540 |
goto fail_fw; |
| 541 |
} |
| 542 |
|
| 543 |
if ((ret = |
| 544 |
request_irq(FPGA_RX_IRQ, xpp_mmap_rx_irq, IRQF_TRIGGER_RISING, |
| 545 |
"xpp_mmap_rx", NULL)) < 0) { |
| 546 |
ERR("Unable to attach to RX interrupt %d\n", FPGA_RX_IRQ); |
| 547 |
goto fail_irq_rx; |
| 548 |
} |
| 549 |
if ((ret = |
| 550 |
request_irq(FPGA_TX_IRQ, xpp_mmap_tx_irq, IRQF_TRIGGER_RISING, |
| 551 |
"xpp_mmap_tx", NULL)) < 0) { |
| 552 |
ERR("Unable to attach to TX interrupt %d\n", FPGA_TX_IRQ); |
| 553 |
goto fail_irq_tx; |
| 554 |
} |
| 555 |
if (!request_region((resource_size_t) FPGA_BASE_ADDR, 8, "xpp_mmap")) { |
| 556 |
ERR("Unable to request memory region at %p\n", FPGA_BASE_ADDR); |
| 557 |
goto fail_region; |
| 558 |
} |
| 559 |
outw(AS_BF_MODE, FPGA_BASE_ADDR + 4); |
| 560 |
|
| 561 |
xframe_cache = |
| 562 |
kmem_cache_create("xframe_cache", |
| 563 |
sizeof(xframe_t) + XFRAME_DATASIZE, 0, 0, |
| 564 |
NULL); |
| 565 |
if (!xframe_cache) { |
| 566 |
ret = -ENOMEM; |
| 567 |
goto fail_cache; |
| 568 |
} |
| 569 |
|
| 570 |
global_xbus = xbus_new(&xmmap_ops, XFRAME_DATASIZE, NULL); |
| 571 |
if (!global_xbus) { |
| 572 |
ret = -ENOMEM; |
| 573 |
goto fail_xbus; |
| 574 |
} |
| 575 |
strncpy(global_xbus->connector, "mmap", XBUS_DESCLEN); |
| 576 |
strncpy(global_xbus->label, "mmap:0", LABEL_SIZE); |
| 577 |
|
| 578 |
xframe_queue_init(&txpool, 10, 200, "mmap_txpool", global_xbus); |
| 579 |
if (! |
| 580 |
(proc_entry = |
| 581 |
create_proc_entry("xpp_mmap", 0, global_xbus->proc_xbus_dir))) { |
| 582 |
ERR("create_proc_entry() failed\n"); |
| 583 |
ret = -EINVAL; |
| 584 |
goto fail_proc; |
| 585 |
} |
| 586 |
proc_entry->write_proc = xpp_mmap_proc_write; |
| 587 |
proc_entry->read_proc = xpp_mmap_proc_read; |
| 588 |
|
| 589 |
xbus_connect(global_xbus); |
| 590 |
INFO("xpp_mmap module loaded\n"); |
| 591 |
|
| 592 |
return 0; |
| 593 |
|
| 594 |
fail_proc: |
| 595 |
xbus_disconnect(global_xbus); |
| 596 |
fail_xbus: |
| 597 |
kmem_cache_destroy(xframe_cache); |
| 598 |
fail_cache: |
| 599 |
release_region((resource_size_t) FPGA_BASE_ADDR, 8); |
| 600 |
fail_region: |
| 601 |
free_irq(FPGA_TX_IRQ, NULL); |
| 602 |
fail_irq_tx: |
| 603 |
free_irq(FPGA_RX_IRQ, NULL); |
| 604 |
fail_irq_rx: |
| 605 |
fail_fw: |
| 606 |
unregister_mmap_device(&astribank_dev); |
| 607 |
dev_reg: |
| 608 |
unregister_mmap_driver(&astribank_driver); |
| 609 |
driver_reg: |
| 610 |
unregister_mmap_bus(); |
| 611 |
bus_reg: |
| 612 |
return ret; |
| 613 |
} |
| 614 |
|
| 615 |
static void __exit xpp_mmap_exit(void) |
| 616 |
{ |
| 617 |
xbus_t *xbus; |
| 618 |
DBG(GENERAL, "\n"); |
| 619 |
disconnecting = 1; |
| 620 |
xbus = xbus_num(global_xbus->num); |
| 621 |
remove_proc_entry("xpp_mmap", xbus->proc_xbus_dir); |
| 622 |
xframe_queue_clear(&txpool); |
| 623 |
xbus_disconnect(xbus); |
| 624 |
kmem_cache_destroy(xframe_cache); |
| 625 |
|
| 626 |
release_region((resource_size_t) FPGA_BASE_ADDR, 8); |
| 627 |
free_irq(FPGA_RX_IRQ, NULL); |
| 628 |
free_irq(FPGA_TX_IRQ, NULL); |
| 629 |
|
| 630 |
unregister_mmap_device(&astribank_dev); |
| 631 |
unregister_mmap_driver(&astribank_driver); |
| 632 |
unregister_mmap_bus(); |
| 633 |
xpp_mmap_unload_fpga(); |
| 634 |
INFO("xpp_mmap module unloaded\n"); |
| 635 |
} |
| 636 |
|
| 637 |
module_init(xpp_mmap_init); |
| 638 |
module_exit(xpp_mmap_exit); |
| 639 |
MODULE_AUTHOR("Alexander Landau <[email protected]>"); |
| 640 |
MODULE_LICENSE("GPL"); |