1 // SPDX-License-Identifier: GPL-2.0-only
3 * Register map access API - ENCX24J600 support
5 * Copyright 2015 Gridpoint
7 * Author: Jon Ringle <jringle@gridpoint.com>
10 #include <linux/delay.h>
11 #include <linux/errno.h>
12 #include <linux/init.h>
13 #include <linux/module.h>
14 #include <linux/netdevice.h>
15 #include <linux/regmap.h>
16 #include <linux/spi/spi.h>
18 #include "encx24j600_hw.h"
20 static int encx24j600_switch_bank(struct encx24j600_context
*ctx
,
24 int bank_opcode
= BANK_SELECT(bank
);
26 ret
= spi_write(ctx
->spi
, &bank_opcode
, 1);
33 static int encx24j600_cmdn(struct encx24j600_context
*ctx
, u8 opcode
,
34 const void *buf
, size_t len
)
37 struct spi_transfer t
[2] = { { .tx_buf
= &opcode
, .len
= 1, },
38 { .tx_buf
= buf
, .len
= len
}, };
40 spi_message_add_tail(&t
[0], &m
);
41 spi_message_add_tail(&t
[1], &m
);
43 return spi_sync(ctx
->spi
, &m
);
46 static void regmap_lock_mutex(void *context
)
48 struct encx24j600_context
*ctx
= context
;
50 mutex_lock(&ctx
->mutex
);
53 static void regmap_unlock_mutex(void *context
)
55 struct encx24j600_context
*ctx
= context
;
57 mutex_unlock(&ctx
->mutex
);
60 static int regmap_encx24j600_sfr_read(void *context
, u8 reg
, u8
*val
,
63 struct encx24j600_context
*ctx
= context
;
64 u8 banked_reg
= reg
& ADDR_MASK
;
65 u8 bank
= ((reg
& BANK_MASK
) >> BANK_SHIFT
);
72 cmd
= RCRCODE
| banked_reg
;
73 if ((banked_reg
< 0x16) && (ctx
->bank
!= bank
))
74 ret
= encx24j600_switch_bank(ctx
, bank
);
78 /* Translate registers that are more efficient using
91 cmd
= RUDARDPT
; break;
93 cmd
= RUDAWRPT
; break;
106 ret
= spi_write_then_read(ctx
->spi
, tx_buf
, i
, val
, len
);
111 static int regmap_encx24j600_sfr_update(struct encx24j600_context
*ctx
,
112 u8 reg
, u8
*val
, size_t len
,
113 u8 unbanked_cmd
, u8 banked_code
)
115 u8 banked_reg
= reg
& ADDR_MASK
;
116 u8 bank
= ((reg
& BANK_MASK
) >> BANK_SHIFT
);
117 u8 cmd
= unbanked_cmd
;
118 struct spi_message m
;
119 struct spi_transfer t
[3] = { { .tx_buf
= &cmd
, .len
= sizeof(cmd
), },
120 { .tx_buf
= ®
, .len
= sizeof(reg
), },
121 { .tx_buf
= val
, .len
= len
}, };
126 cmd
= banked_code
| banked_reg
;
127 if ((banked_reg
< 0x16) && (ctx
->bank
!= bank
))
128 ret
= encx24j600_switch_bank(ctx
, bank
);
132 /* Translate registers that are more efficient using
133 * 3-byte SPI commands
137 cmd
= WGPRDPT
; break;
139 cmd
= WGPWRPT
; break;
141 cmd
= WRXRDPT
; break;
143 cmd
= WRXWRPT
; break;
145 cmd
= WUDARDPT
; break;
147 cmd
= WUDAWRPT
; break;
156 spi_message_init(&m
);
157 spi_message_add_tail(&t
[0], &m
);
159 if (cmd
== unbanked_cmd
) {
161 spi_message_add_tail(&t
[1], &m
);
164 spi_message_add_tail(&t
[2], &m
);
165 return spi_sync(ctx
->spi
, &m
);
168 static int regmap_encx24j600_sfr_write(void *context
, u8 reg
, u8
*val
,
171 struct encx24j600_context
*ctx
= context
;
173 return regmap_encx24j600_sfr_update(ctx
, reg
, val
, len
, WCRU
, WCRCODE
);
176 static int regmap_encx24j600_sfr_set_bits(struct encx24j600_context
*ctx
,
179 return regmap_encx24j600_sfr_update(ctx
, reg
, &val
, 1, BFSU
, BFSCODE
);
182 static int regmap_encx24j600_sfr_clr_bits(struct encx24j600_context
*ctx
,
185 return regmap_encx24j600_sfr_update(ctx
, reg
, &val
, 1, BFCU
, BFCCODE
);
188 static int regmap_encx24j600_reg_update_bits(void *context
, unsigned int reg
,
192 struct encx24j600_context
*ctx
= context
;
195 unsigned int set_mask
= mask
& val
;
196 unsigned int clr_mask
= mask
& ~val
;
198 if ((reg
>= 0x40 && reg
< 0x6c) || reg
>= 0x80)
202 ret
= regmap_encx24j600_sfr_set_bits(ctx
, reg
, set_mask
);
204 set_mask
= (set_mask
& 0xff00) >> 8;
206 if ((set_mask
& 0xff) && (ret
== 0))
207 ret
= regmap_encx24j600_sfr_set_bits(ctx
, reg
+ 1, set_mask
);
209 if ((clr_mask
& 0xff) && (ret
== 0))
210 ret
= regmap_encx24j600_sfr_clr_bits(ctx
, reg
, clr_mask
);
212 clr_mask
= (clr_mask
& 0xff00) >> 8;
214 if ((clr_mask
& 0xff) && (ret
== 0))
215 ret
= regmap_encx24j600_sfr_clr_bits(ctx
, reg
+ 1, clr_mask
);
220 int regmap_encx24j600_spi_write(void *context
, u8 reg
, const u8
*data
,
223 struct encx24j600_context
*ctx
= context
;
226 return encx24j600_cmdn(ctx
, reg
, data
, count
);
228 /* SPI 1-byte command. Ignore data */
229 return spi_write(ctx
->spi
, ®
, 1);
231 EXPORT_SYMBOL_GPL(regmap_encx24j600_spi_write
);
233 int regmap_encx24j600_spi_read(void *context
, u8 reg
, u8
*data
, size_t count
)
235 struct encx24j600_context
*ctx
= context
;
237 if (reg
== RBSEL
&& count
> 1)
240 return spi_write_then_read(ctx
->spi
, ®
, sizeof(reg
), data
, count
);
242 EXPORT_SYMBOL_GPL(regmap_encx24j600_spi_read
);
244 static int regmap_encx24j600_write(void *context
, const void *data
,
247 u8
*dout
= (u8
*)data
;
253 return regmap_encx24j600_spi_write(context
, reg
, dout
, len
);
258 return regmap_encx24j600_sfr_write(context
, reg
, dout
, len
);
261 static int regmap_encx24j600_read(void *context
,
262 const void *reg_buf
, size_t reg_size
,
263 void *val
, size_t val_size
)
265 u8 reg
= *(const u8
*)reg_buf
;
268 pr_err("%s: reg=%02x reg_size=%zu\n", __func__
, reg
, reg_size
);
273 return regmap_encx24j600_spi_read(context
, reg
, val
, val_size
);
276 pr_err("%s: reg=%02x val_size=%zu\n", __func__
, reg
, val_size
);
280 return regmap_encx24j600_sfr_read(context
, reg
, val
, val_size
);
283 static bool encx24j600_regmap_readable(struct device
*dev
, unsigned int reg
)
286 ((reg
>= 0x40) && (reg
< 0x4c)) ||
287 ((reg
>= 0x52) && (reg
< 0x56)) ||
288 ((reg
>= 0x60) && (reg
< 0x66)) ||
289 ((reg
>= 0x68) && (reg
< 0x80)) ||
290 ((reg
>= 0x86) && (reg
< 0x92)) ||
297 static bool encx24j600_regmap_writeable(struct device
*dev
, unsigned int reg
)
300 ((reg
>= 0x14) && (reg
< 0x1a)) ||
301 ((reg
>= 0x1c) && (reg
< 0x36)) ||
302 ((reg
>= 0x40) && (reg
< 0x4c)) ||
303 ((reg
>= 0x52) && (reg
< 0x56)) ||
304 ((reg
>= 0x60) && (reg
< 0x68)) ||
305 ((reg
>= 0x6c) && (reg
< 0x80)) ||
306 ((reg
>= 0x86) && (reg
< 0x92)) ||
307 ((reg
>= 0xc0) && (reg
< 0xc8)) ||
308 ((reg
>= 0xca) && (reg
< 0xf0)))
314 static bool encx24j600_regmap_volatile(struct device
*dev
, unsigned int reg
)
321 case ECON1
: /* Can be modified via single byte cmds */
322 case ECON2
: /* Can be modified via single byte cmds */
324 case EIR
: /* Can be modified via single byte cmds */
335 static bool encx24j600_regmap_precious(struct device
*dev
, unsigned int reg
)
337 /* single byte cmds are precious */
338 if (((reg
>= 0xc0) && (reg
< 0xc8)) ||
339 ((reg
>= 0xca) && (reg
< 0xf0)))
345 static int regmap_encx24j600_phy_reg_read(void *context
, unsigned int reg
,
348 struct encx24j600_context
*ctx
= context
;
352 reg
= MIREGADR_VAL
| (reg
& PHREG_MASK
);
353 ret
= regmap_write(ctx
->regmap
, MIREGADR
, reg
);
357 ret
= regmap_write(ctx
->regmap
, MICMD
, MIIRD
);
361 usleep_range(26, 100);
362 while (((ret
= regmap_read(ctx
->regmap
, MISTAT
, &mistat
)) == 0) &&
369 ret
= regmap_write(ctx
->regmap
, MICMD
, 0);
373 ret
= regmap_read(ctx
->regmap
, MIRD
, val
);
377 pr_err("%s: error %d reading reg %02x\n", __func__
, ret
,
383 static int regmap_encx24j600_phy_reg_write(void *context
, unsigned int reg
,
386 struct encx24j600_context
*ctx
= context
;
390 reg
= MIREGADR_VAL
| (reg
& PHREG_MASK
);
391 ret
= regmap_write(ctx
->regmap
, MIREGADR
, reg
);
395 ret
= regmap_write(ctx
->regmap
, MIWR
, val
);
399 usleep_range(26, 100);
400 while (((ret
= regmap_read(ctx
->regmap
, MISTAT
, &mistat
)) == 0) &&
406 pr_err("%s: error %d writing reg %02x=%04x\n", __func__
, ret
,
407 reg
& PHREG_MASK
, val
);
412 static bool encx24j600_phymap_readable(struct device
*dev
, unsigned int reg
)
429 static bool encx24j600_phymap_writeable(struct device
*dev
, unsigned int reg
)
446 static bool encx24j600_phymap_volatile(struct device
*dev
, unsigned int reg
)
461 static struct regmap_config regcfg
= {
465 .max_register
= 0xee,
467 .cache_type
= REGCACHE_MAPLE
,
468 .val_format_endian
= REGMAP_ENDIAN_LITTLE
,
469 .readable_reg
= encx24j600_regmap_readable
,
470 .writeable_reg
= encx24j600_regmap_writeable
,
471 .volatile_reg
= encx24j600_regmap_volatile
,
472 .precious_reg
= encx24j600_regmap_precious
,
473 .lock
= regmap_lock_mutex
,
474 .unlock
= regmap_unlock_mutex
,
477 static const struct regmap_bus regmap_encx24j600
= {
478 .write
= regmap_encx24j600_write
,
479 .read
= regmap_encx24j600_read
,
480 .reg_update_bits
= regmap_encx24j600_reg_update_bits
,
483 static const struct regmap_config phycfg
= {
487 .max_register
= 0x1f,
488 .cache_type
= REGCACHE_MAPLE
,
489 .val_format_endian
= REGMAP_ENDIAN_LITTLE
,
490 .readable_reg
= encx24j600_phymap_readable
,
491 .writeable_reg
= encx24j600_phymap_writeable
,
492 .volatile_reg
= encx24j600_phymap_volatile
,
495 static const struct regmap_bus phymap_encx24j600
= {
496 .reg_write
= regmap_encx24j600_phy_reg_write
,
497 .reg_read
= regmap_encx24j600_phy_reg_read
,
500 int devm_regmap_init_encx24j600(struct device
*dev
,
501 struct encx24j600_context
*ctx
)
503 mutex_init(&ctx
->mutex
);
504 regcfg
.lock_arg
= ctx
;
505 ctx
->regmap
= devm_regmap_init(dev
, ®map_encx24j600
, ctx
, ®cfg
);
506 if (IS_ERR(ctx
->regmap
))
507 return PTR_ERR(ctx
->regmap
);
508 ctx
->phymap
= devm_regmap_init(dev
, &phymap_encx24j600
, ctx
, &phycfg
);
509 if (IS_ERR(ctx
->phymap
))
510 return PTR_ERR(ctx
->phymap
);
514 EXPORT_SYMBOL_GPL(devm_regmap_init_encx24j600
);
516 MODULE_DESCRIPTION("Microchip ENCX24J600 helpers");
517 MODULE_LICENSE("GPL");