2 * Register map access API - ENCX24J600 support
4 * Copyright 2015 Gridpoint
6 * Author: Jon Ringle <jringle@gridpoint.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
13 #include <linux/delay.h>
14 #include <linux/errno.h>
15 #include <linux/init.h>
16 #include <linux/module.h>
17 #include <linux/netdevice.h>
18 #include <linux/regmap.h>
19 #include <linux/spi/spi.h>
21 #include "encx24j600_hw.h"
23 static inline bool is_bits_set(int value
, int mask
)
25 return (value
& mask
) == mask
;
28 static int encx24j600_switch_bank(struct encx24j600_context
*ctx
,
32 int bank_opcode
= BANK_SELECT(bank
);
34 ret
= spi_write(ctx
->spi
, &bank_opcode
, 1);
41 static int encx24j600_cmdn(struct encx24j600_context
*ctx
, u8 opcode
,
42 const void *buf
, size_t len
)
45 struct spi_transfer t
[2] = { { .tx_buf
= &opcode
, .len
= 1, },
46 { .tx_buf
= buf
, .len
= len
}, };
48 spi_message_add_tail(&t
[0], &m
);
49 spi_message_add_tail(&t
[1], &m
);
51 return spi_sync(ctx
->spi
, &m
);
54 static void regmap_lock_mutex(void *context
)
56 struct encx24j600_context
*ctx
= context
;
58 mutex_lock(&ctx
->mutex
);
61 static void regmap_unlock_mutex(void *context
)
63 struct encx24j600_context
*ctx
= context
;
65 mutex_unlock(&ctx
->mutex
);
68 static int regmap_encx24j600_sfr_read(void *context
, u8 reg
, u8
*val
,
71 struct encx24j600_context
*ctx
= context
;
72 u8 banked_reg
= reg
& ADDR_MASK
;
73 u8 bank
= ((reg
& BANK_MASK
) >> BANK_SHIFT
);
80 cmd
= RCRCODE
| banked_reg
;
81 if ((banked_reg
< 0x16) && (ctx
->bank
!= bank
))
82 ret
= encx24j600_switch_bank(ctx
, bank
);
86 /* Translate registers that are more effecient using
99 cmd
= RUDARDPT
; break;
101 cmd
= RUDAWRPT
; break;
114 ret
= spi_write_then_read(ctx
->spi
, tx_buf
, i
, val
, len
);
119 static int regmap_encx24j600_sfr_update(struct encx24j600_context
*ctx
,
120 u8 reg
, u8
*val
, size_t len
,
121 u8 unbanked_cmd
, u8 banked_code
)
123 u8 banked_reg
= reg
& ADDR_MASK
;
124 u8 bank
= ((reg
& BANK_MASK
) >> BANK_SHIFT
);
125 u8 cmd
= unbanked_cmd
;
126 struct spi_message m
;
127 struct spi_transfer t
[3] = { { .tx_buf
= &cmd
, .len
= sizeof(cmd
), },
128 { .tx_buf
= ®
, .len
= sizeof(reg
), },
129 { .tx_buf
= val
, .len
= len
}, };
134 cmd
= banked_code
| banked_reg
;
135 if ((banked_reg
< 0x16) && (ctx
->bank
!= bank
))
136 ret
= encx24j600_switch_bank(ctx
, bank
);
140 /* Translate registers that are more effecient using
141 * 3-byte SPI commands
145 cmd
= WGPRDPT
; break;
147 cmd
= WGPWRPT
; break;
149 cmd
= WRXRDPT
; break;
151 cmd
= WRXWRPT
; break;
153 cmd
= WUDARDPT
; break;
155 cmd
= WUDAWRPT
; break;
164 spi_message_init(&m
);
165 spi_message_add_tail(&t
[0], &m
);
167 if (cmd
== unbanked_cmd
) {
169 spi_message_add_tail(&t
[1], &m
);
172 spi_message_add_tail(&t
[2], &m
);
173 return spi_sync(ctx
->spi
, &m
);
176 static int regmap_encx24j600_sfr_write(void *context
, u8 reg
, u8
*val
,
179 struct encx24j600_context
*ctx
= context
;
181 return regmap_encx24j600_sfr_update(ctx
, reg
, val
, len
, WCRU
, WCRCODE
);
184 static int regmap_encx24j600_sfr_set_bits(struct encx24j600_context
*ctx
,
187 return regmap_encx24j600_sfr_update(ctx
, reg
, &val
, 1, BFSU
, BFSCODE
);
190 static int regmap_encx24j600_sfr_clr_bits(struct encx24j600_context
*ctx
,
193 return regmap_encx24j600_sfr_update(ctx
, reg
, &val
, 1, BFCU
, BFCCODE
);
196 static int regmap_encx24j600_reg_update_bits(void *context
, unsigned int reg
,
200 struct encx24j600_context
*ctx
= context
;
203 unsigned int set_mask
= mask
& val
;
204 unsigned int clr_mask
= mask
& ~val
;
206 if ((reg
>= 0x40 && reg
< 0x6c) || reg
>= 0x80)
210 ret
= regmap_encx24j600_sfr_set_bits(ctx
, reg
, set_mask
);
212 set_mask
= (set_mask
& 0xff00) >> 8;
214 if ((set_mask
& 0xff) && (ret
== 0))
215 ret
= regmap_encx24j600_sfr_set_bits(ctx
, reg
+ 1, set_mask
);
217 if ((clr_mask
& 0xff) && (ret
== 0))
218 ret
= regmap_encx24j600_sfr_clr_bits(ctx
, reg
, clr_mask
);
220 clr_mask
= (clr_mask
& 0xff00) >> 8;
222 if ((clr_mask
& 0xff) && (ret
== 0))
223 ret
= regmap_encx24j600_sfr_clr_bits(ctx
, reg
+ 1, clr_mask
);
228 int regmap_encx24j600_spi_write(void *context
, u8 reg
, const u8
*data
,
231 struct encx24j600_context
*ctx
= context
;
234 return encx24j600_cmdn(ctx
, reg
, data
, count
);
236 /* SPI 1-byte command. Ignore data */
237 return spi_write(ctx
->spi
, ®
, 1);
239 EXPORT_SYMBOL_GPL(regmap_encx24j600_spi_write
);
241 int regmap_encx24j600_spi_read(void *context
, u8 reg
, u8
*data
, size_t count
)
243 struct encx24j600_context
*ctx
= context
;
245 if (reg
== RBSEL
&& count
> 1)
248 return spi_write_then_read(ctx
->spi
, ®
, sizeof(reg
), data
, count
);
250 EXPORT_SYMBOL_GPL(regmap_encx24j600_spi_read
);
252 static int regmap_encx24j600_write(void *context
, const void *data
,
255 u8
*dout
= (u8
*)data
;
261 return regmap_encx24j600_spi_write(context
, reg
, dout
, len
);
266 return regmap_encx24j600_sfr_write(context
, reg
, dout
, len
);
269 static int regmap_encx24j600_read(void *context
,
270 const void *reg_buf
, size_t reg_size
,
271 void *val
, size_t val_size
)
273 u8 reg
= *(const u8
*)reg_buf
;
276 pr_err("%s: reg=%02x reg_size=%zu\n", __func__
, reg
, reg_size
);
281 return regmap_encx24j600_spi_read(context
, reg
, val
, val_size
);
284 pr_err("%s: reg=%02x val_size=%zu\n", __func__
, reg
, val_size
);
288 return regmap_encx24j600_sfr_read(context
, reg
, val
, val_size
);
291 static bool encx24j600_regmap_readable(struct device
*dev
, unsigned int reg
)
294 ((reg
>= 0x40) && (reg
< 0x4c)) ||
295 ((reg
>= 0x52) && (reg
< 0x56)) ||
296 ((reg
>= 0x60) && (reg
< 0x66)) ||
297 ((reg
>= 0x68) && (reg
< 0x80)) ||
298 ((reg
>= 0x86) && (reg
< 0x92)) ||
305 static bool encx24j600_regmap_writeable(struct device
*dev
, unsigned int reg
)
308 ((reg
>= 0x14) && (reg
< 0x1a)) ||
309 ((reg
>= 0x1c) && (reg
< 0x36)) ||
310 ((reg
>= 0x40) && (reg
< 0x4c)) ||
311 ((reg
>= 0x52) && (reg
< 0x56)) ||
312 ((reg
>= 0x60) && (reg
< 0x68)) ||
313 ((reg
>= 0x6c) && (reg
< 0x80)) ||
314 ((reg
>= 0x86) && (reg
< 0x92)) ||
315 ((reg
>= 0xc0) && (reg
< 0xc8)) ||
316 ((reg
>= 0xca) && (reg
< 0xf0)))
322 static bool encx24j600_regmap_volatile(struct device
*dev
, unsigned int reg
)
329 case ECON1
: /* Can be modified via single byte cmds */
330 case ECON2
: /* Can be modified via single byte cmds */
332 case EIR
: /* Can be modified via single byte cmds */
343 static bool encx24j600_regmap_precious(struct device
*dev
, unsigned int reg
)
345 /* single byte cmds are precious */
346 if (((reg
>= 0xc0) && (reg
< 0xc8)) ||
347 ((reg
>= 0xca) && (reg
< 0xf0)))
353 static int regmap_encx24j600_phy_reg_read(void *context
, unsigned int reg
,
356 struct encx24j600_context
*ctx
= context
;
360 reg
= MIREGADR_VAL
| (reg
& PHREG_MASK
);
361 ret
= regmap_write(ctx
->regmap
, MIREGADR
, reg
);
365 ret
= regmap_write(ctx
->regmap
, MICMD
, MIIRD
);
369 usleep_range(26, 100);
370 while ((ret
= regmap_read(ctx
->regmap
, MISTAT
, &mistat
) != 0) &&
377 ret
= regmap_write(ctx
->regmap
, MICMD
, 0);
381 ret
= regmap_read(ctx
->regmap
, MIRD
, val
);
385 pr_err("%s: error %d reading reg %02x\n", __func__
, ret
,
391 static int regmap_encx24j600_phy_reg_write(void *context
, unsigned int reg
,
394 struct encx24j600_context
*ctx
= context
;
398 reg
= MIREGADR_VAL
| (reg
& PHREG_MASK
);
399 ret
= regmap_write(ctx
->regmap
, MIREGADR
, reg
);
403 ret
= regmap_write(ctx
->regmap
, MIWR
, val
);
407 usleep_range(26, 100);
408 while ((ret
= regmap_read(ctx
->regmap
, MISTAT
, &mistat
) != 0) &&
414 pr_err("%s: error %d writing reg %02x=%04x\n", __func__
, ret
,
415 reg
& PHREG_MASK
, val
);
420 static bool encx24j600_phymap_readable(struct device
*dev
, unsigned int reg
)
437 static bool encx24j600_phymap_writeable(struct device
*dev
, unsigned int reg
)
454 static bool encx24j600_phymap_volatile(struct device
*dev
, unsigned int reg
)
469 static struct regmap_config regcfg
= {
473 .max_register
= 0xee,
475 .cache_type
= REGCACHE_RBTREE
,
476 .val_format_endian
= REGMAP_ENDIAN_LITTLE
,
477 .readable_reg
= encx24j600_regmap_readable
,
478 .writeable_reg
= encx24j600_regmap_writeable
,
479 .volatile_reg
= encx24j600_regmap_volatile
,
480 .precious_reg
= encx24j600_regmap_precious
,
481 .lock
= regmap_lock_mutex
,
482 .unlock
= regmap_unlock_mutex
,
485 static struct regmap_bus regmap_encx24j600
= {
486 .write
= regmap_encx24j600_write
,
487 .read
= regmap_encx24j600_read
,
488 .reg_update_bits
= regmap_encx24j600_reg_update_bits
,
491 static struct regmap_config phycfg
= {
495 .max_register
= 0x1f,
496 .cache_type
= REGCACHE_RBTREE
,
497 .val_format_endian
= REGMAP_ENDIAN_LITTLE
,
498 .readable_reg
= encx24j600_phymap_readable
,
499 .writeable_reg
= encx24j600_phymap_writeable
,
500 .volatile_reg
= encx24j600_phymap_volatile
,
503 static struct regmap_bus phymap_encx24j600
= {
504 .reg_write
= regmap_encx24j600_phy_reg_write
,
505 .reg_read
= regmap_encx24j600_phy_reg_read
,
508 void devm_regmap_init_encx24j600(struct device
*dev
,
509 struct encx24j600_context
*ctx
)
511 mutex_init(&ctx
->mutex
);
512 regcfg
.lock_arg
= ctx
;
513 ctx
->regmap
= devm_regmap_init(dev
, ®map_encx24j600
, ctx
, ®cfg
);
514 ctx
->phymap
= devm_regmap_init(dev
, &phymap_encx24j600
, ctx
, &phycfg
);
516 EXPORT_SYMBOL_GPL(devm_regmap_init_encx24j600
);
518 MODULE_LICENSE("GPL");