at86rf230.c 41.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * AT86RF230/RF231 driver
 *
 * Copyright (C) 2009-2012 Siemens AG
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2
 * as published by the Free Software Foundation.
 *
 * 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.
 *
 * Written by:
 * Dmitry Eremin-Solenikov <dbaryshkov@gmail.com>
 * Alexander Smirnov <alex.bluesman.smirnov@gmail.com>
A
Alexander Aring 已提交
18
 * Alexander Aring <aar@pengutronix.de>
19 20 21 22
 */
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/interrupt.h>
23
#include <linux/irq.h>
24 25 26 27 28
#include <linux/gpio.h>
#include <linux/delay.h>
#include <linux/spinlock.h>
#include <linux/spi/spi.h>
#include <linux/spi/at86rf230.h>
A
Alexander Aring 已提交
29
#include <linux/regmap.h>
30
#include <linux/skbuff.h>
31
#include <linux/of_gpio.h>
32
#include <linux/ieee802154.h>
33 34

#include <net/mac802154.h>
35
#include <net/cfg802154.h>
36

37 38 39 40 41
struct at86rf230_local;
/* at86rf2xx chip depend data.
 * All timings are in us.
 */
struct at86rf2xx_chip_data {
42
	u16 t_sleep_cycle;
43
	u16 t_channel_switch;
44
	u16 t_reset_to_off;
45 46
	u16 t_off_to_aack;
	u16 t_off_to_tx_on;
47 48
	u16 t_frame;
	u16 t_p_ack;
49 50
	int rssi_base_val;

51
	int (*set_channel)(struct at86rf230_local *, u8, u8);
52
	int (*get_desense_steps)(struct at86rf230_local *, s32);
53 54
};

55
#define AT86RF2XX_MAX_BUF (127 + 3)
56

57 58
struct at86rf230_state_change {
	struct at86rf230_local *lp;
59

60 61 62 63 64 65 66
	struct spi_message msg;
	struct spi_transfer trx;
	u8 buf[AT86RF2XX_MAX_BUF];

	void (*complete)(void *context);
	u8 from_state;
	u8 to_state;
67 68

	bool irq_enable;
69 70 71 72
};

struct at86rf230_local {
	struct spi_device *spi;
73

74
	struct ieee802154_hw *hw;
75
	struct at86rf2xx_chip_data *data;
A
Alexander Aring 已提交
76
	struct regmap *regmap;
77

78 79 80
	struct completion state_complete;
	struct at86rf230_state_change state;

81
	struct at86rf230_state_change irq;
82

83
	bool tx_aret;
84
	s8 max_frame_retries;
85 86 87 88 89
	bool is_tx;
	/* spinlock for is_tx protection */
	spinlock_t lock;
	struct sk_buff *tx_skb;
	struct at86rf230_state_change tx;
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
};

#define	RG_TRX_STATUS	(0x01)
#define	SR_TRX_STATUS		0x01, 0x1f, 0
#define	SR_RESERVED_01_3	0x01, 0x20, 5
#define	SR_CCA_STATUS		0x01, 0x40, 6
#define	SR_CCA_DONE		0x01, 0x80, 7
#define	RG_TRX_STATE	(0x02)
#define	SR_TRX_CMD		0x02, 0x1f, 0
#define	SR_TRAC_STATUS		0x02, 0xe0, 5
#define	RG_TRX_CTRL_0	(0x03)
#define	SR_CLKM_CTRL		0x03, 0x07, 0
#define	SR_CLKM_SHA_SEL		0x03, 0x08, 3
#define	SR_PAD_IO_CLKM		0x03, 0x30, 4
#define	SR_PAD_IO		0x03, 0xc0, 6
#define	RG_TRX_CTRL_1	(0x04)
#define	SR_IRQ_POLARITY		0x04, 0x01, 0
#define	SR_IRQ_MASK_MODE	0x04, 0x02, 1
#define	SR_SPI_CMD_MODE		0x04, 0x0c, 2
#define	SR_RX_BL_CTRL		0x04, 0x10, 4
#define	SR_TX_AUTO_CRC_ON	0x04, 0x20, 5
#define	SR_IRQ_2_EXT_EN		0x04, 0x40, 6
#define	SR_PA_EXT_EN		0x04, 0x80, 7
#define	RG_PHY_TX_PWR	(0x05)
#define	SR_TX_PWR		0x05, 0x0f, 0
#define	SR_PA_LT		0x05, 0x30, 4
#define	SR_PA_BUF_LT		0x05, 0xc0, 6
#define	RG_PHY_RSSI	(0x06)
#define	SR_RSSI			0x06, 0x1f, 0
#define	SR_RND_VALUE		0x06, 0x60, 5
#define	SR_RX_CRC_VALID		0x06, 0x80, 7
#define	RG_PHY_ED_LEVEL	(0x07)
#define	SR_ED_LEVEL		0x07, 0xff, 0
#define	RG_PHY_CC_CCA	(0x08)
#define	SR_CHANNEL		0x08, 0x1f, 0
#define	SR_CCA_MODE		0x08, 0x60, 5
#define	SR_CCA_REQUEST		0x08, 0x80, 7
#define	RG_CCA_THRES	(0x09)
#define	SR_CCA_ED_THRES		0x09, 0x0f, 0
#define	SR_RESERVED_09_1	0x09, 0xf0, 4
#define	RG_RX_CTRL	(0x0a)
#define	SR_PDT_THRES		0x0a, 0x0f, 0
#define	SR_RESERVED_0a_1	0x0a, 0xf0, 4
#define	RG_SFD_VALUE	(0x0b)
#define	SR_SFD_VALUE		0x0b, 0xff, 0
#define	RG_TRX_CTRL_2	(0x0c)
#define	SR_OQPSK_DATA_RATE	0x0c, 0x03, 0
137 138
#define	SR_SUB_MODE		0x0c, 0x04, 2
#define	SR_BPSK_QPSK		0x0c, 0x08, 3
139 140
#define	SR_OQPSK_SUB1_RC_EN	0x0c, 0x10, 4
#define	SR_RESERVED_0c_5	0x0c, 0x60, 5
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
#define	SR_RX_SAFE_MODE		0x0c, 0x80, 7
#define	RG_ANT_DIV	(0x0d)
#define	SR_ANT_CTRL		0x0d, 0x03, 0
#define	SR_ANT_EXT_SW_EN	0x0d, 0x04, 2
#define	SR_ANT_DIV_EN		0x0d, 0x08, 3
#define	SR_RESERVED_0d_2	0x0d, 0x70, 4
#define	SR_ANT_SEL		0x0d, 0x80, 7
#define	RG_IRQ_MASK	(0x0e)
#define	SR_IRQ_MASK		0x0e, 0xff, 0
#define	RG_IRQ_STATUS	(0x0f)
#define	SR_IRQ_0_PLL_LOCK	0x0f, 0x01, 0
#define	SR_IRQ_1_PLL_UNLOCK	0x0f, 0x02, 1
#define	SR_IRQ_2_RX_START	0x0f, 0x04, 2
#define	SR_IRQ_3_TRX_END	0x0f, 0x08, 3
#define	SR_IRQ_4_CCA_ED_DONE	0x0f, 0x10, 4
#define	SR_IRQ_5_AMI		0x0f, 0x20, 5
#define	SR_IRQ_6_TRX_UR		0x0f, 0x40, 6
#define	SR_IRQ_7_BAT_LOW	0x0f, 0x80, 7
#define	RG_VREG_CTRL	(0x10)
#define	SR_RESERVED_10_6	0x10, 0x03, 0
#define	SR_DVDD_OK		0x10, 0x04, 2
#define	SR_DVREG_EXT		0x10, 0x08, 3
#define	SR_RESERVED_10_3	0x10, 0x30, 4
#define	SR_AVDD_OK		0x10, 0x40, 6
#define	SR_AVREG_EXT		0x10, 0x80, 7
#define	RG_BATMON	(0x11)
#define	SR_BATMON_VTH		0x11, 0x0f, 0
#define	SR_BATMON_HR		0x11, 0x10, 4
#define	SR_BATMON_OK		0x11, 0x20, 5
#define	SR_RESERVED_11_1	0x11, 0xc0, 6
#define	RG_XOSC_CTRL	(0x12)
#define	SR_XTAL_TRIM		0x12, 0x0f, 0
#define	SR_XTAL_MODE		0x12, 0xf0, 4
#define	RG_RX_SYN	(0x15)
#define	SR_RX_PDT_LEVEL		0x15, 0x0f, 0
#define	SR_RESERVED_15_2	0x15, 0x70, 4
#define	SR_RX_PDT_DIS		0x15, 0x80, 7
#define	RG_XAH_CTRL_1	(0x17)
#define	SR_RESERVED_17_8	0x17, 0x01, 0
#define	SR_AACK_PROM_MODE	0x17, 0x02, 1
#define	SR_AACK_ACK_TIME	0x17, 0x04, 2
#define	SR_RESERVED_17_5	0x17, 0x08, 3
#define	SR_AACK_UPLD_RES_FT	0x17, 0x10, 4
#define	SR_AACK_FLTR_RES_FT	0x17, 0x20, 5
185
#define	SR_CSMA_LBT_MODE	0x17, 0x40, 6
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
#define	SR_RESERVED_17_1	0x17, 0x80, 7
#define	RG_FTN_CTRL	(0x18)
#define	SR_RESERVED_18_2	0x18, 0x7f, 0
#define	SR_FTN_START		0x18, 0x80, 7
#define	RG_PLL_CF	(0x1a)
#define	SR_RESERVED_1a_2	0x1a, 0x7f, 0
#define	SR_PLL_CF_START		0x1a, 0x80, 7
#define	RG_PLL_DCU	(0x1b)
#define	SR_RESERVED_1b_3	0x1b, 0x3f, 0
#define	SR_RESERVED_1b_2	0x1b, 0x40, 6
#define	SR_PLL_DCU_START	0x1b, 0x80, 7
#define	RG_PART_NUM	(0x1c)
#define	SR_PART_NUM		0x1c, 0xff, 0
#define	RG_VERSION_NUM	(0x1d)
#define	SR_VERSION_NUM		0x1d, 0xff, 0
#define	RG_MAN_ID_0	(0x1e)
#define	SR_MAN_ID_0		0x1e, 0xff, 0
#define	RG_MAN_ID_1	(0x1f)
#define	SR_MAN_ID_1		0x1f, 0xff, 0
#define	RG_SHORT_ADDR_0	(0x20)
#define	SR_SHORT_ADDR_0		0x20, 0xff, 0
#define	RG_SHORT_ADDR_1	(0x21)
#define	SR_SHORT_ADDR_1		0x21, 0xff, 0
#define	RG_PAN_ID_0	(0x22)
#define	SR_PAN_ID_0		0x22, 0xff, 0
#define	RG_PAN_ID_1	(0x23)
#define	SR_PAN_ID_1		0x23, 0xff, 0
#define	RG_IEEE_ADDR_0	(0x24)
#define	SR_IEEE_ADDR_0		0x24, 0xff, 0
#define	RG_IEEE_ADDR_1	(0x25)
#define	SR_IEEE_ADDR_1		0x25, 0xff, 0
#define	RG_IEEE_ADDR_2	(0x26)
#define	SR_IEEE_ADDR_2		0x26, 0xff, 0
#define	RG_IEEE_ADDR_3	(0x27)
#define	SR_IEEE_ADDR_3		0x27, 0xff, 0
#define	RG_IEEE_ADDR_4	(0x28)
#define	SR_IEEE_ADDR_4		0x28, 0xff, 0
#define	RG_IEEE_ADDR_5	(0x29)
#define	SR_IEEE_ADDR_5		0x29, 0xff, 0
#define	RG_IEEE_ADDR_6	(0x2a)
#define	SR_IEEE_ADDR_6		0x2a, 0xff, 0
#define	RG_IEEE_ADDR_7	(0x2b)
#define	SR_IEEE_ADDR_7		0x2b, 0xff, 0
#define	RG_XAH_CTRL_0	(0x2c)
#define	SR_SLOTTED_OPERATION	0x2c, 0x01, 0
#define	SR_MAX_CSMA_RETRIES	0x2c, 0x0e, 1
#define	SR_MAX_FRAME_RETRIES	0x2c, 0xf0, 4
#define	RG_CSMA_SEED_0	(0x2d)
#define	SR_CSMA_SEED_0		0x2d, 0xff, 0
#define	RG_CSMA_SEED_1	(0x2e)
#define	SR_CSMA_SEED_1		0x2e, 0x07, 0
#define	SR_AACK_I_AM_COORD	0x2e, 0x08, 3
#define	SR_AACK_DIS_ACK		0x2e, 0x10, 4
#define	SR_AACK_SET_PD		0x2e, 0x20, 5
#define	SR_AACK_FVN_MODE	0x2e, 0xc0, 6
#define	RG_CSMA_BE	(0x2f)
#define	SR_MIN_BE		0x2f, 0x0f, 0
#define	SR_MAX_BE		0x2f, 0xf0, 4

