1 // SPDX-License-Identifier: GPL-2.0
3 * SPI driver for Micrel/Kendin KS8995M and KSZ8864RMN ethernet switches
5 * Copyright (C) 2008 Gabor Juhos <juhosg at openwrt.org>
7 * This file was based on: drivers/spi/at25.c
8 * Copyright (C) 2006 David Brownell
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13 #include <linux/types.h>
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/delay.h>
17 #include <linux/device.h>
18 #include <linux/gpio/consumer.h>
21 #include <linux/spi/spi.h>
23 #define DRV_VERSION "0.1.1"
24 #define DRV_DESC "Micrel KS8995 Ethernet switch SPI driver"
26 /* ------------------------------------------------------------------------ */
28 #define KS8995_REG_ID0 0x00 /* Chip ID0 */
29 #define KS8995_REG_ID1 0x01 /* Chip ID1 */
31 #define KS8995_REG_GC0 0x02 /* Global Control 0 */
32 #define KS8995_REG_GC1 0x03 /* Global Control 1 */
33 #define KS8995_REG_GC2 0x04 /* Global Control 2 */
34 #define KS8995_REG_GC3 0x05 /* Global Control 3 */
35 #define KS8995_REG_GC4 0x06 /* Global Control 4 */
36 #define KS8995_REG_GC5 0x07 /* Global Control 5 */
37 #define KS8995_REG_GC6 0x08 /* Global Control 6 */
38 #define KS8995_REG_GC7 0x09 /* Global Control 7 */
39 #define KS8995_REG_GC8 0x0a /* Global Control 8 */
40 #define KS8995_REG_GC9 0x0b /* Global Control 9 */
42 #define KS8995_REG_PC(p, r) ((0x10 * p) + r) /* Port Control */
43 #define KS8995_REG_PS(p, r) ((0x10 * p) + r + 0xe) /* Port Status */
45 #define KS8995_REG_TPC0 0x60 /* TOS Priority Control 0 */
46 #define KS8995_REG_TPC1 0x61 /* TOS Priority Control 1 */
47 #define KS8995_REG_TPC2 0x62 /* TOS Priority Control 2 */
48 #define KS8995_REG_TPC3 0x63 /* TOS Priority Control 3 */
49 #define KS8995_REG_TPC4 0x64 /* TOS Priority Control 4 */
50 #define KS8995_REG_TPC5 0x65 /* TOS Priority Control 5 */
51 #define KS8995_REG_TPC6 0x66 /* TOS Priority Control 6 */
52 #define KS8995_REG_TPC7 0x67 /* TOS Priority Control 7 */
54 #define KS8995_REG_MAC0 0x68 /* MAC address 0 */
55 #define KS8995_REG_MAC1 0x69 /* MAC address 1 */
56 #define KS8995_REG_MAC2 0x6a /* MAC address 2 */
57 #define KS8995_REG_MAC3 0x6b /* MAC address 3 */
58 #define KS8995_REG_MAC4 0x6c /* MAC address 4 */
59 #define KS8995_REG_MAC5 0x6d /* MAC address 5 */
61 #define KS8995_REG_IAC0 0x6e /* Indirect Access Control 0 */
62 #define KS8995_REG_IAC1 0x6f /* Indirect Access Control 0 */
63 #define KS8995_REG_IAD7 0x70 /* Indirect Access Data 7 */
64 #define KS8995_REG_IAD6 0x71 /* Indirect Access Data 6 */
65 #define KS8995_REG_IAD5 0x72 /* Indirect Access Data 5 */
66 #define KS8995_REG_IAD4 0x73 /* Indirect Access Data 4 */
67 #define KS8995_REG_IAD3 0x74 /* Indirect Access Data 3 */
68 #define KS8995_REG_IAD2 0x75 /* Indirect Access Data 2 */
69 #define KS8995_REG_IAD1 0x76 /* Indirect Access Data 1 */
70 #define KS8995_REG_IAD0 0x77 /* Indirect Access Data 0 */
72 #define KSZ8864_REG_ID1 0xfe /* Chip ID in bit 7 */
74 #define KS8995_REGS_SIZE 0x80
75 #define KSZ8864_REGS_SIZE 0x100
76 #define KSZ8795_REGS_SIZE 0x100
78 #define ID1_CHIPID_M 0xf
79 #define ID1_CHIPID_S 4
80 #define ID1_REVISION_M 0x7
81 #define ID1_REVISION_S 1
82 #define ID1_START_SW 1 /* start the switch */
84 #define FAMILY_KS8995 0x95
85 #define FAMILY_KSZ8795 0x87
87 #define KS8995_CHIP_ID 0x00
88 #define KSZ8864_CHIP_ID 0x01
89 #define KSZ8795_CHIP_ID 0x09
91 #define KS8995_CMD_WRITE 0x02U
92 #define KS8995_CMD_READ 0x03U
94 #define KS8995_RESET_DELAY 10 /* usec */
96 enum ks8995_chip_variant
{
103 struct ks8995_chip_params
{
112 static const struct ks8995_chip_params ks8995_chip
[] = {
115 .family_id
= FAMILY_KS8995
,
116 .chip_id
= KS8995_CHIP_ID
,
117 .regs_size
= KS8995_REGS_SIZE
,
122 .name
= "KSZ8864RMN",
123 .family_id
= FAMILY_KS8995
,
124 .chip_id
= KSZ8864_CHIP_ID
,
125 .regs_size
= KSZ8864_REGS_SIZE
,
130 .name
= "KSZ8795CLX",
131 .family_id
= FAMILY_KSZ8795
,
132 .chip_id
= KSZ8795_CHIP_ID
,
133 .regs_size
= KSZ8795_REGS_SIZE
,
139 struct ks8995_switch
{
140 struct spi_device
*spi
;
142 struct gpio_desc
*reset_gpio
;
143 struct bin_attribute regs_attr
;
144 const struct ks8995_chip_params
*chip
;
148 static const struct spi_device_id ks8995_id
[] = {
150 {"ksz8864", ksz8864
},
151 {"ksz8795", ksz8795
},
154 MODULE_DEVICE_TABLE(spi
, ks8995_id
);
156 static const struct of_device_id ks8895_spi_of_match
[] = {
157 { .compatible
= "micrel,ks8995" },
158 { .compatible
= "micrel,ksz8864" },
159 { .compatible
= "micrel,ksz8795" },
162 MODULE_DEVICE_TABLE(of
, ks8895_spi_of_match
);
164 static inline u8
get_chip_id(u8 val
)
166 return (val
>> ID1_CHIPID_S
) & ID1_CHIPID_M
;
169 static inline u8
get_chip_rev(u8 val
)
171 return (val
>> ID1_REVISION_S
) & ID1_REVISION_M
;
174 /* create_spi_cmd - create a chip specific SPI command header
175 * @ks: pointer to switch instance
176 * @cmd: SPI command for switch
177 * @address: register address for command
179 * Different chip families use different bit pattern to address the switches
182 * KS8995: 8bit command + 8bit address
183 * KSZ8795: 3bit command + 12bit address + 1bit TR (?)
185 static inline __be16
create_spi_cmd(struct ks8995_switch
*ks
, int cmd
,
190 /* make room for address (incl. address shift) */
191 result
<<= ks
->chip
->addr_width
+ ks
->chip
->addr_shift
;
193 result
|= address
<< ks
->chip
->addr_shift
;
194 /* SPI protocol needs big endian */
195 return cpu_to_be16(result
);
197 /* ------------------------------------------------------------------------ */
198 static int ks8995_read(struct ks8995_switch
*ks
, char *buf
,
199 unsigned offset
, size_t count
)
202 struct spi_transfer t
[2];
203 struct spi_message m
;
206 cmd
= create_spi_cmd(ks
, KS8995_CMD_READ
, offset
);
207 spi_message_init(&m
);
209 memset(&t
, 0, sizeof(t
));
212 t
[0].len
= sizeof(cmd
);
213 spi_message_add_tail(&t
[0], &m
);
217 spi_message_add_tail(&t
[1], &m
);
219 mutex_lock(&ks
->lock
);
220 err
= spi_sync(ks
->spi
, &m
);
221 mutex_unlock(&ks
->lock
);
223 return err
? err
: count
;
226 static int ks8995_write(struct ks8995_switch
*ks
, char *buf
,
227 unsigned offset
, size_t count
)
230 struct spi_transfer t
[2];
231 struct spi_message m
;
234 cmd
= create_spi_cmd(ks
, KS8995_CMD_WRITE
, offset
);
235 spi_message_init(&m
);
237 memset(&t
, 0, sizeof(t
));
240 t
[0].len
= sizeof(cmd
);
241 spi_message_add_tail(&t
[0], &m
);
245 spi_message_add_tail(&t
[1], &m
);
247 mutex_lock(&ks
->lock
);
248 err
= spi_sync(ks
->spi
, &m
);
249 mutex_unlock(&ks
->lock
);
251 return err
? err
: count
;
254 static inline int ks8995_read_reg(struct ks8995_switch
*ks
, u8 addr
, u8
*buf
)
256 return ks8995_read(ks
, buf
, addr
, 1) != 1;
259 static inline int ks8995_write_reg(struct ks8995_switch
*ks
, u8 addr
, u8 val
)
263 return ks8995_write(ks
, &buf
, addr
, 1) != 1;
266 /* ------------------------------------------------------------------------ */
268 static int ks8995_stop(struct ks8995_switch
*ks
)
270 return ks8995_write_reg(ks
, KS8995_REG_ID1
, 0);
273 static int ks8995_start(struct ks8995_switch
*ks
)
275 return ks8995_write_reg(ks
, KS8995_REG_ID1
, 1);
278 static int ks8995_reset(struct ks8995_switch
*ks
)
282 err
= ks8995_stop(ks
);
286 udelay(KS8995_RESET_DELAY
);
288 return ks8995_start(ks
);
291 static ssize_t
ks8995_registers_read(struct file
*filp
, struct kobject
*kobj
,
292 struct bin_attribute
*bin_attr
, char *buf
, loff_t off
, size_t count
)
295 struct ks8995_switch
*ks8995
;
297 dev
= kobj_to_dev(kobj
);
298 ks8995
= dev_get_drvdata(dev
);
300 return ks8995_read(ks8995
, buf
, off
, count
);
303 static ssize_t
ks8995_registers_write(struct file
*filp
, struct kobject
*kobj
,
304 struct bin_attribute
*bin_attr
, char *buf
, loff_t off
, size_t count
)
307 struct ks8995_switch
*ks8995
;
309 dev
= kobj_to_dev(kobj
);
310 ks8995
= dev_get_drvdata(dev
);
312 return ks8995_write(ks8995
, buf
, off
, count
);
315 /* ks8995_get_revision - get chip revision
316 * @ks: pointer to switch instance
318 * Verify chip family and id and get chip revision.
320 static int ks8995_get_revision(struct ks8995_switch
*ks
)
323 u8 id0
, id1
, ksz8864_id
;
326 err
= ks8995_read_reg(ks
, KS8995_REG_ID0
, &id0
);
332 /* verify family id */
333 if (id0
!= ks
->chip
->family_id
) {
334 dev_err(&ks
->spi
->dev
, "chip family id mismatch: expected 0x%02x but 0x%02x read\n",
335 ks
->chip
->family_id
, id0
);
340 switch (ks
->chip
->family_id
) {
342 /* try reading chip id at CHIP ID1 */
343 err
= ks8995_read_reg(ks
, KS8995_REG_ID1
, &id1
);
350 if ((get_chip_id(id1
) == CHIPID_M
) &&
351 (get_chip_id(id1
) == ks
->chip
->chip_id
)) {
353 ks
->revision_id
= get_chip_rev(id1
);
354 } else if (get_chip_id(id1
) != CHIPID_M
) {
356 err
= ks8995_read_reg(ks
, KS8995_REG_ID1
, &ksz8864_id
);
362 if ((ksz8864_id
& 0x80) &&
363 (ks
->chip
->chip_id
== KSZ8864_CHIP_ID
)) {
364 ks
->revision_id
= get_chip_rev(id1
);
368 dev_err(&ks
->spi
->dev
, "unsupported chip id for KS8995 family: 0x%02x\n",
374 /* try reading chip id at CHIP ID1 */
375 err
= ks8995_read_reg(ks
, KS8995_REG_ID1
, &id1
);
381 if (get_chip_id(id1
) == ks
->chip
->chip_id
) {
382 ks
->revision_id
= get_chip_rev(id1
);
384 dev_err(&ks
->spi
->dev
, "unsupported chip id for KSZ8795 family: 0x%02x\n",
390 dev_err(&ks
->spi
->dev
, "unsupported family id: 0x%02x\n", id0
);
398 static const struct bin_attribute ks8995_registers_attr
= {
403 .size
= KS8995_REGS_SIZE
,
404 .read
= ks8995_registers_read
,
405 .write
= ks8995_registers_write
,
408 /* ------------------------------------------------------------------------ */
409 static int ks8995_probe(struct spi_device
*spi
)
411 struct ks8995_switch
*ks
;
413 int variant
= spi_get_device_id(spi
)->driver_data
;
415 if (variant
>= max_variant
) {
416 dev_err(&spi
->dev
, "bad chip variant %d\n", variant
);
420 ks
= devm_kzalloc(&spi
->dev
, sizeof(*ks
), GFP_KERNEL
);
424 mutex_init(&ks
->lock
);
426 ks
->chip
= &ks8995_chip
[variant
];
428 ks
->reset_gpio
= devm_gpiod_get_optional(&spi
->dev
, "reset",
430 err
= PTR_ERR_OR_ZERO(ks
->reset_gpio
);
433 "failed to get reset gpio: %d\n", err
);
437 err
= gpiod_set_consumer_name(ks
->reset_gpio
, "switch-reset");
441 /* de-assert switch reset */
442 /* FIXME: this likely requires a delay */
443 gpiod_set_value_cansleep(ks
->reset_gpio
, 0);
445 spi_set_drvdata(spi
, ks
);
447 spi
->mode
= SPI_MODE_0
;
448 spi
->bits_per_word
= 8;
449 err
= spi_setup(spi
);
451 dev_err(&spi
->dev
, "spi_setup failed, err=%d\n", err
);
455 err
= ks8995_get_revision(ks
);
459 memcpy(&ks
->regs_attr
, &ks8995_registers_attr
, sizeof(ks
->regs_attr
));
460 ks
->regs_attr
.size
= ks
->chip
->regs_size
;
462 err
= ks8995_reset(ks
);
466 sysfs_attr_init(&ks
->regs_attr
.attr
);
467 err
= sysfs_create_bin_file(&spi
->dev
.kobj
, &ks
->regs_attr
);
469 dev_err(&spi
->dev
, "unable to create sysfs file, err=%d\n",
474 dev_info(&spi
->dev
, "%s device found, Chip ID:%x, Revision:%x\n",
475 ks
->chip
->name
, ks
->chip
->chip_id
, ks
->revision_id
);
480 static void ks8995_remove(struct spi_device
*spi
)
482 struct ks8995_switch
*ks
= spi_get_drvdata(spi
);
484 sysfs_remove_bin_file(&spi
->dev
.kobj
, &ks
->regs_attr
);
487 gpiod_set_value_cansleep(ks
->reset_gpio
, 1);
490 /* ------------------------------------------------------------------------ */
491 static struct spi_driver ks8995_driver
= {
493 .name
= "spi-ks8995",
494 .of_match_table
= ks8895_spi_of_match
,
496 .probe
= ks8995_probe
,
497 .remove
= ks8995_remove
,
498 .id_table
= ks8995_id
,
501 module_spi_driver(ks8995_driver
);
503 MODULE_DESCRIPTION(DRV_DESC
);
504 MODULE_VERSION(DRV_VERSION
);
505 MODULE_AUTHOR("Gabor Juhos <juhosg at openwrt.org>");
506 MODULE_LICENSE("GPL v2");