mrf24j40.c 27.9 KB
Newer Older
A
Alan Ott 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * Driver for Microchip MRF24J40 802.15.4 Wireless-PAN Networking controller
 *
 * Copyright (C) 2012 Alan Ott <alan@signal11.us>
 *                    Signal 11 Software
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

#include <linux/spi/spi.h>
#include <linux/interrupt.h>
#include <linux/module.h>
A
Alexander Aring 已提交
21
#include <linux/regmap.h>
22
#include <linux/ieee802154.h>
23
#include <net/cfg802154.h>
A
Alan Ott 已提交
24 25 26
#include <net/mac802154.h>

/* MRF24J40 Short Address Registers */
27 28 29 30 31 32
#define REG_RXMCR	0x00  /* Receive MAC control */
#define REG_PANIDL	0x01  /* PAN ID (low) */
#define REG_PANIDH	0x02  /* PAN ID (high) */
#define REG_SADRL	0x03  /* Short address (low) */
#define REG_SADRH	0x04  /* Short address (high) */
#define REG_EADR0	0x05  /* Long address (low) (high is EADR7) */
33 34 35 36 37 38 39 40 41
#define REG_EADR1	0x06
#define REG_EADR2	0x07
#define REG_EADR3	0x08
#define REG_EADR4	0x09
#define REG_EADR5	0x0A
#define REG_EADR6	0x0B
#define REG_EADR7	0x0C
#define REG_RXFLUSH	0x0D
#define REG_ORDER	0x10
42
#define REG_TXMCR	0x11  /* Transmit MAC control */
43 44 45 46
#define REG_ACKTMOUT	0x12
#define REG_ESLOTG1	0x13
#define REG_SYMTICKL	0x14
#define REG_SYMTICKH	0x15
47 48 49
#define REG_PACON0	0x16  /* Power Amplifier Control */
#define REG_PACON1	0x17  /* Power Amplifier Control */
#define REG_PACON2	0x18  /* Power Amplifier Control */
50
#define REG_TXBCON0	0x1A
51
#define REG_TXNCON	0x1B  /* Transmit Normal FIFO Control */
52 53 54 55 56 57 58 59
#define REG_TXG1CON	0x1C
#define REG_TXG2CON	0x1D
#define REG_ESLOTG23	0x1E
#define REG_ESLOTG45	0x1F
#define REG_ESLOTG67	0x20
#define REG_TXPEND	0x21
#define REG_WAKECON	0x22
#define REG_FROMOFFSET	0x23
60
#define REG_TXSTAT	0x24  /* TX MAC Status Register */
61 62 63 64 65
#define REG_TXBCON1	0x25
#define REG_GATECLK	0x26
#define REG_TXTIME	0x27
#define REG_HSYMTMRL	0x28
#define REG_HSYMTMRH	0x29
66
#define REG_SOFTRST	0x2A  /* Soft Reset */
67 68
#define REG_SECCON0	0x2C
#define REG_SECCON1	0x2D
69
#define REG_TXSTBL	0x2E  /* TX Stabilization */
70
#define REG_RXSR	0x30
71 72 73 74
#define REG_INTSTAT	0x31  /* Interrupt Status */
#define REG_INTCON	0x32  /* Interrupt Control */
#define REG_GPIO	0x33  /* GPIO */
#define REG_TRISGPIO	0x34  /* GPIO direction */
75
#define REG_SLPACK	0x35
76
#define REG_RFCTL	0x36  /* RF Control Mode Register */
77 78
#define REG_SECCR2	0x37
#define REG_BBREG0	0x38
79 80
#define REG_BBREG1	0x39  /* Baseband Registers */
#define REG_BBREG2	0x3A  /* */
81 82
#define REG_BBREG3	0x3B
#define REG_BBREG4	0x3C
83 84
#define REG_BBREG6	0x3E  /* */
#define REG_CCAEDTH	0x3F  /* Energy Detection Threshold */
A
Alan Ott 已提交
85 86

/* MRF24J40 Long Address Registers */
87 88 89 90 91 92 93 94
#define REG_RFCON0	0x200  /* RF Control Registers */
#define REG_RFCON1	0x201
#define REG_RFCON2	0x202
#define REG_RFCON3	0x203
#define REG_RFCON5	0x205
#define REG_RFCON6	0x206
#define REG_RFCON7	0x207
#define REG_RFCON8	0x208
95 96 97 98
#define REG_SLPCAL0	0x209
#define REG_SLPCAL1	0x20A
#define REG_SLPCAL2	0x20B
#define REG_RFSTATE	0x20F
99 100 101 102 103
#define REG_RSSI	0x210
#define REG_SLPCON0	0x211  /* Sleep Clock Control Registers */
#define REG_SLPCON1	0x220
#define REG_WAKETIMEL	0x222  /* Wake-up Time Match Value Low */
#define REG_WAKETIMEH	0x223  /* Wake-up Time Match Value High */
104 105 106 107 108 109
#define REG_REMCNTL	0x224
#define REG_REMCNTH	0x225
#define REG_MAINCNT0	0x226
#define REG_MAINCNT1	0x227
#define REG_MAINCNT2	0x228
#define REG_MAINCNT3	0x229
110
#define REG_TESTMODE	0x22F  /* Test mode */
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
#define REG_ASSOEAR0	0x230
#define REG_ASSOEAR1	0x231
#define REG_ASSOEAR2	0x232
#define REG_ASSOEAR3	0x233
#define REG_ASSOEAR4	0x234
#define REG_ASSOEAR5	0x235
#define REG_ASSOEAR6	0x236
#define REG_ASSOEAR7	0x237
#define REG_ASSOSAR0	0x238
#define REG_ASSOSAR1	0x239
#define REG_UNONCE0	0x240
#define REG_UNONCE1	0x241
#define REG_UNONCE2	0x242
#define REG_UNONCE3	0x243
#define REG_UNONCE4	0x244
#define REG_UNONCE5	0x245
#define REG_UNONCE6	0x246
#define REG_UNONCE7	0x247
#define REG_UNONCE8	0x248
#define REG_UNONCE9	0x249
#define REG_UNONCE10	0x24A
#define REG_UNONCE11	0x24B
#define REG_UNONCE12	0x24C
134
#define REG_RX_FIFO	0x300  /* Receive FIFO */
A
Alan Ott 已提交
135 136 137 138 139 140 141 142 143 144 145