#define CMD_REG		0x80
#define CMD_REG_MASK	0x3f
#define CMD_WRITE	0x40
#define CMD_FB		0x20

#define IRQ_BAT_LOW	(1 << 7)
#define IRQ_TRX_UR	(1 << 6)
#define IRQ_AMI		(1 << 5)
#define IRQ_CCA_ED	(1 << 4)
#define IRQ_TRX_END	(1 << 3)
#define IRQ_RX_START	(1 << 2)
#define IRQ_PLL_UNL	(1 << 1)
#define IRQ_PLL_LOCK	(1 << 0)

259 260 261
#define IRQ_ACTIVE_HIGH	0
#define IRQ_ACTIVE_LOW	1

262 263 264 265 266 267 268 269 270 271 272 273
#define STATE_P_ON		0x00	/* BUSY */
#define STATE_BUSY_RX		0x01
#define STATE_BUSY_TX		0x02
#define STATE_FORCE_TRX_OFF	0x03
#define STATE_FORCE_TX_ON	0x04	/* IDLE */
/* 0x05 */				/* INVALID_PARAMETER */
#define STATE_RX_ON		0x06
/* 0x07 */				/* SUCCESS */
#define STATE_TRX_OFF		0x08
#define STATE_TX_ON		0x09
/* 0x0a - 0x0e */			/* 0x0a - UNSUPPORTED_ATTRIBUTE */
#define STATE_SLEEP		0x0F
274
#define STATE_PREP_DEEP_SLEEP	0x10
275 276
#define STATE_BUSY_RX_AACK	0x11
#define STATE_BUSY_TX_ARET	0x12
277 278
#define STATE_RX_AACK_ON	0x16
#define STATE_TX_ARET_ON	0x19
279 280 281 282 283
#define STATE_RX_ON_NOCLK	0x1C
#define STATE_RX_AACK_ON_NOCLK	0x1D
#define STATE_BUSY_RX_AACK_NOCLK 0x1E
#define STATE_TRANSITION_IN_PROGRESS 0x1F

A
Alexander Aring 已提交
284 285
#define AT86RF2XX_NUMREGS 0x3F

286
static void
287 288
at86rf230_async_state_change(struct at86rf230_local *lp,
			     struct at86rf230_state_change *ctx,
289 290
			     const u8 state, void (*complete)(void *context),
			     const bool irq_enable);
291

A
Alexander Aring 已提交
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
static inline int
__at86rf230_write(struct at86rf230_local *lp,
		  unsigned int addr, unsigned int data)
{
	return regmap_write(lp->regmap, addr, data);
}

static inline int
__at86rf230_read(struct at86rf230_local *lp,
		 unsigned int addr, unsigned int *data)
{
	return regmap_read(lp->regmap, addr, data);
}

static inline int
at86rf230_read_subreg(struct at86rf230_local *lp,
		      unsigned int addr, unsigned int mask,
		      unsigned int shift, unsigned int *data)
{
	int rc;

	rc = __at86rf230_read(lp, addr, data);
	if (rc > 0)
		*data = (*data & mask) >> shift;

	return rc;
}

static inline int
at86rf230_write_subreg(struct at86rf230_local *lp,
		       unsigned int addr, unsigned int mask,
		       unsigned int shift, unsigned int data)
{
	return regmap_update_bits(lp->regmap, addr, mask, data << shift);
}

