Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / drivers / net / ethernet / microchip / encx24j600-regmap.c
blob26b00e66d91228541f0af59fb00dbae35b5e7e24
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Register map access API - ENCX24J600 support
5 * Copyright 2015 Gridpoint
7 * Author: Jon Ringle <jringle@gridpoint.com>
8 */
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,
21 int bank)
23 int ret = 0;
24 int bank_opcode = BANK_SELECT(bank);
26 ret = spi_write(ctx->spi, &bank_opcode, 1);
27 if (ret == 0)
28 ctx->bank = bank;
30 return ret;
33 static int encx24j600_cmdn(struct encx24j600_context *ctx, u8 opcode,
34 const void *buf, size_t len)
36 struct spi_message m;
37 struct spi_transfer t[2] = { { .tx_buf = &opcode, .len = 1, },
38 { .tx_buf = buf, .len = len }, };
39 spi_message_init(&m);
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,
61 size_t len)
63 struct encx24j600_context *ctx = context;
64 u8 banked_reg = reg & ADDR_MASK;
65 u8 bank = ((reg & BANK_MASK) >> BANK_SHIFT);
66 u8 cmd = RCRU;
67 int ret = 0;
68 int i = 0;
69 u8 tx_buf[2];
71 if (reg < 0x80) {
72 cmd = RCRCODE | banked_reg;
73 if ((banked_reg < 0x16) && (ctx->bank != bank))
74 ret = encx24j600_switch_bank(ctx, bank);
75 if (unlikely(ret))
76 return ret;
77 } else {
78 /* Translate registers that are more efficient using
79 * 3-byte SPI commands
81 switch (reg) {
82 case EGPRDPT:
83 cmd = RGPRDPT; break;
84 case EGPWRPT:
85 cmd = RGPWRPT; break;
86 case ERXRDPT:
87 cmd = RRXRDPT; break;
88 case ERXWRPT:
89 cmd = RRXWRPT; break;
90 case EUDARDPT:
91 cmd = RUDARDPT; break;
92 case EUDAWRPT:
93 cmd = RUDAWRPT; break;
94 case EGPDATA:
95 case ERXDATA:
96 case EUDADATA:
97 default:
98 return -EINVAL;
102 tx_buf[i++] = cmd;
103 if (cmd == RCRU)
104 tx_buf[i++] = reg;
106 ret = spi_write_then_read(ctx->spi, tx_buf, i, val, len);
108 return ret;
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 = &reg, .len = sizeof(reg), },
121 { .tx_buf = val, .len = len }, };
123 if (reg < 0x80) {
124 int ret = 0;
126 cmd = banked_code | banked_reg;
127 if ((banked_reg < 0x16) && (ctx->bank != bank))
128 ret = encx24j600_switch_bank(ctx, bank);
129 if (unlikely(ret))
130 return ret;
131 } else {
132 /* Translate registers that are more efficient using
133 * 3-byte SPI commands
135 switch (reg) {
136 case EGPRDPT:
137 cmd = WGPRDPT; break;
138 case EGPWRPT:
139 cmd = WGPWRPT; break;
140 case ERXRDPT:
141 cmd = WRXRDPT; break;
142 case ERXWRPT:
143 cmd = WRXWRPT; break;
144 case EUDARDPT:
145 cmd = WUDARDPT; break;
146 case EUDAWRPT:
147 cmd = WUDAWRPT; break;
148 case EGPDATA:
149 case ERXDATA:
150 case EUDADATA:
151 default:
152 return -EINVAL;
156 spi_message_init(&m);
157 spi_message_add_tail(&t[0], &m);
159 if (cmd == unbanked_cmd) {
160 t[1].tx_buf = &reg;
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,
169 size_t len)
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,
177 u8 reg, u8 val)
179 return regmap_encx24j600_sfr_update(ctx, reg, &val, 1, BFSU, BFSCODE);
182 static int regmap_encx24j600_sfr_clr_bits(struct encx24j600_context *ctx,
183 u8 reg, u8 val)
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,
189 unsigned int mask,
190 unsigned int val)
192 struct encx24j600_context *ctx = context;
194 int ret = 0;
195 unsigned int set_mask = mask & val;
196 unsigned int clr_mask = mask & ~val;
198 if ((reg >= 0x40 && reg < 0x6c) || reg >= 0x80)
199 return -EINVAL;
201 if (set_mask & 0xff)
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);
217 return ret;
220 int regmap_encx24j600_spi_write(void *context, u8 reg, const u8 *data,
221 size_t count)
223 struct encx24j600_context *ctx = context;
225 if (reg < 0xc0)
226 return encx24j600_cmdn(ctx, reg, data, count);
228 /* SPI 1-byte command. Ignore data */
229 return spi_write(ctx->spi, &reg, 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)
238 count = 1;
240 return spi_write_then_read(ctx->spi, &reg, sizeof(reg), data, count);
242 EXPORT_SYMBOL_GPL(regmap_encx24j600_spi_read);
244 static int regmap_encx24j600_write(void *context, const void *data,
245 size_t len)
247 u8 *dout = (u8 *)data;
248 u8 reg = dout[0];
249 ++dout;
250 --len;
252 if (reg > 0xa0)
253 return regmap_encx24j600_spi_write(context, reg, dout, len);
255 if (len > 2)
256 return -EINVAL;
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;
267 if (reg_size != 1) {
268 pr_err("%s: reg=%02x reg_size=%zu\n", __func__, reg, reg_size);
269 return -EINVAL;
272 if (reg > 0xa0)
273 return regmap_encx24j600_spi_read(context, reg, val, val_size);
275 if (val_size > 2) {
276 pr_err("%s: reg=%02x val_size=%zu\n", __func__, reg, val_size);
277 return -EINVAL;
280 return regmap_encx24j600_sfr_read(context, reg, val, val_size);
283 static bool encx24j600_regmap_readable(struct device *dev, unsigned int reg)
285 if ((reg < 0x36) ||
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)) ||
291 (reg == 0xc8))
292 return true;
293 else
294 return false;
297 static bool encx24j600_regmap_writeable(struct device *dev, unsigned int reg)
299 if ((reg < 0x12) ||
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)))
309 return true;
310 else
311 return false;
314 static bool encx24j600_regmap_volatile(struct device *dev, unsigned int reg)
316 switch (reg) {
317 case ERXHEAD:
318 case EDMACS:
319 case ETXSTAT:
320 case ETXWIRE:
321 case ECON1: /* Can be modified via single byte cmds */
322 case ECON2: /* Can be modified via single byte cmds */
323 case ESTAT:
324 case EIR: /* Can be modified via single byte cmds */
325 case MIRD:
326 case MISTAT:
327 return true;
328 default:
329 break;
332 return false;
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)))
340 return true;
341 else
342 return false;
345 static int regmap_encx24j600_phy_reg_read(void *context, unsigned int reg,
346 unsigned int *val)
348 struct encx24j600_context *ctx = context;
349 int ret;
350 unsigned int mistat;
352 reg = MIREGADR_VAL | (reg & PHREG_MASK);
353 ret = regmap_write(ctx->regmap, MIREGADR, reg);
354 if (unlikely(ret))
355 goto err_out;
357 ret = regmap_write(ctx->regmap, MICMD, MIIRD);
358 if (unlikely(ret))
359 goto err_out;
361 usleep_range(26, 100);
362 while (((ret = regmap_read(ctx->regmap, MISTAT, &mistat)) == 0) &&
363 (mistat & BUSY))
364 cpu_relax();
366 if (unlikely(ret))
367 goto err_out;
369 ret = regmap_write(ctx->regmap, MICMD, 0);
370 if (unlikely(ret))
371 goto err_out;
373 ret = regmap_read(ctx->regmap, MIRD, val);
375 err_out:
376 if (ret)
377 pr_err("%s: error %d reading reg %02x\n", __func__, ret,
378 reg & PHREG_MASK);
380 return ret;
383 static int regmap_encx24j600_phy_reg_write(void *context, unsigned int reg,
384 unsigned int val)
386 struct encx24j600_context *ctx = context;
387 int ret;
388 unsigned int mistat;
390 reg = MIREGADR_VAL | (reg & PHREG_MASK);
391 ret = regmap_write(ctx->regmap, MIREGADR, reg);
392 if (unlikely(ret))
393 goto err_out;
395 ret = regmap_write(ctx->regmap, MIWR, val);
396 if (unlikely(ret))
397 goto err_out;
399 usleep_range(26, 100);
400 while (((ret = regmap_read(ctx->regmap, MISTAT, &mistat)) == 0) &&
401 (mistat & BUSY))
402 cpu_relax();
404 err_out:
405 if (ret)
406 pr_err("%s: error %d writing reg %02x=%04x\n", __func__, ret,
407 reg & PHREG_MASK, val);
409 return ret;
412 static bool encx24j600_phymap_readable(struct device *dev, unsigned int reg)
414 switch (reg) {
415 case PHCON1:
416 case PHSTAT1:
417 case PHANA:
418 case PHANLPA:
419 case PHANE:
420 case PHCON2:
421 case PHSTAT2:
422 case PHSTAT3:
423 return true;
424 default:
425 return false;
429 static bool encx24j600_phymap_writeable(struct device *dev, unsigned int reg)
431 switch (reg) {
432 case PHCON1:
433 case PHCON2:
434 case PHANA:
435 return true;
436 case PHSTAT1:
437 case PHSTAT2:
438 case PHSTAT3:
439 case PHANLPA:
440 case PHANE:
441 default:
442 return false;
446 static bool encx24j600_phymap_volatile(struct device *dev, unsigned int reg)
448 switch (reg) {
449 case PHSTAT1:
450 case PHSTAT2:
451 case PHSTAT3:
452 case PHANLPA:
453 case PHANE:
454 case PHCON2:
455 return true;
456 default:
457 return false;
461 static struct regmap_config regcfg = {
462 .name = "reg",
463 .reg_bits = 8,
464 .val_bits = 16,
465 .max_register = 0xee,
466 .reg_stride = 2,
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 = {
484 .name = "phy",
485 .reg_bits = 8,
486 .val_bits = 16,
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, &regmap_encx24j600, ctx, &regcfg);
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);
512 return 0;
514 EXPORT_SYMBOL_GPL(devm_regmap_init_encx24j600);
516 MODULE_DESCRIPTION("Microchip ENCX24J600 helpers");
517 MODULE_LICENSE("GPL");