/* Device configuration: Only channels 11-26 on page 0 are supported. */
#define MRF24J40_CHAN_MIN 11
#define MRF24J40_CHAN_MAX 26
#define CHANNEL_MASK (((u32)1 << (MRF24J40_CHAN_MAX + 1)) \
		      - ((u32)1 << MRF24J40_CHAN_MIN))

#define TX_FIFO_SIZE 128 /* From datasheet */
#define RX_FIFO_SIZE 144 /* From datasheet */
#define SET_CHANNEL_DELAY_US 192 /* From datasheet */

146 147
enum mrf24j40_modules { MRF24J40, MRF24J40MA, MRF24J40MC };

A
Alan Ott 已提交
148 149 150
/* Device Private Data */
struct mrf24j40 {
	struct spi_device *spi;
151
	struct ieee802154_hw *hw;
A
Alan Ott 已提交
152

A
Alexander Aring 已提交
153 154
	struct regmap *regmap_short;
	struct regmap *regmap_long;
A
Alan Ott 已提交
155 156 157 158 159
	struct mutex buffer_mutex; /* only used to protect buf */
	struct completion tx_complete;
	u8 *buf; /* 3 bytes. Used for SPI single-register transfers. */
};

A
Alexander Aring 已提交
160 161 162 163 164 165 166 167 168
/* regmap information for short address register access */
#define MRF24J40_SHORT_WRITE	0x01
#define MRF24J40_SHORT_READ	0x00
#define MRF24J40_SHORT_NUMREGS	0x3F

/* regmap information for long address register access */
#define MRF24J40_LONG_ACCESS	0x80
#define MRF24J40_LONG_NUMREGS	0x38F

A
Alan Ott 已提交
169 170 171 172 173 174
/* Read/Write SPI Commands for Short and Long Address registers. */
#define MRF24J40_READSHORT(reg) ((reg) << 1)
#define MRF24J40_WRITESHORT(reg) ((reg) << 1 | 1)
#define MRF24J40_READLONG(reg) (1 << 15 | (reg) << 5)
#define MRF24J40_WRITELONG(reg) (1 << 15 | (reg) << 5 | 1 << 4)

175 176
/* The datasheet indicates the theoretical maximum for SCK to be 10MHz */
#define MAX_SPI_SPEED_HZ 10000000
A
Alan Ott 已提交
177 178 179

#define printdev(X) (&X->spi->dev)

A
Alexander Aring 已提交
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460
static bool
mrf24j40_short_reg_writeable(struct device *dev, unsigned int reg)
{
	switch (reg) {
	case REG_RXMCR:
	case REG_PANIDL:
	case REG_PANIDH:
	case REG_SADRL:
	case REG_SADRH:
	case REG_EADR0:
	case REG_EADR1:
	case REG_EADR2:
	case REG_EADR3:
	case REG_EADR4:
	case REG_EADR5:
	case REG_EADR6:
	case REG_EADR7:
	case REG_RXFLUSH:
	case REG_ORDER:
	case REG_TXMCR:
	case REG_ACKTMOUT:
	case REG_ESLOTG1:
	case REG_SYMTICKL:
	case REG_SYMTICKH:
	case REG_PACON0:
	case REG_PACON1:
	case REG_PACON2:
	case REG_TXBCON0:
	case REG_TXNCON:
	case REG_TXG1CON:
	case REG_TXG2CON:
	case REG_ESLOTG23:
	case REG_ESLOTG45:
	case REG_ESLOTG67:
	case REG_TXPEND:
	case REG_WAKECON:
	case REG_FROMOFFSET:
	case REG_TXBCON1:
	case REG_GATECLK:
	case REG_TXTIME:
	case REG_HSYMTMRL:
	case REG_HSYMTMRH:
	case REG_SOFTRST:
	case REG_SECCON0:
	case REG_SECCON1:
	case REG_TXSTBL:
	case REG_RXSR:
	case REG_INTCON:
	case REG_TRISGPIO:
	case REG_GPIO:
	case REG_RFCTL:
	case REG_SLPACK:
	case REG_BBREG0:
	case REG_BBREG1:
	case REG_BBREG2:
	case REG_BBREG3:
	case REG_BBREG4:
	case REG_BBREG6:
	case REG_CCAEDTH:
		return true;
	default:
		return false;
	}
}

static bool
mrf24j40_short_reg_readable(struct device *dev, unsigned int reg)
{
	bool rc;

	/* all writeable are also readable */
	rc = mrf24j40_short_reg_writeable(dev, reg);
	if (rc)
		return rc;

	/* readonly regs */
	switch (reg) {
	case REG_TXSTAT:
	case REG_INTSTAT:
		return true;
	default:
		return false;
	}
}