static bool
at86rf230_reg_writeable(struct device *dev, unsigned int reg)
{
	switch (reg) {
	case RG_TRX_STATE:
	case RG_TRX_CTRL_0:
	case RG_TRX_CTRL_1:
	case RG_PHY_TX_PWR:
	case RG_PHY_ED_LEVEL:
	case RG_PHY_CC_CCA:
	case RG_CCA_THRES:
	case RG_RX_CTRL:
	case RG_SFD_VALUE:
	case RG_TRX_CTRL_2:
	case RG_ANT_DIV:
	case RG_IRQ_MASK:
	case RG_VREG_CTRL:
	case RG_BATMON:
	case RG_XOSC_CTRL:
	case RG_RX_SYN:
	case RG_XAH_CTRL_1:
	case RG_FTN_CTRL:
	case RG_PLL_CF:
	case RG_PLL_DCU:
	case RG_SHORT_ADDR_0:
	case RG_SHORT_ADDR_1:
	case RG_PAN_ID_0:
	case RG_PAN_ID_1:
	case RG_IEEE_ADDR_0:
	case RG_IEEE_ADDR_1:
	case RG_IEEE_ADDR_2:
	case RG_IEEE_ADDR_3:
	case RG_IEEE_ADDR_4:
	case RG_IEEE_ADDR_5:
	case RG_IEEE_ADDR_6:
	case RG_IEEE_ADDR_7:
	case RG_XAH_CTRL_0:
	case RG_CSMA_SEED_0:
	case RG_CSMA_SEED_1:
	case RG_CSMA_BE:
		return true;
	default:
		return false;
	}
}

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

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

	/* readonly regs */
	switch (reg) {
	case RG_TRX_STATUS:
	case RG_PHY_RSSI:
	case RG_IRQ_STATUS:
	case RG_PART_NUM:
	case RG_VERSION_NUM:
	case RG_MAN_ID_1:
	case RG_MAN_ID_0:
		return true;
	default:
		return false;
	}
}

static bool
at86rf230_reg_volatile(struct device *dev, unsigned int reg)
{
	/* can be changed during runtime */
	switch (reg) {
	case RG_TRX_STATUS:
	case RG_TRX_STATE:
	case RG_PHY_RSSI:
	case RG_PHY_ED_LEVEL:
	case RG_IRQ_STATUS:
	case RG_VREG_CTRL:
		return true;
	default:
		return false;
	}
}

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

428
static const struct regmap_config at86rf230_regmap_spi_config = {
A
Alexander Aring 已提交
429 430 431 432 433 434 435 436 437 438 439 440
	.reg_bits = 8,
	.val_bits = 8,
	.write_flag_mask = CMD_REG | CMD_WRITE,
	.read_flag_mask = CMD_REG,
	.cache_type = REGCACHE_RBTREE,
	.max_register = AT86RF2XX_NUMREGS,
	.writeable_reg = at86rf230_reg_writeable,
	.readable_reg = at86rf230_reg_readable,
	.volatile_reg = at86rf230_reg_volatile,
	.precious_reg = at86rf230_reg_precious,
};

441 442 443 444 445 446
static void
at86rf230_async_error_recover(void *context)
{
	struct at86rf230_state_change *ctx = context;
	struct at86rf230_local *lp = ctx->lp;

447
	at86rf230_async_state_change(lp, ctx, STATE_RX_AACK_ON, NULL, false);
448
	ieee802154_wake_queue(lp->hw);
449 450
}

451
static inline void
452 453 454 455 456 457
at86rf230_async_error(struct at86rf230_local *lp,
		      struct at86rf230_state_change *ctx, int rc)
{
	dev_err(&lp->spi->dev, "spi_async error %d\n", rc);

	at86rf230_async_state_change(lp, ctx, STATE_FORCE_TRX_OFF,
458
				     at86rf230_async_error_recover, false);
459 460 461
}

/* Generic function to get some register value in async mode */
462
static void
463 464
at86rf230_async_read_reg(struct at86rf230_local *lp, const u8 reg,
			 struct at86rf230_state_change *ctx,
465 466
			 void (*complete)(void *context),
			 const bool irq_enable)
467
{
468 469
	int rc;

470 471 472 473 474
	u8 *tx_buf = ctx->buf;

	tx_buf[0] = (reg & CMD_REG_MASK) | CMD_REG;
	ctx->trx.len = 2;
	ctx->msg.complete = complete;
475 476 477 478 479 480 481 482
	ctx->irq_enable = irq_enable;
	rc = spi_async(lp->spi, &ctx->msg);
	if (rc) {
		if (irq_enable)
			enable_irq(lp->spi->irq);

		at86rf230_async_error(lp, ctx, rc);
	}
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
}

static void
at86rf230_async_state_assert(void *context)
{
	struct at86rf230_state_change *ctx = context;
	struct at86rf230_local *lp = ctx->lp;
	const u8 *buf = ctx->buf;
	const u8 trx_state = buf[1] & 0x1f;

	/* Assert state change */
	if (trx_state != ctx->to_state) {
		/* Special handling if transceiver state is in
		 * STATE_BUSY_RX_AACK and a SHR was detected.
		 */
		if  (trx_state == STATE_BUSY_RX_AACK) {
			/* Undocumented race condition. If we send a state
			 * change to STATE_RX_AACK_ON the transceiver could
			 * change his state automatically to STATE_BUSY_RX_AACK
			 * if a SHR was detected. This is not an error, but we
			 * can't assert this.
			 */
			if (ctx->to_state == STATE_RX_AACK_ON)
				goto done;

			/* If we change to STATE_TX_ON without forcing and
			 * transceiver state is STATE_BUSY_RX_AACK, we wait
			 * 'tFrame + tPAck' receiving time. In this time the
			 * PDU should be received. If the transceiver is still
			 * in STATE_BUSY_RX_AACK, we run a force state change
			 * to STATE_TX_ON. This is a timeout handling, if the
			 * transceiver stucks in STATE_BUSY_RX_AACK.
			 */
			if (ctx->to_state == STATE_TX_ON) {
				at86rf230_async_state_change(lp, ctx,
							     STATE_FORCE_TX_ON,
519 520
							     ctx->complete,
							     ctx->irq_enable);
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
				return;
			}
		}

		dev_warn(&lp->spi->dev, "unexcept state change from 0x%02x to 0x%02x. Actual state: 0x%02x\n",
			 ctx->from_state, ctx->to_state, trx_state);
	}

done:
	if (ctx->complete)
		ctx->complete(context);
}

/* Do state change timing delay. */
static void
at86rf230_async_state_delay(void *context)
{
	struct at86rf230_state_change *ctx = context;
	struct at86rf230_local *lp = ctx->lp;
	struct at86rf2xx_chip_data *c = lp->data;
	bool force = false;

	/* The force state changes are will show as normal states in the
	 * state status subregister. We change the to_state to the
	 * corresponding one and remember if it was a force change, this
	 * differs if we do a state change from STATE_BUSY_RX_AACK.
	 */
	switch (ctx->to_state) {
	case STATE_FORCE_TX_ON:
		ctx->to_state = STATE_TX_ON;
		force = true;
		break;
	case STATE_FORCE_TRX_OFF:
		ctx->to_state = STATE_TRX_OFF;
		force = true;
		break;
	default:
		break;
	}

	switch (ctx->from_state) {
562 563 564 565 566 567 568 569 570 571 572 573 574
	case STATE_TRX_OFF:
		switch (ctx->to_state) {
		case STATE_RX_AACK_ON:
			usleep_range(c->t_off_to_aack, c->t_off_to_aack + 10);
			goto change;
		case STATE_TX_ON:
			usleep_range(c->t_off_to_tx_on,
				     c->t_off_to_tx_on + 10);
			goto change;
		default:
			break;
		}
		break;
575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591
	case STATE_BUSY_RX_AACK:
		switch (ctx->to_state) {
		case STATE_TX_ON:
			/* Wait for worst case receiving time if we
			 * didn't make a force change from BUSY_RX_AACK
			 * to TX_ON.
			 */
			if (!force) {
				usleep_range(c->t_frame + c->t_p_ack,
					     c->t_frame + c->t_p_ack + 1000);
				goto change;
			}
			break;
		default:
			break;
		}
		break;
592 593 594 595 596 597 598 599 600 601
	/* Default value, means RESET state */
	case STATE_P_ON:
		switch (ctx->to_state) {
		case STATE_TRX_OFF:
			usleep_range(c->t_reset_to_off, c->t_reset_to_off + 10);
			goto change;
		default:
			break;
		}
		break;
602 603 604 605 606 607 608 609
	default:
		break;
	}

	/* Default delay is 1us in the most cases */
	udelay(1);

change:
610 611 612
	at86rf230_async_read_reg(lp, RG_TRX_STATUS, ctx,
				 at86rf230_async_state_assert,
				 ctx->irq_enable);
613 614 615 616 617 618 619 620 621 622 623 624 625 626
}

