Linux 4.19.133
[linux/fpc-iii.git] / drivers / net / phy / spi_ks8995.c
blobd8ea4147dfe782249da0305ac6d21365092757fb
1 /*
2 * SPI driver for Micrel/Kendin KS8995M and KSZ8864RMN ethernet switches
4 * Copyright (C) 2008 Gabor Juhos <juhosg at openwrt.org>
6 * This file was based on: drivers/spi/at25.c
7 * Copyright (C) 2006 David Brownell
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License version 2 as published
11 * by the Free Software Foundation.
14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16 #include <linux/types.h>
17 #include <linux/kernel.h>
18 #include <linux/module.h>
19 #include <linux/delay.h>
20 #include <linux/device.h>
21 #include <linux/gpio/consumer.h>
22 #include <linux/of.h>
23 #include <linux/of_gpio.h>
25 #include <linux/spi/spi.h>
27 #define DRV_VERSION "0.1.1"
28 #define DRV_DESC "Micrel KS8995 Ethernet switch SPI driver"
30 /* ------------------------------------------------------------------------ */
32 #define KS8995_REG_ID0 0x00 /* Chip ID0 */
33 #define KS8995_REG_ID1 0x01 /* Chip ID1 */
35 #define KS8995_REG_GC0 0x02 /* Global Control 0 */
36 #define KS8995_REG_GC1 0x03 /* Global Control 1 */
37 #define KS8995_REG_GC2 0x04 /* Global Control 2 */
38 #define KS8995_REG_GC3 0x05 /* Global Control 3 */
39 #define KS8995_REG_GC4 0x06 /* Global Control 4 */
40 #define KS8995_REG_GC5 0x07 /* Global Control 5 */
41 #define KS8995_REG_GC6 0x08 /* Global Control 6 */
42 #define KS8995_REG_GC7 0x09 /* Global Control 7 */
43 #define KS8995_REG_GC8 0x0a /* Global Control 8 */
44 #define KS8995_REG_GC9 0x0b /* Global Control 9 */
46 #define KS8995_REG_PC(p, r) ((0x10 * p) + r) /* Port Control */
47 #define KS8995_REG_PS(p, r) ((0x10 * p) + r + 0xe) /* Port Status */
49 #define KS8995_REG_TPC0 0x60 /* TOS Priority Control 0 */
50 #define KS8995_REG_TPC1 0x61 /* TOS Priority Control 1 */
51 #define KS8995_REG_TPC2 0x62 /* TOS Priority Control 2 */
52 #define KS8995_REG_TPC3 0x63 /* TOS Priority Control 3 */
53 #define KS8995_REG_TPC4 0x64 /* TOS Priority Control 4 */
54 #define KS8995_REG_TPC5 0x65 /* TOS Priority Control 5 */
55 #define KS8995_REG_TPC6 0x66 /* TOS Priority Control 6 */
56 #define KS8995_REG_TPC7 0x67 /* TOS Priority Control 7 */
58 #define KS8995_REG_MAC0 0x68 /* MAC address 0 */
59 #define KS8995_REG_MAC1 0x69 /* MAC address 1 */
60 #define KS8995_REG_MAC2 0x6a /* MAC address 2 */
61 #define KS8995_REG_MAC3 0x6b /* MAC address 3 */
62 #define KS8995_REG_MAC4 0x6c /* MAC address 4 */
63 #define KS8995_REG_MAC5 0x6d /* MAC address 5 */
65 #define KS8995_REG_IAC0 0x6e /* Indirect Access Control 0 */
66 #define KS8995_REG_IAC1 0x6f /* Indirect Access Control 0 */
67 #define KS8995_REG_IAD7 0x70 /* Indirect Access Data 7 */
68 #define KS8995_REG_IAD6 0x71 /* Indirect Access Data 6 */
69 #define KS8995_REG_IAD5 0x72 /* Indirect Access Data 5 */
70 #define KS8995_REG_IAD4 0x73 /* Indirect Access Data 4 */
71 #define KS8995_REG_IAD3 0x74 /* Indirect Access Data 3 */
72 #define KS8995_REG_IAD2 0x75 /* Indirect Access Data 2 */
73 #define KS8995_REG_IAD1 0x76 /* Indirect Access Data 1 */
74 #define KS8995_REG_IAD0 0x77 /* Indirect Access Data 0 */
76 #define KSZ8864_REG_ID1 0xfe /* Chip ID in bit 7 */
78 #define KS8995_REGS_SIZE 0x80
79 #define KSZ8864_REGS_SIZE 0x100
80 #define KSZ8795_REGS_SIZE 0x100
82 #define ID1_CHIPID_M 0xf
83 #define ID1_CHIPID_S 4
84 #define ID1_REVISION_M 0x7
85 #define ID1_REVISION_S 1
86 #define ID1_START_SW 1 /* start the switch */
88 #define FAMILY_KS8995 0x95
89 #define FAMILY_KSZ8795 0x87
90 #define CHIPID_M 0
91 #define KS8995_CHIP_ID 0x00
92 #define KSZ8864_CHIP_ID 0x01
93 #define KSZ8795_CHIP_ID 0x09
95 #define KS8995_CMD_WRITE 0x02U
96 #define KS8995_CMD_READ 0x03U
98 #define KS8995_RESET_DELAY 10 /* usec */
100 enum ks8995_chip_variant {
101 ks8995,
102 ksz8864,
103 ksz8795,
104 max_variant
107 struct ks8995_chip_params {
108 char *name;
109 int family_id;
110 int chip_id;
111 int regs_size;
112 int addr_width;
113 int addr_shift;
116 static const struct ks8995_chip_params ks8995_chip[] = {
117 [ks8995] = {
118 .name = "KS8995MA",
119 .family_id = FAMILY_KS8995,
120 .chip_id = KS8995_CHIP_ID,
121 .regs_size = KS8995_REGS_SIZE,
122 .addr_width = 8,
123 .addr_shift = 0,
125 [ksz8864] = {
126 .name = "KSZ8864RMN",
127 .family_id = FAMILY_KS8995,
128 .chip_id = KSZ8864_CHIP_ID,
129 .regs_size = KSZ8864_REGS_SIZE,
130 .addr_width = 8,
131 .addr_shift = 0,
133 [ksz8795] = {
134 .name = "KSZ8795CLX",
135 .family_id = FAMILY_KSZ8795,
136 .chip_id = KSZ8795_CHIP_ID,
137 .regs_size = KSZ8795_REGS_SIZE,
138 .addr_width = 12,
139 .addr_shift = 1,
143 struct ks8995_pdata {
144 int reset_gpio;
145 enum of_gpio_flags reset_gpio_flags;
148 struct ks8995_switch {
149 struct spi_device *spi;
150 struct mutex lock;
151 struct ks8995_pdata *pdata;
152 struct bin_attribute regs_attr;
153 const struct ks8995_chip_params *chip;
154 int revision_id;
157 static const struct spi_device_id ks8995_id[] = {
158 {"ks8995", ks8995},
159 {"ksz8864", ksz8864},
160 {"ksz8795", ksz8795},
163 MODULE_DEVICE_TABLE(spi, ks8995_id);
165 static const struct of_device_id ks8895_spi_of_match[] = {
166 { .compatible = "micrel,ks8995" },
167 { .compatible = "micrel,ksz8864" },
168 { .compatible = "micrel,ksz8795" },
169 { },
171 MODULE_DEVICE_TABLE(of, ks8895_spi_of_match);
173 static inline u8 get_chip_id(u8 val)
175 return (val >> ID1_CHIPID_S) & ID1_CHIPID_M;
178 static inline u8 get_chip_rev(u8 val)
180 return (val >> ID1_REVISION_S) & ID1_REVISION_M;
183 /* create_spi_cmd - create a chip specific SPI command header
184 * @ks: pointer to switch instance
185 * @cmd: SPI command for switch
186 * @address: register address for command
188 * Different chip families use different bit pattern to address the switches
189 * registers:
191 * KS8995: 8bit command + 8bit address
192 * KSZ8795: 3bit command + 12bit address + 1bit TR (?)
194 static inline __be16 create_spi_cmd(struct ks8995_switch *ks, int cmd,
195 unsigned address)
197 u16 result = cmd;
199 /* make room for address (incl. address shift) */
200 result <<= ks->chip->addr_width + ks->chip->addr_shift;
201 /* add address */
202 result |= address << ks->chip->addr_shift;
203 /* SPI protocol needs big endian */
204 return cpu_to_be16(result);
206 /* ------------------------------------------------------------------------ */
207 static int ks8995_read(struct ks8995_switch *ks, char *buf,
208 unsigned offset, size_t count)
210 __be16 cmd;
211 struct spi_transfer t[2];
212 struct spi_message m;
213 int err;
215 cmd = create_spi_cmd(ks, KS8995_CMD_READ, offset);
216 spi_message_init(&m);
218 memset(&t, 0, sizeof(t));
220 t[0].tx_buf = &cmd;
221 t[0].len = sizeof(cmd);
222 spi_message_add_tail(&t[0], &m);
224 t[1].rx_buf = buf;
225 t[1].len = count;
226 spi_message_add_tail(&t[1], &m);
228 mutex_lock(&ks->lock);
229 err = spi_sync(ks->spi, &m);
230 mutex_unlock(&ks->lock);
232 return err ? err : count;
235 static int ks8995_write(struct ks8995_switch *ks, char *buf,
236 unsigned offset, size_t count)
238 __be16 cmd;
239 struct spi_transfer t[2];
240 struct spi_message m;
241 int err;
243 cmd = create_spi_cmd(ks, KS8995_CMD_WRITE, offset);
244 spi_message_init(&m);
246 memset(&t, 0, sizeof(t));
248 t[0].tx_buf = &cmd;
249 t[0].len = sizeof(cmd);
250 spi_message_add_tail(&t[0], &m);
252 t[1].tx_buf = buf;
253 t[1].len = count;
254 spi_message_add_tail(&t[1], &m);
256 mutex_lock(&ks->lock);
257 err = spi_sync(ks->spi, &m);
258 mutex_unlock(&ks->lock);
260 return err ? err : count;
263 static inline int ks8995_read_reg(struct ks8995_switch *ks, u8 addr, u8 *buf)
265 return ks8995_read(ks, buf, addr, 1) != 1;
268 static inline int ks8995_write_reg(struct ks8995_switch *ks, u8 addr, u8 val)
270 char buf = val;
272 return ks8995_write(ks, &buf, addr, 1) != 1;
275 /* ------------------------------------------------------------------------ */
277 static int ks8995_stop(struct ks8995_switch *ks)
279 return ks8995_write_reg(ks, KS8995_REG_ID1, 0);
282 static int ks8995_start(struct ks8995_switch *ks)
284 return ks8995_write_reg(ks, KS8995_REG_ID1, 1);
287 static int ks8995_reset(struct ks8995_switch *ks)
289 int err;
291 err = ks8995_stop(ks);
292 if (err)
293 return err;
295 udelay(KS8995_RESET_DELAY);
297 return ks8995_start(ks);
300 static ssize_t ks8995_registers_read(struct file *filp, struct kobject *kobj,
301 struct bin_attribute *bin_attr, char *buf, loff_t off, size_t count)
303 struct device *dev;
304 struct ks8995_switch *ks8995;
306 dev = container_of(kobj, struct device, kobj);
307 ks8995 = dev_get_drvdata(dev);
309 return ks8995_read(ks8995, buf, off, count);
312 static ssize_t ks8995_registers_write(struct file *filp, struct kobject *kobj,
313 struct bin_attribute *bin_attr, char *buf, loff_t off, size_t count)
315 struct device *dev;
316 struct ks8995_switch *ks8995;
318 dev = container_of(kobj, struct device, kobj);
319 ks8995 = dev_get_drvdata(dev);
321 return ks8995_write(ks8995, buf, off, count);
324 /* ks8995_get_revision - get chip revision
325 * @ks: pointer to switch instance
327 * Verify chip family and id and get chip revision.
329 static int ks8995_get_revision(struct ks8995_switch *ks)
331 int err;
332 u8 id0, id1, ksz8864_id;
334 /* read family id */
335 err = ks8995_read_reg(ks, KS8995_REG_ID0, &id0);
336 if (err) {
337 err = -EIO;
338 goto err_out;
341 /* verify family id */
342 if (id0 != ks->chip->family_id) {
343 dev_err(&ks->spi->dev, "chip family id mismatch: expected 0x%02x but 0x%02x read\n",
344 ks->chip->family_id, id0);
345 err = -ENODEV;
346 goto err_out;
349 switch (ks->chip->family_id) {
350 case FAMILY_KS8995:
351 /* try reading chip id at CHIP ID1 */
352 err = ks8995_read_reg(ks, KS8995_REG_ID1, &id1);
353 if (err) {
354 err = -EIO;
355 goto err_out;
358 /* verify chip id */
359 if ((get_chip_id(id1) == CHIPID_M) &&
360 (get_chip_id(id1) == ks->chip->chip_id)) {
361 /* KS8995MA */
362 ks->revision_id = get_chip_rev(id1);
363 } else if (get_chip_id(id1) != CHIPID_M) {
364 /* KSZ8864RMN */
365 err = ks8995_read_reg(ks, KS8995_REG_ID1, &ksz8864_id);
366 if (err) {
367 err = -EIO;
368 goto err_out;
371 if ((ksz8864_id & 0x80) &&
372 (ks->chip->chip_id == KSZ8864_CHIP_ID)) {
373 ks->revision_id = get_chip_rev(id1);
376 } else {
377 dev_err(&ks->spi->dev, "unsupported chip id for KS8995 family: 0x%02x\n",
378 id1);
379 err = -ENODEV;
381 break;
382 case FAMILY_KSZ8795:
383 /* try reading chip id at CHIP ID1 */
384 err = ks8995_read_reg(ks, KS8995_REG_ID1, &id1);
385 if (err) {
386 err = -EIO;
387 goto err_out;
390 if (get_chip_id(id1) == ks->chip->chip_id) {
391 ks->revision_id = get_chip_rev(id1);
392 } else {
393 dev_err(&ks->spi->dev, "unsupported chip id for KSZ8795 family: 0x%02x\n",
394 id1);
395 err = -ENODEV;
397 break;
398 default:
399 dev_err(&ks->spi->dev, "unsupported family id: 0x%02x\n", id0);
400 err = -ENODEV;
401 break;
403 err_out:
404 return err;
407 /* ks8995_parse_dt - setup platform data from devicetree
408 * @ks: pointer to switch instance
410 * Parses supported DT properties and sets up platform data
411 * accordingly.
413 static void ks8995_parse_dt(struct ks8995_switch *ks)
415 struct device_node *np = ks->spi->dev.of_node;
416 struct ks8995_pdata *pdata = ks->pdata;
418 if (!np)
419 return;
421 pdata->reset_gpio = of_get_named_gpio_flags(np, "reset-gpios", 0,
422 &pdata->reset_gpio_flags);
425 static const struct bin_attribute ks8995_registers_attr = {
426 .attr = {
427 .name = "registers",
428 .mode = 0600,
430 .size = KS8995_REGS_SIZE,
431 .read = ks8995_registers_read,
432 .write = ks8995_registers_write,
435 /* ------------------------------------------------------------------------ */
436 static int ks8995_probe(struct spi_device *spi)
438 struct ks8995_switch *ks;
439 int err;
440 int variant = spi_get_device_id(spi)->driver_data;
442 if (variant >= max_variant) {
443 dev_err(&spi->dev, "bad chip variant %d\n", variant);
444 return -ENODEV;
447 ks = devm_kzalloc(&spi->dev, sizeof(*ks), GFP_KERNEL);
448 if (!ks)
449 return -ENOMEM;
451 mutex_init(&ks->lock);
452 ks->spi = spi;
453 ks->chip = &ks8995_chip[variant];
455 if (ks->spi->dev.of_node) {
456 ks->pdata = devm_kzalloc(&spi->dev, sizeof(*ks->pdata),
457 GFP_KERNEL);
458 if (!ks->pdata)
459 return -ENOMEM;
461 ks->pdata->reset_gpio = -1;
463 ks8995_parse_dt(ks);
466 if (!ks->pdata)
467 ks->pdata = spi->dev.platform_data;
469 /* de-assert switch reset */
470 if (ks->pdata && gpio_is_valid(ks->pdata->reset_gpio)) {
471 unsigned long flags;
473 flags = (ks->pdata->reset_gpio_flags == OF_GPIO_ACTIVE_LOW ?
474 GPIOF_ACTIVE_LOW : 0);
476 err = devm_gpio_request_one(&spi->dev,
477 ks->pdata->reset_gpio,
478 flags, "switch-reset");
479 if (err) {
480 dev_err(&spi->dev,
481 "failed to get reset-gpios: %d\n", err);
482 return -EIO;
485 gpiod_set_value(gpio_to_desc(ks->pdata->reset_gpio), 0);
488 spi_set_drvdata(spi, ks);
490 spi->mode = SPI_MODE_0;
491 spi->bits_per_word = 8;
492 err = spi_setup(spi);
493 if (err) {
494 dev_err(&spi->dev, "spi_setup failed, err=%d\n", err);
495 return err;
498 err = ks8995_get_revision(ks);
499 if (err)
500 return err;
502 memcpy(&ks->regs_attr, &ks8995_registers_attr, sizeof(ks->regs_attr));
503 ks->regs_attr.size = ks->chip->regs_size;
505 err = ks8995_reset(ks);
506 if (err)
507 return err;
509 sysfs_attr_init(&ks->regs_attr.attr);
510 err = sysfs_create_bin_file(&spi->dev.kobj, &ks->regs_attr);
511 if (err) {
512 dev_err(&spi->dev, "unable to create sysfs file, err=%d\n",
513 err);
514 return err;
517 dev_info(&spi->dev, "%s device found, Chip ID:%x, Revision:%x\n",
518 ks->chip->name, ks->chip->chip_id, ks->revision_id);
520 return 0;
523 static int ks8995_remove(struct spi_device *spi)
525 struct ks8995_switch *ks = spi_get_drvdata(spi);
527 sysfs_remove_bin_file(&spi->dev.kobj, &ks->regs_attr);
529 /* assert reset */
530 if (ks->pdata && gpio_is_valid(ks->pdata->reset_gpio))
531 gpiod_set_value(gpio_to_desc(ks->pdata->reset_gpio), 1);
533 return 0;
536 /* ------------------------------------------------------------------------ */
537 static struct spi_driver ks8995_driver = {
538 .driver = {
539 .name = "spi-ks8995",
540 .of_match_table = of_match_ptr(ks8895_spi_of_match),
542 .probe = ks8995_probe,
543 .remove = ks8995_remove,
544 .id_table = ks8995_id,
547 module_spi_driver(ks8995_driver);
549 MODULE_DESCRIPTION(DRV_DESC);
550 MODULE_VERSION(DRV_VERSION);
551 MODULE_AUTHOR("Gabor Juhos <juhosg at openwrt.org>");
552 MODULE_LICENSE("GPL v2");