static bool
mrf24j40_short_reg_volatile(struct device *dev, unsigned int reg)
{
	/* can be changed during runtime */
	switch (reg) {
	case REG_TXSTAT:
	case REG_INTSTAT:
	case REG_RXFLUSH:
	case REG_TXNCON:
	case REG_SOFTRST:
	case REG_RFCTL:
	case REG_TXBCON0:
	case REG_TXG1CON:
	case REG_TXG2CON:
	case REG_TXBCON1:
	case REG_SECCON0:
	case REG_RXSR:
	case REG_SLPACK:
	case REG_SECCR2:
	case REG_BBREG6:
	/* use them in spi_async and regmap so it's volatile */
	case REG_BBREG1:
		return true;
	default:
		return false;
	}
}

static bool
mrf24j40_short_reg_precious(struct device *dev, unsigned int reg)
{
	/* don't clear irq line on read */
	switch (reg) {
	case REG_INTSTAT:
		return true;
	default:
		return false;
	}
}

static const struct regmap_config mrf24j40_short_regmap = {
	.name = "mrf24j40_short",
	.reg_bits = 7,
	.val_bits = 8,
	.pad_bits = 1,
	.write_flag_mask = MRF24J40_SHORT_WRITE,
	.read_flag_mask = MRF24J40_SHORT_READ,
	.cache_type = REGCACHE_RBTREE,
	.max_register = MRF24J40_SHORT_NUMREGS,
	.writeable_reg = mrf24j40_short_reg_writeable,
	.readable_reg = mrf24j40_short_reg_readable,
	.volatile_reg = mrf24j40_short_reg_volatile,
	.precious_reg = mrf24j40_short_reg_precious,
};

static bool
mrf24j40_long_reg_writeable(struct device *dev, unsigned int reg)
{
	switch (reg) {
	case REG_RFCON0:
	case REG_RFCON1:
	case REG_RFCON2:
	case REG_RFCON3:
	case REG_RFCON5:
	case REG_RFCON6:
	case REG_RFCON7:
	case REG_RFCON8:
	case REG_SLPCAL2:
	case REG_SLPCON0:
	case REG_SLPCON1:
	case REG_WAKETIMEL:
	case REG_WAKETIMEH:
	case REG_REMCNTL:
	case REG_REMCNTH:
	case REG_MAINCNT0:
	case REG_MAINCNT1:
	case REG_MAINCNT2:
	case REG_MAINCNT3:
	case REG_TESTMODE:
	case REG_ASSOEAR0:
	case REG_ASSOEAR1:
	case REG_ASSOEAR2:
	case REG_ASSOEAR3:
	case REG_ASSOEAR4:
	case REG_ASSOEAR5:
	case REG_ASSOEAR6:
	case REG_ASSOEAR7:
	case REG_ASSOSAR0:
	case REG_ASSOSAR1:
	case REG_UNONCE0:
	case REG_UNONCE1:
	case REG_UNONCE2:
	case REG_UNONCE3:
	case REG_UNONCE4:
	case REG_UNONCE5:
	case REG_UNONCE6:
	case REG_UNONCE7:
	case REG_UNONCE8:
	case REG_UNONCE9:
	case REG_UNONCE10:
	case REG_UNONCE11:
	case REG_UNONCE12:
		return true;
	default:
		return false;
	}
}

static bool
mrf24j40_long_reg_readable(struct device *dev, unsigned int reg)
{
	bool rc;

	/* all writeable are also readable */
	rc = mrf24j40_long_reg_writeable(dev, reg);
	if (rc)
		return rc;

	/* readonly regs */
	switch (reg) {
	case REG_SLPCAL0:
	case REG_SLPCAL1:
	case REG_RFSTATE:
	case REG_RSSI:
		return true;
	default:
		return false;
	}
}

static bool
mrf24j40_long_reg_volatile(struct device *dev, unsigned int reg)
{
	/* can be changed during runtime */
	switch (reg) {
	case REG_SLPCAL0:
	case REG_SLPCAL1:
	case REG_SLPCAL2:
	case REG_RFSTATE:
	case REG_RSSI:
	case REG_MAINCNT3:
		return true;
	default:
		return false;
	}
}

static const struct regmap_config mrf24j40_long_regmap = {
	.name = "mrf24j40_long",
	.reg_bits = 11,
	.val_bits = 8,
	.pad_bits = 5,
	.write_flag_mask = MRF24J40_LONG_ACCESS,
	.read_flag_mask = MRF24J40_LONG_ACCESS,
	.cache_type = REGCACHE_RBTREE,
	.max_register = MRF24J40_LONG_NUMREGS,
	.writeable_reg = mrf24j40_long_reg_writeable,
	.readable_reg = mrf24j40_long_reg_readable,
	.volatile_reg = mrf24j40_long_reg_volatile,
};

static int mrf24j40_long_regmap_write(void *context, const void *data,
				      size_t count)
{
	struct spi_device *spi = context;
	u8 buf[3];

	if (count > 3)
		return -EINVAL;

	/* regmap supports read/write mask only in frist byte
	 * long write access need to set the 12th bit, so we
	 * make special handling for write.
	 */
	memcpy(buf, data, count);
	buf[1] |= (1 << 4);

	return spi_write(spi, buf, count);
}

static int
mrf24j40_long_regmap_read(void *context, const void *reg, size_t reg_size,
			  void *val, size_t val_size)
{
	struct spi_device *spi = context;

	return spi_write_then_read(spi, reg, reg_size, val, val_size);
}

static const struct regmap_bus mrf24j40_long_regmap_bus = {
	.write = mrf24j40_long_regmap_write,
	.read = mrf24j40_long_regmap_read,
	.reg_format_endian_default = REGMAP_ENDIAN_BIG,
	.val_format_endian_default = REGMAP_ENDIAN_BIG,
};