static void
at86rf230_async_state_change_start(void *context)
{
	struct at86rf230_state_change *ctx = context;
	struct at86rf230_local *lp = ctx->lp;
	u8 *buf = ctx->buf;
	const u8 trx_state = buf[1] & 0x1f;
	int rc;

	/* Check for "possible" STATE_TRANSITION_IN_PROGRESS */
	if (trx_state == STATE_TRANSITION_IN_PROGRESS) {
		udelay(1);
627 628 629
		at86rf230_async_read_reg(lp, RG_TRX_STATUS, ctx,
					 at86rf230_async_state_change_start,
					 ctx->irq_enable);
630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650
		return;
	}

	/* Check if we already are in the state which we change in */
	if (trx_state == ctx->to_state) {
		if (ctx->complete)
			ctx->complete(context);
		return;
	}

	/* Set current state to the context of state change */
	ctx->from_state = trx_state;

	/* Going into the next step for a state change which do a timing
	 * relevant delay.
	 */
	buf[0] = (RG_TRX_STATE & CMD_REG_MASK) | CMD_REG | CMD_WRITE;
	buf[1] = ctx->to_state;
	ctx->trx.len = 2;
	ctx->msg.complete = at86rf230_async_state_delay;
	rc = spi_async(lp->spi, &ctx->msg);
651 652 653 654
	if (rc) {
		if (ctx->irq_enable)
			enable_irq(lp->spi->irq);

655
		at86rf230_async_error(lp, ctx, rc);
656
	}
657 658
}

659
static void
660 661
at86rf230_async_state_change(struct at86rf230_local *lp,
			     struct at86rf230_state_change *ctx,
662 663
			     const u8 state, void (*complete)(void *context),
			     const bool irq_enable)
664
{
665 666 667
	/* Initialization for the state change context */
	ctx->to_state = state;
	ctx->complete = complete;
668 669 670 671
	ctx->irq_enable = irq_enable;
	at86rf230_async_read_reg(lp, RG_TRX_STATUS, ctx,
				 at86rf230_async_state_change_start,
				 irq_enable);
672
}
673

674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689
static void
at86rf230_sync_state_change_complete(void *context)
{
	struct at86rf230_state_change *ctx = context;
	struct at86rf230_local *lp = ctx->lp;

	complete(&lp->state_complete);
}

/* This function do a sync framework above the async state change.
 * Some callbacks of the IEEE 802.15.4 driver interface need to be
 * handled synchronously.
 */
static int
at86rf230_sync_state_change(struct at86rf230_local *lp, unsigned int state)
{
690
	unsigned long rc;
691

692 693 694
	at86rf230_async_state_change(lp, &lp->state, state,
				     at86rf230_sync_state_change_complete,
				     false);
695 696 697

	rc = wait_for_completion_timeout(&lp->state_complete,
					 msecs_to_jiffies(100));
698 699
	if (!rc) {
		at86rf230_async_error(lp, &lp->state, -ETIMEDOUT);
700
		return -ETIMEDOUT;
701
	}
702 703 704 705

	return 0;
}

706 707 708 709 710
static void
at86rf230_tx_complete(void *context)
{
	struct at86rf230_state_change *ctx = context;
	struct at86rf230_local *lp = ctx->lp;
711
	struct sk_buff *skb = lp->tx_skb;
712

A
Alexander Aring 已提交
713
	enable_irq(lp->spi->irq);
714

A
Alexander Aring 已提交
715
	ieee802154_xmit_complete(lp->hw, skb, !lp->tx_aret);
716 717 718 719 720 721 722 723
}

static void
at86rf230_tx_on(void *context)
{
	struct at86rf230_state_change *ctx = context;
	struct at86rf230_local *lp = ctx->lp;

724 725
	at86rf230_async_state_change(lp, &lp->irq, STATE_RX_AACK_ON,
				     at86rf230_tx_complete, true);
726 727 728 729 730 731 732 733
}

static void
at86rf230_tx_trac_error(void *context)
{
	struct at86rf230_state_change *ctx = context;
	struct at86rf230_local *lp = ctx->lp;

734 735
	at86rf230_async_state_change(lp, ctx, STATE_TX_ON,
				     at86rf230_tx_on, true);
736 737 738 739 740 741 742 743 744 745 746 747 748 749
}

static void
at86rf230_tx_trac_check(void *context)
{
	struct at86rf230_state_change *ctx = context;
	struct at86rf230_local *lp = ctx->lp;
	const u8 *buf = ctx->buf;
	const u8 trac = (buf[1] & 0xe0) >> 5;

	/* If trac status is different than zero we need to do a state change
	 * to STATE_FORCE_TRX_OFF then STATE_TX_ON to recover the transceiver
	 * state to TX_ON.
	 */
750
	if (trac)
751 752
		at86rf230_async_state_change(lp, ctx, STATE_FORCE_TRX_OFF,
					     at86rf230_tx_trac_error, true);
753 754
	else
		at86rf230_tx_on(context);
755 756 757 758 759 760 761 762
}

static void
at86rf230_tx_trac_status(void *context)
{
	struct at86rf230_state_change *ctx = context;
	struct at86rf230_local *lp = ctx->lp;

763 764
	at86rf230_async_read_reg(lp, RG_TRX_STATE, ctx,
				 at86rf230_tx_trac_check, true);
765 766 767 768
}

static void
at86rf230_rx(struct at86rf230_local *lp,
769
	     const u8 *data, const u8 len, const u8 lqi)
770 771 772 773 774 775 776
{
	struct sk_buff *skb;
	u8 rx_local_buf[AT86RF2XX_MAX_BUF];

	memcpy(rx_local_buf, data, len);
	enable_irq(lp->spi->irq);

777
	skb = dev_alloc_skb(IEEE802154_MTU);
778 779 780 781 782 783
	if (!skb) {
		dev_vdbg(&lp->spi->dev, "failed to allocate sk_buff\n");
		return;
	}

	memcpy(skb_put(skb, len), rx_local_buf, len);
784
	ieee802154_rx_irqsafe(lp->hw, skb, lqi);
785
}
786

787 788 789 790 791 792
static void
at86rf230_rx_read_frame_complete(void *context)
{
	struct at86rf230_state_change *ctx = context;
	struct at86rf230_local *lp = ctx->lp;
	const u8 *buf = lp->irq.buf;
793 794 795 796 797 798
	u8 len = buf[1];

	if (!ieee802154_is_valid_psdu_len(len)) {
		dev_vdbg(&lp->spi->dev, "corrupted frame received\n");
		len = IEEE802154_MTU;
	}
799

A
Alexander Aring 已提交
800
	at86rf230_rx(lp, buf + 2, len, buf[2 + len]);
801 802
}

803
static void
804 805
at86rf230_rx_read_frame(struct at86rf230_local *lp)
{
806 807
	int rc;

808
	u8 *buf = lp->irq.buf;
809 810

	buf[0] = CMD_FB;
811 812
	lp->irq.trx.len = AT86RF2XX_MAX_BUF;
	lp->irq.msg.complete = at86rf230_rx_read_frame_complete;
813 814 815 816 817
	rc = spi_async(lp->spi, &lp->irq.msg);
	if (rc) {
		enable_irq(lp->spi->irq);
		at86rf230_async_error(lp, &lp->irq, rc);
	}
818 819 820 821 822 823 824 825 826 827 828 829 830 831
}

static void
at86rf230_rx_trac_check(void *context)
{
	struct at86rf230_state_change *ctx = context;
	struct at86rf230_local *lp = ctx->lp;

	/* Possible check on trac status here. This could be useful to make
	 * some stats why receive is failed. Not used at the moment, but it's
	 * maybe timing relevant. Datasheet doesn't say anything about this.
	 * The programming guide say do it so.
	 */

832
	at86rf230_rx_read_frame(lp);
833 834
}

835
static void
836 837 838 839 840 841 842 843
at86rf230_irq_trx_end(struct at86rf230_local *lp)
{
	spin_lock(&lp->lock);
	if (lp->is_tx) {
		lp->is_tx = 0;
		spin_unlock(&lp->lock);

		if (lp->tx_aret)
844 845 846 847
			at86rf230_async_state_change(lp, &lp->irq,
						     STATE_FORCE_TX_ON,
						     at86rf230_tx_trac_status,
						     true);
848
		else
849 850 851 852
			at86rf230_async_state_change(lp, &lp->irq,
						     STATE_RX_AACK_ON,
						     at86rf230_tx_complete,
						     true);
853 854
	} else {
		spin_unlock(&lp->lock);
855 856
		at86rf230_async_read_reg(lp, RG_TRX_STATE, &lp->irq,
					 at86rf230_rx_trac_check, true);
857 858 859 860 861 862 863 864 865 866 867 868
	}
}

static void
at86rf230_irq_status(void *context)
{
	struct at86rf230_state_change *ctx = context;
	struct at86rf230_local *lp = ctx->lp;
	const u8 *buf = lp->irq.buf;
	const u8 irq = buf[1];

	if (irq & IRQ_TRX_END) {
869
		at86rf230_irq_trx_end(lp);
870 871 872 873 874 875 876 877 878 879 880 881 882 883
	} else {
		enable_irq(lp->spi->irq);
		dev_err(&lp->spi->dev, "not supported irq %02x received\n",
			irq);
	}
}

static irqreturn_t at86rf230_isr(int irq, void *data)
{
	struct at86rf230_local *lp = data;
	struct at86rf230_state_change *ctx = &lp->irq;
	u8 *buf = ctx->buf;
	int rc;

884
	disable_irq_nosync(irq);
885 886 887 888 889 890

	buf[0] = (RG_IRQ_STATUS & CMD_REG_MASK) | CMD_REG;
	ctx->trx.len = 2;
	ctx->msg.complete = at86rf230_irq_status;
	rc = spi_async(lp->spi, &ctx->msg);
	if (rc) {
891
		enable_irq(irq);
892 893 894 895 896 897 898 899 900 901 902 903 904 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 932 933 934 935 936 937 938 939 940 941 942 943
		at86rf230_async_error(lp, ctx, rc);
		return IRQ_NONE;
	}

	return IRQ_HANDLED;
}

static void
at86rf230_write_frame_complete(void *context)
{
	struct at86rf230_state_change *ctx = context;
	struct at86rf230_local *lp = ctx->lp;
	u8 *buf = ctx->buf;
	int rc;

	buf[0] = (RG_TRX_STATE & CMD_REG_MASK) | CMD_REG | CMD_WRITE;
	buf[1] = STATE_BUSY_TX;
	ctx->trx.len = 2;
	ctx->msg.complete = NULL;
	rc = spi_async(lp->spi, &ctx->msg);
	if (rc)
		at86rf230_async_error(lp, ctx, rc);
}

static void
at86rf230_write_frame(void *context)
{
	struct at86rf230_state_change *ctx = context;
	struct at86rf230_local *lp = ctx->lp;
	struct sk_buff *skb = lp->tx_skb;
	u8 *buf = lp->tx.buf;
	int rc;

	spin_lock(&lp->lock);
	lp->is_tx = 1;
	spin_unlock(&lp->lock);

	buf[0] = CMD_FB | CMD_WRITE;
	buf[1] = skb->len + 2;
	memcpy(buf + 2, skb->data, skb->len);
	lp->tx.trx.len = skb->len + 2;
	lp->tx.msg.complete = at86rf230_write_frame_complete;
	rc = spi_async(lp->spi, &lp->tx.msg);
	if (rc)
		at86rf230_async_error(lp, ctx, rc);
}

static void
at86rf230_xmit_tx_on(void *context)
{
	struct at86rf230_state_change *ctx = context;
	struct at86rf230_local *lp = ctx->lp;
944

945 946
	at86rf230_async_state_change(lp, ctx, STATE_TX_ARET_ON,
				     at86rf230_write_frame, false);
947 948 949
}

static int
950
at86rf230_xmit(struct ieee802154_hw *hw, struct sk_buff *skb)
951
{
952
	struct at86rf230_local *lp = hw->priv;
953
	struct at86rf230_state_change *ctx = &lp->tx;
954

955
	void (*tx_complete)(void *context) = at86rf230_write_frame;
956

957
	lp->tx_skb = skb;
958

959 960 961 962 963 964
	/* In ARET mode we need to go into STATE_TX_ARET_ON after we
	 * are in STATE_TX_ON. The pfad differs here, so we change
	 * the complete handler.
	 */
	if (lp->tx_aret)
		tx_complete = at86rf230_xmit_tx_on;
965

966 967
	at86rf230_async_state_change(lp, ctx, STATE_TX_ON, tx_complete, false);

968
	return 0;
969 970 971
}

static int
972
at86rf230_ed(struct ieee802154_hw *hw, u8 *level)
973 974 975 976 977 978 979
{
	BUG_ON(!level);
	*level = 0xbe;
	return 0;
}

static int
980
at86rf230_start(struct ieee802154_hw *hw)
981
{
982
	return at86rf230_sync_state_change(hw->priv, STATE_RX_AACK_ON);
983 984 985
}

static void
986
at86rf230_stop(struct ieee802154_hw *hw)
987
{
988
	at86rf230_sync_state_change(hw->priv, STATE_FORCE_TRX_OFF);
989 990
}

991
static int
992
at86rf23x_set_channel(struct at86rf230_local *lp, u8 page, u8 channel)
993 994 995 996 997
{
	return at86rf230_write_subreg(lp, SR_CHANNEL, channel);
}

static int
998
at86rf212_set_channel(struct at86rf230_local *lp, u8 page, u8 channel)
999 1000 1001 1002 1003 1004 1005 1006 1007 1008
{
	int rc;

	if (channel == 0)
		rc = at86rf230_write_subreg(lp, SR_SUB_MODE, 0);
	else
		rc = at86rf230_write_subreg(lp, SR_SUB_MODE, 1);
	if (rc < 0)
		return rc;

1009
	if (page == 0) {
1010
		rc = at86rf230_write_subreg(lp, SR_BPSK_QPSK, 0);
1011
		lp->data->rssi_base_val = -100;
1012
	} else {
1013
		rc = at86rf230_write_subreg(lp, SR_BPSK_QPSK, 1);
1014
		lp->data->rssi_base_val = -98;
1015
	}
1016 1017 1018
	if (rc < 0)
		return rc;

1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036
	/* This sets the symbol_duration according frequency on the 212.
	 * TODO move this handling while set channel and page in cfg802154.
	 * We can do that, this timings are according 802.15.4 standard.
	 * If we do that in cfg802154, this is a more generic calculation.
	 *
	 * This should also protected from ifs_timer. Means cancel timer and
	 * init with a new value. For now, this is okay.
	 */
	if (channel == 0) {
		if (page == 0) {
			/* SUB:0 and BPSK:0 -> BPSK-20 */
			lp->hw->phy->symbol_duration = 50;
		} else {
			/* SUB:1 and BPSK:0 -> BPSK-40 */
			lp->hw->phy->symbol_duration = 25;
		}
	} else {
		if (page == 0)
1037
			/* SUB:0 and BPSK:1 -> OQPSK-100/200/400 */
1038 1039
			lp->hw->phy->symbol_duration = 40;
		else
1040
			/* SUB:1 and BPSK:1 -> OQPSK-250/500/1000 */
1041 1042 1043 1044 1045 1046 1047 1048
			lp->hw->phy->symbol_duration = 16;
	}

	lp->hw->phy->lifs_period = IEEE802154_LIFS_PERIOD *
				   lp->hw->phy->symbol_duration;
	lp->hw->phy->sifs_period = IEEE802154_SIFS_PERIOD *
				   lp->hw->phy->symbol_duration;

1049 1050 1051
	return at86rf230_write_subreg(lp, SR_CHANNEL, channel);
}

1052
static int
1053
at86rf230_channel(struct ieee802154_hw *hw, u8 page, u8 channel)
1054
{
1055
	struct at86rf230_local *lp = hw->priv;
1056 1057
	int rc;

1058
	rc = lp->data->set_channel(lp, page, channel);
1059 1060 1061
	/* Wait for PLL */
	usleep_range(lp->data->t_channel_switch,
		     lp->data->t_channel_switch + 10);
1062
	return rc;
1063 1064
}