A
Alan Ott 已提交
461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623
static int write_short_reg(struct mrf24j40 *devrec, u8 reg, u8 value)
{
	int ret;
	struct spi_message msg;
	struct spi_transfer xfer = {
		.len = 2,
		.tx_buf = devrec->buf,
		.rx_buf = devrec->buf,
	};

	spi_message_init(&msg);
	spi_message_add_tail(&xfer, &msg);

	mutex_lock(&devrec->buffer_mutex);
	devrec->buf[0] = MRF24J40_WRITESHORT(reg);
	devrec->buf[1] = value;

	ret = spi_sync(devrec->spi, &msg);
	if (ret)
		dev_err(printdev(devrec),
			"SPI write Failed for short register 0x%hhx\n", reg);

	mutex_unlock(&devrec->buffer_mutex);
	return ret;
}

static int read_short_reg(struct mrf24j40 *devrec, u8 reg, u8 *val)
{
	int ret = -1;
	struct spi_message msg;
	struct spi_transfer xfer = {
		.len = 2,
		.tx_buf = devrec->buf,
		.rx_buf = devrec->buf,
	};

	spi_message_init(&msg);
	spi_message_add_tail(&xfer, &msg);

	mutex_lock(&devrec->buffer_mutex);
	devrec->buf[0] = MRF24J40_READSHORT(reg);
	devrec->buf[1] = 0;

	ret = spi_sync(devrec->spi, &msg);
	if (ret)
		dev_err(printdev(devrec),
			"SPI read Failed for short register 0x%hhx\n", reg);
	else
		*val = devrec->buf[1];

	mutex_unlock(&devrec->buffer_mutex);
	return ret;
}

static int read_long_reg(struct mrf24j40 *devrec, u16 reg, u8 *value)
{
	int ret;
	u16 cmd;
	struct spi_message msg;
	struct spi_transfer xfer = {
		.len = 3,
		.tx_buf = devrec->buf,
		.rx_buf = devrec->buf,
	};

	spi_message_init(&msg);
	spi_message_add_tail(&xfer, &msg);

	cmd = MRF24J40_READLONG(reg);
	mutex_lock(&devrec->buffer_mutex);
	devrec->buf[0] = cmd >> 8 & 0xff;
	devrec->buf[1] = cmd & 0xff;
	devrec->buf[2] = 0;

	ret = spi_sync(devrec->spi, &msg);
	if (ret)
		dev_err(printdev(devrec),
			"SPI read Failed for long register 0x%hx\n", reg);
	else
		*value = devrec->buf[2];

	mutex_unlock(&devrec->buffer_mutex);
	return ret;
}

/* This function relies on an undocumented write method. Once a write command
   and address is set, as many bytes of data as desired can be clocked into
   the device. The datasheet only shows setting one byte at a time. */
static int write_tx_buf(struct mrf24j40 *devrec, u16 reg,
			const u8 *data, size_t length)
{
	int ret;
	u16 cmd;
	u8 lengths[2];
	struct spi_message msg;
	struct spi_transfer addr_xfer = {
		.len = 2,
		.tx_buf = devrec->buf,
	};
	struct spi_transfer lengths_xfer = {
		.len = 2,
		.tx_buf = &lengths, /* TODO: Is DMA really required for SPI? */
	};
	struct spi_transfer data_xfer = {
		.len = length,
		.tx_buf = data,
	};

	/* Range check the length. 2 bytes are used for the length fields.*/
	if (length > TX_FIFO_SIZE-2) {
		dev_err(printdev(devrec), "write_tx_buf() was passed too large a buffer. Performing short write.\n");
		length = TX_FIFO_SIZE-2;
	}

	spi_message_init(&msg);
	spi_message_add_tail(&addr_xfer, &msg);
	spi_message_add_tail(&lengths_xfer, &msg);
	spi_message_add_tail(&data_xfer, &msg);

	cmd = MRF24J40_WRITELONG(reg);
	mutex_lock(&devrec->buffer_mutex);
	devrec->buf[0] = cmd >> 8 & 0xff;
	devrec->buf[1] = cmd & 0xff;
	lengths[0] = 0x0; /* Header Length. Set to 0 for now. TODO */
	lengths[1] = length; /* Total length */

	ret = spi_sync(devrec->spi, &msg);
	if (ret)
		dev_err(printdev(devrec), "SPI write Failed for TX buf\n");

	mutex_unlock(&devrec->buffer_mutex);
	return ret;
}

static int mrf24j40_read_rx_buf(struct mrf24j40 *devrec,
				u8 *data, u8 *len, u8 *lqi)
{
	u8 rx_len;
	u8 addr[2];
	u8 lqi_rssi[2];
	u16 cmd;
	int ret;
	struct spi_message msg;
	struct spi_transfer addr_xfer = {
		.len = 2,
		.tx_buf = &addr,
	};
	struct spi_transfer data_xfer = {
		.len = 0x0, /* set below */
		.rx_buf = data,
	};
	struct spi_transfer status_xfer = {
		.len = 2,
		.rx_buf = &lqi_rssi,
	};

	/* Get the length of the data in the RX FIFO. The length in this
	 * register exclues the 1-byte length field at the beginning. */
	ret = read_long_reg(devrec, REG_RX_FIFO, &rx_len);
	if (ret)
		goto out;

	/* Range check the RX FIFO length, accounting for the one-byte
624
	 * length field at the beginning. */
A
Alan Ott 已提交
625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657
	if (rx_len > RX_FIFO_SIZE-1) {
		dev_err(printdev(devrec), "Invalid length read from device. Performing short read.\n");
		rx_len = RX_FIFO_SIZE-1;
	}

	if (rx_len > *len) {
		/* Passed in buffer wasn't big enough. Should never happen. */
		dev_err(printdev(devrec), "Buffer not big enough. Performing short read\n");
		rx_len = *len;
	}

	/* Set up the commands to read the data. */
	cmd = MRF24J40_READLONG(REG_RX_FIFO+1);
	addr[0] = cmd >> 8 & 0xff;
	addr[1] = cmd & 0xff;
	data_xfer.len = rx_len;

	spi_message_init(&msg);
	spi_message_add_tail(&addr_xfer, &msg);
	spi_message_add_tail(&data_xfer, &msg);
	spi_message_add_tail(&status_xfer, &msg);

	ret = spi_sync(devrec->spi, &msg);
	if (ret) {
		dev_err(printdev(devrec), "SPI RX Buffer Read Failed.\n");
		goto out;
	}

	*lqi = lqi_rssi[0];
	*len = rx_len;

#ifdef DEBUG
	print_hex_dump(KERN_DEBUG, "mrf24j40 rx: ",
658
		       DUMP_PREFIX_OFFSET, 16, 1, data, *len, 0);
659 660
	pr_debug("mrf24j40 rx: lqi: %02hhx rssi: %02hhx\n",
		 lqi_rssi[0], lqi_rssi[1]);
A
Alan Ott 已提交
661 662 663 664 665 666
#endif

out:
	return ret;
}