1065
static int
1066
at86rf230_set_hw_addr_filt(struct ieee802154_hw *hw,
1067 1068 1069
			   struct ieee802154_hw_addr_filt *filt,
			   unsigned long changed)
{
1070
	struct at86rf230_local *lp = hw->priv;
1071

1072
	if (changed & IEEE802154_AFILT_SADDR_CHANGED) {
1073 1074
		u16 addr = le16_to_cpu(filt->short_addr);

1075
		dev_vdbg(&lp->spi->dev,
1076
			 "at86rf230_set_hw_addr_filt called for saddr\n");
1077 1078
		__at86rf230_write(lp, RG_SHORT_ADDR_0, addr);
		__at86rf230_write(lp, RG_SHORT_ADDR_1, addr >> 8);
1079 1080
	}

1081
	if (changed & IEEE802154_AFILT_PANID_CHANGED) {
1082 1083
		u16 pan = le16_to_cpu(filt->pan_id);

1084
		dev_vdbg(&lp->spi->dev,
1085
			 "at86rf230_set_hw_addr_filt called for pan id\n");
1086 1087
		__at86rf230_write(lp, RG_PAN_ID_0, pan);
		__at86rf230_write(lp, RG_PAN_ID_1, pan >> 8);
1088 1089
	}

1090
	if (changed & IEEE802154_AFILT_IEEEADDR_CHANGED) {
1091 1092 1093
		u8 i, addr[8];

		memcpy(addr, &filt->ieee_addr, 8);
1094
		dev_vdbg(&lp->spi->dev,
1095
			 "at86rf230_set_hw_addr_filt called for IEEE addr\n");
1096 1097
		for (i = 0; i < 8; i++)
			__at86rf230_write(lp, RG_IEEE_ADDR_0 + i, addr[i]);
1098 1099
	}

1100
	if (changed & IEEE802154_AFILT_PANC_CHANGED) {
1101
		dev_vdbg(&lp->spi->dev,
1102
			 "at86rf230_set_hw_addr_filt called for panc change\n");
1103 1104 1105 1106 1107 1108 1109 1110 1111
		if (filt->pan_coord)
			at86rf230_write_subreg(lp, SR_AACK_I_AM_COORD, 1);
		else
			at86rf230_write_subreg(lp, SR_AACK_I_AM_COORD, 0);
	}

	return 0;
}

1112
static int
1113
at86rf230_set_txpower(struct ieee802154_hw *hw, int db)
1114
{
1115
	struct at86rf230_local *lp = hw->priv;
1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127

	/* typical maximum output is 5dBm with RG_PHY_TX_PWR 0x60, lower five
	 * bits decrease power in 1dB steps. 0x60 represents extra PA gain of
	 * 0dB.
	 * thus, supported values for db range from -26 to 5, for 31dB of
	 * reduction to 0dB of reduction.
	 */
	if (db > 5 || db < -26)
		return -EINVAL;

	db = -(db - 5);

1128
	return __at86rf230_write(lp, RG_PHY_TX_PWR, 0x60 | db);
1129 1130
}

1131
static int
1132
at86rf230_set_lbt(struct ieee802154_hw *hw, bool on)
1133
{
1134
	struct at86rf230_local *lp = hw->priv;
1135 1136 1137 1138

	return at86rf230_write_subreg(lp, SR_CSMA_LBT_MODE, on);
}

1139
static int
A
Alexander Aring 已提交
1140 1141
at86rf230_set_cca_mode(struct ieee802154_hw *hw,
		       const struct wpan_phy_cca *cca)
1142
{
1143
	struct at86rf230_local *lp = hw->priv;
A
Alexander Aring 已提交
1144
	u8 val;
1145

A
Alexander Aring 已提交
1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170
	/* mapping 802.15.4 to driver spec */
	switch (cca->mode) {
	case NL802154_CCA_ENERGY:
		val = 1;
		break;
	case NL802154_CCA_CARRIER:
		val = 2;
		break;
	case NL802154_CCA_ENERGY_CARRIER:
		switch (cca->opt) {
		case NL802154_CCA_OPT_ENERGY_CARRIER_AND:
			val = 3;
			break;
		case NL802154_CCA_OPT_ENERGY_CARRIER_OR:
			val = 0;
			break;
		default:
			return -EINVAL;
		}
		break;
	default:
		return -EINVAL;
	}

	return at86rf230_write_subreg(lp, SR_CCA_MODE, val);
1171 1172
}

1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184
static int
at86rf212_get_desens_steps(struct at86rf230_local *lp, s32 level)
{
	return (level - lp->data->rssi_base_val) * 100 / 207;
}

static int
at86rf23x_get_desens_steps(struct at86rf230_local *lp, s32 level)
{
	return (level - lp->data->rssi_base_val) / 2;
}

1185
static int
1186
at86rf230_set_cca_ed_level(struct ieee802154_hw *hw, s32 level)
1187
{
1188
	struct at86rf230_local *lp = hw->priv;
1189

1190
	if (level < lp->data->rssi_base_val || level > 30)
1191 1192
		return -EINVAL;

1193 1194
	return at86rf230_write_subreg(lp, SR_CCA_ED_THRES,
				      lp->data->get_desense_steps(lp, level));
1195 1196
}

1197
static int
1198
at86rf230_set_csma_params(struct ieee802154_hw *hw, u8 min_be, u8 max_be,
1199 1200
			  u8 retries)
{
1201
	struct at86rf230_local *lp = hw->priv;
1202 1203 1204 1205 1206 1207 1208 1209 1210 1211
	int rc;

	rc = at86rf230_write_subreg(lp, SR_MIN_BE, min_be);
	if (rc)
		return rc;

	rc = at86rf230_write_subreg(lp, SR_MAX_BE, max_be);
	if (rc)
		return rc;

1212
	return at86rf230_write_subreg(lp, SR_MAX_CSMA_RETRIES, retries);
1213 1214 1215
}

static int
1216
at86rf230_set_frame_retries(struct ieee802154_hw *hw, s8 retries)
1217
{
1218
	struct at86rf230_local *lp = hw->priv;
1219 1220 1221
	int rc = 0;

	lp->tx_aret = retries >= 0;
1222
	lp->max_frame_retries = retries;
1223 1224 1225 1226 1227 1228 1229

	if (retries >= 0)
		rc = at86rf230_write_subreg(lp, SR_MAX_FRAME_RETRIES, retries);

	return rc;
}

1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256
static int
at86rf230_set_promiscuous_mode(struct ieee802154_hw *hw, const bool on)
{
	struct at86rf230_local *lp = hw->priv;
	int rc;

	if (on) {
		rc = at86rf230_write_subreg(lp, SR_AACK_DIS_ACK, 1);
		if (rc < 0)
			return rc;

		rc = at86rf230_write_subreg(lp, SR_AACK_PROM_MODE, 1);
		if (rc < 0)
			return rc;
	} else {
		rc = at86rf230_write_subreg(lp, SR_AACK_PROM_MODE, 0);
		if (rc < 0)
			return rc;

		rc = at86rf230_write_subreg(lp, SR_AACK_DIS_ACK, 0);
		if (rc < 0)
			return rc;
	}

	return 0;
}

1257
static const struct ieee802154_ops at86rf230_ops = {
1258
	.owner = THIS_MODULE,
1259
	.xmit_async = at86rf230_xmit,
1260 1261 1262 1263
	.ed = at86rf230_ed,
	.set_channel = at86rf230_channel,
	.start = at86rf230_start,
	.stop = at86rf230_stop,
1264
	.set_hw_addr_filt = at86rf230_set_hw_addr_filt,
1265 1266 1267 1268 1269 1270
	.set_txpower = at86rf230_set_txpower,
	.set_lbt = at86rf230_set_lbt,
	.set_cca_mode = at86rf230_set_cca_mode,
	.set_cca_ed_level = at86rf230_set_cca_ed_level,
	.set_csma_params = at86rf230_set_csma_params,
	.set_frame_retries = at86rf230_set_frame_retries,
1271
	.set_promiscuous_mode = at86rf230_set_promiscuous_mode,
1272 1273
};

1274
static struct at86rf2xx_chip_data at86rf233_data = {
1275
	.t_sleep_cycle = 330,
1276
	.t_channel_switch = 11,
1277
	.t_reset_to_off = 26,
1278 1279
	.t_off_to_aack = 80,
	.t_off_to_tx_on = 80,
1280 1281
	.t_frame = 4096,
	.t_p_ack = 545,
1282 1283
	.rssi_base_val = -91,
	.set_channel = at86rf23x_set_channel,
1284
	.get_desense_steps = at86rf23x_get_desens_steps
1285 1286 1287
};

static struct at86rf2xx_chip_data at86rf231_data = {
1288
	.t_sleep_cycle = 330,
1289
	.t_channel_switch = 24,
1290
	.t_reset_to_off = 37,
1291 1292
	.t_off_to_aack = 110,
	.t_off_to_tx_on = 110,
1293 1294
	.t_frame = 4096,
	.t_p_ack = 545,
1295 1296
	.rssi_base_val = -91,
	.set_channel = at86rf23x_set_channel,
1297
	.get_desense_steps = at86rf23x_get_desens_steps
1298 1299 1300
};

static struct at86rf2xx_chip_data at86rf212_data = {
1301
	.t_sleep_cycle = 330,
1302
	.t_channel_switch = 11,
1303
	.t_reset_to_off = 26,
1304 1305
	.t_off_to_aack = 200,
	.t_off_to_tx_on = 200,
1306 1307
	.t_frame = 4096,
	.t_p_ack = 545,
1308 1309
	.rssi_base_val = -100,
	.set_channel = at86rf212_set_channel,
1310
	.get_desense_steps = at86rf212_get_desens_steps
1311 1312
};

1313
static int at86rf230_hw_init(struct at86rf230_local *lp, u8 xtal_trim)
1314
{
1315
	int rc, irq_type, irq_pol = IRQ_ACTIVE_HIGH;
A
Alexander Aring 已提交
1316
	unsigned int dvdd;
1317
	u8 csma_seed[2];
1318

1319
	rc = at86rf230_sync_state_change(lp, STATE_FORCE_TRX_OFF);
1320 1321
	if (rc)
		return rc;
1322

1323
	irq_type = irq_get_trigger_type(lp->spi->irq);
1324 1325 1326 1327
	if (irq_type == IRQ_TYPE_EDGE_RISING ||
	    irq_type == IRQ_TYPE_EDGE_FALLING)
		dev_warn(&lp->spi->dev,
			 "Using edge triggered irq's are not recommended!\n");
1328 1329
	if (irq_type == IRQ_TYPE_EDGE_FALLING ||
	    irq_type == IRQ_TYPE_LEVEL_LOW)
1330 1331
		irq_pol = IRQ_ACTIVE_LOW;

1332
	rc = at86rf230_write_subreg(lp, SR_IRQ_POLARITY, irq_pol);
1333 1334 1335
	if (rc)
		return rc;

1336 1337 1338 1339
	rc = at86rf230_write_subreg(lp, SR_RX_SAFE_MODE, 1);
	if (rc)
		return rc;

1340
	rc = at86rf230_write_subreg(lp, SR_IRQ_MASK, IRQ_TRX_END);
1341 1342 1343
	if (rc)
		return rc;

1344 1345 1346 1347 1348
	/* reset values differs in at86rf231 and at86rf233 */
	rc = at86rf230_write_subreg(lp, SR_IRQ_MASK_MODE, 0);
	if (rc)
		return rc;

1349 1350 1351 1352 1353 1354 1355 1356
	get_random_bytes(csma_seed, ARRAY_SIZE(csma_seed));
	rc = at86rf230_write_subreg(lp, SR_CSMA_SEED_0, csma_seed[0]);
	if (rc)
		return rc;
	rc = at86rf230_write_subreg(lp, SR_CSMA_SEED_1, csma_seed[1]);
	if (rc)
		return rc;

1357 1358 1359 1360 1361 1362 1363 1364 1365 1366
	/* CLKM changes are applied immediately */
	rc = at86rf230_write_subreg(lp, SR_CLKM_SHA_SEL, 0x00);
	if (rc)
		return rc;

	/* Turn CLKM Off */
	rc = at86rf230_write_subreg(lp, SR_CLKM_CTRL, 0x00);
	if (rc)
		return rc;
	/* Wait the next SLEEP cycle */
1367 1368
	usleep_range(lp->data->t_sleep_cycle,
		     lp->data->t_sleep_cycle + 100);
1369

1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408
	/* xtal_trim value is calculated by:
	 * CL = 0.5 * (CX + CTRIM + CPAR)
	 *
	 * whereas:
	 * CL = capacitor of used crystal
	 * CX = connected capacitors at xtal pins
	 * CPAR = in all at86rf2xx datasheets this is a constant value 3 pF,
	 *	  but this is different on each board setup. You need to fine
	 *	  tuning this value via CTRIM.
	 * CTRIM = variable capacitor setting. Resolution is 0.3 pF range is
	 *	   0 pF upto 4.5 pF.
	 *
	 * Examples:
	 * atben transceiver:
	 *
	 * CL = 8 pF
	 * CX = 12 pF
	 * CPAR = 3 pF (We assume the magic constant from datasheet)
	 * CTRIM = 0.9 pF
	 *
	 * (12+0.9+3)/2 = 7.95 which is nearly at 8 pF
	 *
	 * xtal_trim = 0x3
	 *
	 * openlabs transceiver:
	 *
	 * CL = 16 pF
	 * CX = 22 pF
	 * CPAR = 3 pF (We assume the magic constant from datasheet)
	 * CTRIM = 4.5 pF
	 *
	 * (22+4.5+3)/2 = 14.75 which is the nearest value to 16 pF
	 *
	 * xtal_trim = 0xf
	 */
	rc = at86rf230_write_subreg(lp, SR_XTAL_TRIM, xtal_trim);
	if (rc)
		return rc;

1409
	rc = at86rf230_read_subreg(lp, SR_DVDD_OK, &dvdd);
1410 1411
	if (rc)
		return rc;
1412
	if (!dvdd) {
1413 1414 1415 1416
		dev_err(&lp->spi->dev, "DVDD error\n");
		return -EINVAL;
	}

1417 1418 1419 1420
	/* Force setting slotted operation bit to 0. Sometimes the atben
	 * sets this bit and I don't know why. We set this always force
	 * to zero while probing.
	 */
1421
	return at86rf230_write_subreg(lp, SR_SLOTTED_OPERATION, 0);
1422 1423
}

1424
static int
1425 1426
at86rf230_get_pdata(struct spi_device *spi, int *rstn, int *slp_tr,
		    u8 *xtal_trim)
1427
{
1428
	struct at86rf230_platform_data *pdata = spi->dev.platform_data;
1429
	int ret;
1430

1431 1432 1433
	if (!IS_ENABLED(CONFIG_OF) || !spi->dev.of_node) {
		if (!pdata)
			return -ENOENT;
1434

1435 1436
		*rstn = pdata->rstn;
		*slp_tr = pdata->slp_tr;
1437
		*xtal_trim = pdata->xtal_trim;
1438 1439
		return 0;
	}
1440

1441 1442
	*rstn = of_get_named_gpio(spi->dev.of_node, "reset-gpio", 0);
	*slp_tr = of_get_named_gpio(spi->dev.of_node, "sleep-gpio", 0);
1443 1444 1445
	ret = of_property_read_u8(spi->dev.of_node, "xtal-trim", xtal_trim);
	if (ret < 0 && ret != -EINVAL)
		return ret;
1446

1447
	return 0;
1448 1449
}

1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471
static int
at86rf230_detect_device(struct at86rf230_local *lp)
{
	unsigned int part, version, val;
	u16 man_id = 0;
	const char *chip;
	int rc;

	rc = __at86rf230_read(lp, RG_MAN_ID_0, &val);
	if (rc)
		return rc;
	man_id |= val;

	rc = __at86rf230_read(lp, RG_MAN_ID_1, &val);
	if (rc)
		return rc;
	man_id |= (val << 8);

	rc = __at86rf230_read(lp, RG_PART_NUM, &part);
	if (rc)
		return rc;

1472
	rc = __at86rf230_read(lp, RG_VERSION_NUM, &version);
1473 1474 1475 1476 1477 1478 1479 1480 1481
	if (rc)
		return rc;

	if (man_id != 0x001f) {
		dev_err(&lp->spi->dev, "Non-Atmel dev found (MAN_ID %02x %02x)\n",
			man_id >> 8, man_id & 0xFF);
		return -EINVAL;
	}

A
Alexander Aring 已提交
1482
	lp->hw->flags = IEEE802154_HW_TX_OMIT_CKSUM | IEEE802154_HW_AACK |
1483
			IEEE802154_HW_TXPOWER | IEEE802154_HW_ARET |
1484
			IEEE802154_HW_AFILT | IEEE802154_HW_PROMISCUOUS;
1485

1486 1487
	lp->hw->phy->cca.mode = NL802154_CCA_ENERGY;

1488 1489 1490 1491 1492 1493 1494
	switch (part) {
	case 2:
		chip = "at86rf230";
		rc = -ENOTSUPP;
		break;
	case 3:
		chip = "at86rf231";
1495
		lp->data = &at86rf231_data;
1496
		lp->hw->phy->channels_supported[0] = 0x7FFF800;
1497
		lp->hw->phy->current_channel = 11;
1498
		lp->hw->phy->symbol_duration = 16;
1499 1500 1501
		break;
	case 7:
		chip = "at86rf212";
1502 1503 1504 1505 1506 1507
		lp->data = &at86rf212_data;
		lp->hw->flags |= IEEE802154_HW_LBT;
		lp->hw->phy->channels_supported[0] = 0x00007FF;
		lp->hw->phy->channels_supported[2] = 0x00007FF;
		lp->hw->phy->current_channel = 5;
		lp->hw->phy->symbol_duration = 25;
1508 1509 1510
		break;
	case 11:
		chip = "at86rf233";
1511
		lp->data = &at86rf233_data;
1512
		lp->hw->phy->channels_supported[0] = 0x7FFF800;
1513
		lp->hw->phy->current_channel = 13;
1514
		lp->hw->phy->symbol_duration = 16;
1515 1516
		break;
	default:
1517
		chip = "unknown";
1518 1519 1520 1521 1522 1523 1524 1525 1526
		rc = -ENOTSUPP;
		break;
	}

	dev_info(&lp->spi->dev, "Detected %s chip version %d\n", chip, version);

	return rc;
}