667
static int mrf24j40_tx(struct ieee802154_hw *hw, struct sk_buff *skb)
A
Alan Ott 已提交
668
{
669
	struct mrf24j40 *devrec = hw->priv;
A
Alan Ott 已提交
670 671 672 673 674 675 676 677 678
	u8 val;
	int ret = 0;

	dev_dbg(printdev(devrec), "tx packet of %d bytes\n", skb->len);

	ret = write_tx_buf(devrec, 0x000, skb->data, skb->len);
	if (ret)
		goto err;

679
	reinit_completion(&devrec->tx_complete);
680

A
Alan Ott 已提交
681 682 683 684 685
	/* Set TXNTRIG bit of TXNCON to send packet */
	ret = read_short_reg(devrec, REG_TXNCON, &val);
	if (ret)
		goto err;
	val |= 0x1;
686 687 688
	/* Set TXNACKREQ if the ACK bit is set in the packet. */
	if (skb->data[0] & IEEE802154_FC_ACK_REQ)
		val |= 0x4;
A
Alan Ott 已提交
689 690 691 692 693 694 695 696 697
	write_short_reg(devrec, REG_TXNCON, val);

	/* Wait for the device to send the TX complete interrupt. */
	ret = wait_for_completion_interruptible_timeout(
						&devrec->tx_complete,
						5 * HZ);
	if (ret == -ERESTARTSYS)
		goto err;
	if (ret == 0) {
698
		dev_warn(printdev(devrec), "Timeout waiting for TX interrupt\n");
A
Alan Ott 已提交
699 700 701 702 703 704 705 706 707
		ret = -ETIMEDOUT;
		goto err;
	}

	/* Check for send error from the device. */
	ret = read_short_reg(devrec, REG_TXSTAT, &val);
	if (ret)
		goto err;
	if (val & 0x1) {
708
		dev_dbg(printdev(devrec), "Error Sending. Retry count exceeded\n");
A
Alan Ott 已提交
709 710 711 712 713 714 715 716 717
		ret = -ECOMM; /* TODO: Better error code ? */
	} else
		dev_dbg(printdev(devrec), "Packet Sent\n");

err:

	return ret;
}

718
static int mrf24j40_ed(struct ieee802154_hw *hw, u8 *level)
A
Alan Ott 已提交
719 720
{
	/* TODO: */
721
	pr_warn("mrf24j40: ed not implemented\n");
A
Alan Ott 已提交
722 723 724 725
	*level = 0;
	return 0;
}

726
static int mrf24j40_start(struct ieee802154_hw *hw)
A
Alan Ott 已提交
727
{
728
	struct mrf24j40 *devrec = hw->priv;
A
Alan Ott 已提交
729 730 731

	dev_dbg(printdev(devrec), "start\n");

732 733 734
	/* Clear TXNIE and RXIE. Enable interrupts */
	return regmap_update_bits(devrec->regmap_short, REG_INTCON,
				  0x01 | 0x08, 0x00);
A
Alan Ott 已提交
735 736
}

737
static void mrf24j40_stop(struct ieee802154_hw *hw)
A
Alan Ott 已提交
738
{
739
	struct mrf24j40 *devrec = hw->priv;
740

A
Alan Ott 已提交
741 742
	dev_dbg(printdev(devrec), "stop\n");

743 744 745
	/* Set TXNIE and RXIE. Disable Interrupts */
	regmap_update_bits(devrec->regmap_short, REG_INTCON, 0x01 | 0x08,
			   0x01 | 0x08);
A
Alan Ott 已提交
746 747
}

748
static int mrf24j40_set_channel(struct ieee802154_hw *hw, u8 page, u8 channel)
A
Alan Ott 已提交
749
{
750
	struct mrf24j40 *devrec = hw->priv;
A
Alan Ott 已提交
751 752 753 754 755 756 757 758 759 760 761
	u8 val;
	int ret;

	dev_dbg(printdev(devrec), "Set Channel %d\n", channel);

	WARN_ON(page != 0);
	WARN_ON(channel < MRF24J40_CHAN_MIN);
	WARN_ON(channel > MRF24J40_CHAN_MAX);

	/* Set Channel TODO */
	val = (channel-11) << 4 | 0x03;
762 763 764
	ret = regmap_update_bits(devrec->regmap_long, REG_RFCON0, 0xf0, val);
	if (ret)
		return ret;
A
Alan Ott 已提交
765 766

	/* RF Reset */
767
	ret = regmap_update_bits(devrec->regmap_short, REG_RFCTL, 0x04, 0x04);
A
Alan Ott 已提交
768 769 770
	if (ret)
		return ret;

771 772 773
	ret = regmap_update_bits(devrec->regmap_short, REG_RFCTL, 0x04, 0x00);
	if (!ret)
		udelay(SET_CHANNEL_DELAY_US); /* per datasheet */
A
Alan Ott 已提交
774

775
	return ret;
A
Alan Ott 已提交
776 777
}