1527 1528 1529
static void
at86rf230_setup_spi_messages(struct at86rf230_local *lp)
{
1530 1531 1532 1533 1534 1535 1536
	lp->state.lp = lp;
	spi_message_init(&lp->state.msg);
	lp->state.msg.context = &lp->state;
	lp->state.trx.tx_buf = lp->state.buf;
	lp->state.trx.rx_buf = lp->state.buf;
	spi_message_add_tail(&lp->state.trx, &lp->state.msg);

1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551
	lp->irq.lp = lp;
	spi_message_init(&lp->irq.msg);
	lp->irq.msg.context = &lp->irq;
	lp->irq.trx.tx_buf = lp->irq.buf;
	lp->irq.trx.rx_buf = lp->irq.buf;
	spi_message_add_tail(&lp->irq.trx, &lp->irq.msg);

	lp->tx.lp = lp;
	spi_message_init(&lp->tx.msg);
	lp->tx.msg.context = &lp->tx;
	lp->tx.trx.tx_buf = lp->tx.buf;
	lp->tx.trx.rx_buf = lp->tx.buf;
	spi_message_add_tail(&lp->tx.trx, &lp->tx.msg);
}

1552
static int at86rf230_probe(struct spi_device *spi)
1553
{
1554
	struct ieee802154_hw *hw;
1555
	struct at86rf230_local *lp;
A
Alexander Aring 已提交
1556
	unsigned int status;
1557
	int rc, irq_type, rstn, slp_tr;
1558
	u8 xtal_trim;
1559 1560 1561 1562 1563 1564

	if (!spi->irq) {
		dev_err(&spi->dev, "no IRQ specified\n");
		return -EINVAL;
	}

1565
	rc = at86rf230_get_pdata(spi, &rstn, &slp_tr, &xtal_trim);
1566 1567 1568
	if (rc < 0) {
		dev_err(&spi->dev, "failed to parse platform_data: %d\n", rc);
		return rc;
1569 1570
	}

1571 1572
	if (gpio_is_valid(rstn)) {
		rc = devm_gpio_request_one(&spi->dev, rstn,
1573
					   GPIOF_OUT_INIT_HIGH, "rstn");
1574 1575 1576
		if (rc)
			return rc;
	}
1577

1578 1579
	if (gpio_is_valid(slp_tr)) {
		rc = devm_gpio_request_one(&spi->dev, slp_tr,
1580
					   GPIOF_OUT_INIT_LOW, "slp_tr");
1581
		if (rc)
1582
			return rc;
1583 1584 1585
	}

	/* Reset */
1586
	if (gpio_is_valid(rstn)) {
1587
		udelay(1);
1588
		gpio_set_value(rstn, 0);
1589
		udelay(1);
1590
		gpio_set_value(rstn, 1);
1591 1592
		usleep_range(120, 240);
	}
1593

1594 1595
	hw = ieee802154_alloc_hw(sizeof(*lp), &at86rf230_ops);
	if (!hw)
1596 1597
		return -ENOMEM;

1598 1599
	lp = hw->priv;
	lp->hw = hw;
1600
	lp->spi = spi;
1601
	hw->parent = &spi->dev;
1602
	hw->vif_data_size = sizeof(*lp);
1603
	ieee802154_random_extended_addr(&hw->phy->perm_extended_addr);
1604

A
Alexander Aring 已提交
1605 1606 1607 1608 1609 1610 1611 1612
	lp->regmap = devm_regmap_init_spi(spi, &at86rf230_regmap_spi_config);
	if (IS_ERR(lp->regmap)) {
		rc = PTR_ERR(lp->regmap);
		dev_err(&spi->dev, "Failed to allocate register map: %d\n",
			rc);
		goto free_dev;
	}

1613 1614
	at86rf230_setup_spi_messages(lp);

1615 1616 1617 1618
	rc = at86rf230_detect_device(lp);
	if (rc < 0)
		goto free_dev;

1619
	spin_lock_init(&lp->lock);
1620
	init_completion(&lp->state_complete);
1621 1622 1623

	spi_set_drvdata(spi, lp);

1624
	rc = at86rf230_hw_init(lp, xtal_trim);
1625
	if (rc)
1626
		goto free_dev;
1627

1628 1629
	/* Read irq status register to reset irq line */
	rc = at86rf230_read_subreg(lp, RG_IRQ_STATUS, 0xff, 0, &status);
1630
	if (rc)
1631
		goto free_dev;
1632

1633 1634 1635 1636 1637 1638
	irq_type = irq_get_trigger_type(spi->irq);
	if (!irq_type)
		irq_type = IRQF_TRIGGER_RISING;

	rc = devm_request_irq(&spi->dev, spi->irq, at86rf230_isr,
			      IRQF_SHARED | irq_type, dev_name(&spi->dev), lp);
1639
	if (rc)
1640
		goto free_dev;
1641

1642
	rc = ieee802154_register_hw(lp->hw);
1643
	if (rc)
1644
		goto free_dev;
1645 1646 1647

	return rc;

1648
free_dev:
1649
	ieee802154_free_hw(lp->hw);
1650

1651 1652 1653
	return rc;
}

1654
static int at86rf230_remove(struct spi_device *spi)
1655 1656 1657
{
	struct at86rf230_local *lp = spi_get_drvdata(spi);

1658 1659
	/* mask all at86rf230 irq's */
	at86rf230_write_subreg(lp, SR_IRQ_MASK, 0);
1660 1661
	ieee802154_unregister_hw(lp->hw);
	ieee802154_free_hw(lp->hw);
1662
	dev_dbg(&spi->dev, "unregistered at86rf230\n");
1663

1664 1665 1666
	return 0;
}

1667
static const struct of_device_id at86rf230_of_match[] = {
1668 1669 1670 1671 1672 1673
	{ .compatible = "atmel,at86rf230", },
	{ .compatible = "atmel,at86rf231", },
	{ .compatible = "atmel,at86rf233", },
	{ .compatible = "atmel,at86rf212", },
	{ },
};
1674
MODULE_DEVICE_TABLE(of, at86rf230_of_match);
1675

1676 1677 1678 1679 1680 1681 1682 1683 1684
static const struct spi_device_id at86rf230_device_id[] = {
	{ .name = "at86rf230", },
	{ .name = "at86rf231", },
	{ .name = "at86rf233", },
	{ .name = "at86rf212", },
	{ },
};
MODULE_DEVICE_TABLE(spi, at86rf230_device_id);

1685
static struct spi_driver at86rf230_driver = {
1686
	.id_table = at86rf230_device_id,
1687
	.driver = {
1688
		.of_match_table = of_match_ptr(at86rf230_of_match),
1689 1690 1691 1692
		.name	= "at86rf230",
		.owner	= THIS_MODULE,
	},
	.probe      = at86rf230_probe,
1693
	.remove     = at86rf230_remove,
1694 1695
};

1696
module_spi_driver(at86rf230_driver);
1697 1698 1699

MODULE_DESCRIPTION("AT86RF230 Transceiver Driver");
MODULE_LICENSE("GPL v2");