778
static int mrf24j40_filter(struct ieee802154_hw *hw,
A
Alan Ott 已提交
779 780 781
			   struct ieee802154_hw_addr_filt *filt,
			   unsigned long changed)
{
782
	struct mrf24j40 *devrec = hw->priv;
A
Alan Ott 已提交
783 784 785

	dev_dbg(printdev(devrec), "filter\n");

786
	if (changed & IEEE802154_AFILT_SADDR_CHANGED) {
A
Alan Ott 已提交
787 788
		/* Short Addr */
		u8 addrh, addrl;
789

790 791
		addrh = le16_to_cpu(filt->short_addr) >> 8 & 0xff;
		addrl = le16_to_cpu(filt->short_addr) & 0xff;
A
Alan Ott 已提交
792

793 794
		regmap_write(devrec->regmap_short, REG_SADRH, addrh);
		regmap_write(devrec->regmap_short, REG_SADRL, addrl);
A
Alan Ott 已提交
795 796 797 798
		dev_dbg(printdev(devrec),
			"Set short addr to %04hx\n", filt->short_addr);
	}

799
	if (changed & IEEE802154_AFILT_IEEEADDR_CHANGED) {
A
Alan Ott 已提交
800
		/* Device Address */
801 802 803
		u8 i, addr[8];

		memcpy(addr, &filt->ieee_addr, 8);
A
Alan Ott 已提交
804
		for (i = 0; i < 8; i++)
805 806
			regmap_write(devrec->regmap_short, REG_EADR0 + i,
				     addr[i]);
A
Alan Ott 已提交
807 808

#ifdef DEBUG
809
		pr_debug("Set long addr to: ");
A
Alan Ott 已提交
810
		for (i = 0; i < 8; i++)
811 812
			pr_debug("%02hhx ", addr[7 - i]);
		pr_debug("\n");
A
Alan Ott 已提交
813 814 815
#endif
	}

816
	if (changed & IEEE802154_AFILT_PANID_CHANGED) {
A
Alan Ott 已提交
817 818
		/* PAN ID */
		u8 panidl, panidh;
819

820 821
		panidh = le16_to_cpu(filt->pan_id) >> 8 & 0xff;
		panidl = le16_to_cpu(filt->pan_id) & 0xff;
822 823
		regmap_write(devrec->regmap_short, REG_PANIDH, panidh);
		regmap_write(devrec->regmap_short, REG_PANIDL, panidl);
A
Alan Ott 已提交
824 825 826 827

		dev_dbg(printdev(devrec), "Set PANID to %04hx\n", filt->pan_id);
	}

828
	if (changed & IEEE802154_AFILT_PANC_CHANGED) {
A
Alan Ott 已提交
829 830 831 832 833
		/* Pan Coordinator */
		u8 val;
		int ret;

		if (filt->pan_coord)
834
			val = 0x8;
A
Alan Ott 已提交
835
		else
836 837 838 839 840
			val = 0x0;
		ret = regmap_update_bits(devrec->regmap_short, REG_RXMCR, 0x8,
					 val);
		if (ret)
			return ret;
A
Alan Ott 已提交
841 842 843 844 845 846

		/* REG_SLOTTED is maintained as default (unslotted/CSMA-CA).
		 * REG_ORDER is maintained as default (no beacon/superframe).
		 */

		dev_dbg(printdev(devrec), "Set Pan Coord to %s\n",
847
			filt->pan_coord ? "on" : "off");
A
Alan Ott 已提交
848 849 850 851 852 853 854 855 856 857 858
	}

	return 0;
}

static int mrf24j40_handle_rx(struct mrf24j40 *devrec)
{
	u8 len = RX_FIFO_SIZE;
	u8 lqi = 0;
	u8 val;
	int ret = 0;
859
	int ret2;
A
Alan Ott 已提交
860 861 862 863 864 865 866 867 868 869
	struct sk_buff *skb;

	/* Turn off reception of packets off the air. This prevents the
	 * device from overwriting the buffer while we're reading it. */
	ret = read_short_reg(devrec, REG_BBREG1, &val);
	if (ret)
		goto out;
	val |= 4; /* SET RXDECINV */
	write_short_reg(devrec, REG_BBREG1, val);

870
	skb = dev_alloc_skb(len);
A
Alan Ott 已提交
871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886
	if (!skb) {
		ret = -ENOMEM;
		goto out;
	}

	ret = mrf24j40_read_rx_buf(devrec, skb_put(skb, len), &len, &lqi);
	if (ret < 0) {
		dev_err(printdev(devrec), "Failure reading RX FIFO\n");
		kfree_skb(skb);
		ret = -EINVAL;
		goto out;
	}

	/* TODO: Other drivers call ieee20154_rx_irqsafe() here (eg: cc2040,
	 * also from a workqueue).  I think irqsafe is not necessary here.
	 * Can someone confirm? */
887
	ieee802154_rx_irqsafe(devrec->hw, skb, lqi);
A
Alan Ott 已提交
888 889 890 891 892

	dev_dbg(printdev(devrec), "RX Handled\n");

out:
	/* Turn back on reception of packets off the air. */
893 894 895
	ret2 = read_short_reg(devrec, REG_BBREG1, &val);
	if (ret2)
		return ret2;
A
Alan Ott 已提交
896 897 898 899 900 901
	val &= ~0x4; /* Clear RXDECINV */
	write_short_reg(devrec, REG_BBREG1, val);

	return ret;
}

902
static const struct ieee802154_ops mrf24j40_ops = {
A
Alan Ott 已提交
903
	.owner = THIS_MODULE,
904
	.xmit_sync = mrf24j40_tx,
A
Alan Ott 已提交
905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931
	.ed = mrf24j40_ed,
	.start = mrf24j40_start,
	.stop = mrf24j40_stop,
	.set_channel = mrf24j40_set_channel,
	.set_hw_addr_filt = mrf24j40_filter,
};

static irqreturn_t mrf24j40_isr(int irq, void *data)
{
	struct mrf24j40 *devrec = data;
	u8 intstat;
	int ret;

	/* Read the interrupt status */
	ret = read_short_reg(devrec, REG_INTSTAT, &intstat);
	if (ret)
		goto out;

	/* Check for TX complete */
	if (intstat & 0x1)
		complete(&devrec->tx_complete);

	/* Check for Rx */
	if (intstat & 0x8)
		mrf24j40_handle_rx(devrec);

out:
A
Alan Ott 已提交
932
	return IRQ_HANDLED;
A
Alan Ott 已提交
933 934
}

935 936 937 938 939 940
static int mrf24j40_hw_init(struct mrf24j40 *devrec)
{
	int ret;

	/* Initialize the device.
		From datasheet section 3.2: Initialization. */
941
	ret = regmap_write(devrec->regmap_short, REG_SOFTRST, 0x07);
942 943 944
	if (ret)
		goto err_ret;

945
	ret = regmap_write(devrec->regmap_short, REG_PACON2, 0x98);
946 947 948
	if (ret)
		goto err_ret;

949
	ret = regmap_write(devrec->regmap_short, REG_TXSTBL, 0x95);
950 951 952
	if (ret)
		goto err_ret;

953
	ret = regmap_write(devrec->regmap_long, REG_RFCON0, 0x03);
954 955 956
	if (ret)
		goto err_ret;

957
	ret = regmap_write(devrec->regmap_long, REG_RFCON1, 0x01);
958 959 960
	if (ret)
		goto err_ret;

961
	ret = regmap_write(devrec->regmap_long, REG_RFCON2, 0x80);
962 963 964
	if (ret)
		goto err_ret;

965
	ret = regmap_write(devrec->regmap_long, REG_RFCON6, 0x90);
966 967 968
	if (ret)
		goto err_ret;

969
	ret = regmap_write(devrec->regmap_long, REG_RFCON7, 0x80);
970 971 972
	if (ret)
		goto err_ret;

973
	ret = regmap_write(devrec->regmap_long, REG_RFCON8, 0x10);
974 975 976
	if (ret)
		goto err_ret;

977
	ret = regmap_write(devrec->regmap_long, REG_SLPCON1, 0x21);
978 979 980
	if (ret)
		goto err_ret;

981
	ret = regmap_write(devrec->regmap_short, REG_BBREG2, 0x80);
982 983 984
	if (ret)
		goto err_ret;

985
	ret = regmap_write(devrec->regmap_short, REG_CCAEDTH, 0x60);
986 987 988
	if (ret)
		goto err_ret;

989
	ret = regmap_write(devrec->regmap_short, REG_BBREG6, 0x40);
990 991 992
	if (ret)
		goto err_ret;

993
	ret = regmap_write(devrec->regmap_short, REG_RFCTL, 0x04);
994 995 996
	if (ret)
		goto err_ret;

997
	ret = regmap_write(devrec->regmap_short, REG_RFCTL, 0x0);
998 999 1000 1001 1002 1003
	if (ret)
		goto err_ret;

	udelay(192);

	/* Set RX Mode. RXMCR<1:0>: 0x0 normal, 0x1 promisc, 0x2 error */
1004
	ret = regmap_update_bits(devrec->regmap_short, REG_RXMCR, 0x03, 0x00);
1005 1006 1007
	if (ret)
		goto err_ret;

1008 1009 1010 1011
	if (spi_get_device_id(devrec->spi)->driver_data == MRF24J40MC) {
		/* Enable external amplifier.
		 * From MRF24J40MC datasheet section 1.3: Operation.
		 */
1012 1013
		regmap_update_bits(devrec->regmap_long, REG_TESTMODE, 0x07,
				   0x07);
1014

1015 1016 1017
		/* Set GPIO3 as output. */
		regmap_update_bits(devrec->regmap_short, REG_TRISGPIO, 0x08,
				   0x08);
1018

1019 1020
		/* Set GPIO3 HIGH to enable U5 voltage regulator */
		regmap_update_bits(devrec->regmap_short, REG_GPIO, 0x08, 0x08);
1021 1022 1023 1024

		/* Reduce TX pwr to meet FCC requirements.
		 * From MRF24J40MC datasheet section 3.1.1
		 */
1025
		regmap_write(devrec->regmap_long, REG_RFCON3, 0x28);
1026 1027
	}

1028 1029 1030 1031 1032 1033
	return 0;

err_ret:
	return ret;
}

1034 1035
static void  mrf24j40_phy_setup(struct mrf24j40 *devrec)
{
1036
	ieee802154_random_extended_addr(&devrec->hw->phy->perm_extended_addr);
1037 1038 1039
	devrec->hw->phy->current_channel = 11;
}

1040
static int mrf24j40_probe(struct spi_device *spi)
A
Alan Ott 已提交
1041 1042
{
	int ret = -ENOMEM;
1043
	struct ieee802154_hw *hw;
A
Alan Ott 已提交
1044 1045
	struct mrf24j40 *devrec;

1046
	dev_info(&spi->dev, "probe(). IRQ: %d\n", spi->irq);
A
Alan Ott 已提交
1047

1048 1049 1050 1051
	/* Register with the 802154 subsystem */

	hw = ieee802154_alloc_hw(sizeof(*devrec), &mrf24j40_ops);
	if (!hw)
1052
		goto err_ret;
1053 1054 1055 1056 1057 1058 1059

	devrec = hw->priv;
	devrec->spi = spi;
	spi_set_drvdata(spi, devrec);
	devrec->hw = hw;
	devrec->hw->parent = &spi->dev;
	devrec->hw->phy->supported.channels[0] = CHANNEL_MASK;
1060
	devrec->hw->flags = IEEE802154_HW_TX_OMIT_CKSUM | IEEE802154_HW_AFILT;
1061

A
Alexander Aring 已提交
1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080
	devrec->regmap_short = devm_regmap_init_spi(spi,
						    &mrf24j40_short_regmap);
	if (IS_ERR(devrec->regmap_short)) {
		ret = PTR_ERR(devrec->regmap_short);
		dev_err(&spi->dev, "Failed to allocate short register map: %d\n",
			ret);
		goto err_register_device;
	}

	devrec->regmap_long = devm_regmap_init(&spi->dev,
					       &mrf24j40_long_regmap_bus,
					       spi, &mrf24j40_long_regmap);
	if (IS_ERR(devrec->regmap_long)) {
		ret = PTR_ERR(devrec->regmap_long);
		dev_err(&spi->dev, "Failed to allocate long register map: %d\n",
			ret);
		goto err_register_device;
	}

1081
	devrec->buf = devm_kzalloc(&spi->dev, 3, GFP_KERNEL);
A
Alan Ott 已提交
1082
	if (!devrec->buf)
1083
		goto err_register_device;
A
Alan Ott 已提交
1084

1085 1086 1087 1088 1089
	if (spi->max_speed_hz > MAX_SPI_SPEED_HZ) {
		dev_warn(&spi->dev, "spi clock above possible maximum: %d",
			 MAX_SPI_SPEED_HZ);
		return -EINVAL;
	}
A
Alan Ott 已提交
1090 1091 1092 1093

	mutex_init(&devrec->buffer_mutex);
	init_completion(&devrec->tx_complete);

1094
	ret = mrf24j40_hw_init(devrec);
A
Alan Ott 已提交
1095
	if (ret)
1096
		goto err_register_device;
A
Alan Ott 已提交
1097

1098 1099
	mrf24j40_phy_setup(devrec);

1100 1101 1102 1103 1104 1105 1106
	ret = devm_request_threaded_irq(&spi->dev,
					spi->irq,
					NULL,
					mrf24j40_isr,
					IRQF_TRIGGER_LOW|IRQF_ONESHOT,
					dev_name(&spi->dev),
					devrec);
A
Alan Ott 已提交
1107 1108 1109

	if (ret) {
		dev_err(printdev(devrec), "Unable to get IRQ");
1110
		goto err_register_device;
A
Alan Ott 已提交
1111 1112
	}

1113 1114 1115 1116 1117
	dev_dbg(printdev(devrec), "registered mrf24j40\n");
	ret = ieee802154_register_hw(devrec->hw);
	if (ret)
		goto err_register_device;

A
Alan Ott 已提交
1118 1119 1120
	return 0;

err_register_device:
1121
	ieee802154_free_hw(devrec->hw);
1122
err_ret:
A
Alan Ott 已提交
1123 1124 1125
	return ret;
}

1126
static int mrf24j40_remove(struct spi_device *spi)
A
Alan Ott 已提交
1127
{
1128
	struct mrf24j40 *devrec = spi_get_drvdata(spi);
A
Alan Ott 已提交
1129 1130 1131

	dev_dbg(printdev(devrec), "remove\n");

1132 1133
	ieee802154_unregister_hw(devrec->hw);
	ieee802154_free_hw(devrec->hw);
A
Alan Ott 已提交
1134 1135 1136 1137 1138 1139
	/* TODO: Will ieee802154_free_device() wait until ->xmit() is
	 * complete? */

	return 0;
}

1140 1141 1142 1143 1144 1145 1146 1147
static const struct of_device_id mrf24j40_of_match[] = {
	{ .compatible = "microchip,mrf24j40", .data = (void *)MRF24J40 },
	{ .compatible = "microchip,mrf24j40ma", .data = (void *)MRF24J40MA },
	{ .compatible = "microchip,mrf24j40mc", .data = (void *)MRF24J40MC },
	{ },
};
MODULE_DEVICE_TABLE(of, mrf24j40_of_match);

A
Alan Ott 已提交
1148
static const struct spi_device_id mrf24j40_ids[] = {
1149 1150 1151
	{ "mrf24j40", MRF24J40 },
	{ "mrf24j40ma", MRF24J40MA },
	{ "mrf24j40mc", MRF24J40MC },
A
Alan Ott 已提交
1152 1153 1154 1155 1156 1157
	{ },
};
MODULE_DEVICE_TABLE(spi, mrf24j40_ids);

static struct spi_driver mrf24j40_driver = {
	.driver = {
1158
		.of_match_table = of_match_ptr(mrf24j40_of_match),
A
Alan Ott 已提交
1159 1160 1161 1162 1163
		.name = "mrf24j40",
		.owner = THIS_MODULE,
	},
	.id_table = mrf24j40_ids,
	.probe = mrf24j40_probe,
1164
	.remove = mrf24j40_remove,
A
Alan Ott 已提交
1165 1166
};

1167
module_spi_driver(mrf24j40_driver);
A
Alan Ott 已提交
1168 1169 1170 1171

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Alan Ott");
MODULE_DESCRIPTION("MRF24J40 SPI 802.15.4 Controller Driver");