1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * SPI driver for Micrel/Kendin KS8995M and KSZ8864RMN ethernet switches 4 * 5 * Copyright (C) 2008 Gabor Juhos <juhosg at openwrt.org> 6 * 7 * This file was based on: drivers/spi/at25.c 8 * Copyright (C) 2006 David Brownell 9 */ 10 11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 12 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> 19 #include <linux/of.h> 20 #include <linux/of_gpio.h> 21 22 #include <linux/spi/spi.h> 23 24 #define DRV_VERSION "0.1.1" 25 #define DRV_DESC "Micrel KS8995 Ethernet switch SPI driver" 26 27 /* ------------------------------------------------------------------------ */ 28 29 #define KS8995_REG_ID0 0x00 /* Chip ID0 */ 30 #define KS8995_REG_ID1 0x01 /* Chip ID1 */ 31 32 #define KS8995_REG_GC0 0x02 /* Global Control 0 */ 33 #define KS8995_REG_GC1 0x03 /* Global Control 1 */ 34 #define KS8995_REG_GC2 0x04 /* Global Control 2 */ 35 #define KS8995_REG_GC3 0x05 /* Global Control 3 */ 36 #define KS8995_REG_GC4 0x06 /* Global Control 4 */ 37 #define KS8995_REG_GC5 0x07 /* Global Control 5 */ 38 #define KS8995_REG_GC6 0x08 /* Global Control 6 */ 39 #define KS8995_REG_GC7 0x09 /* Global Control 7 */ 40 #define KS8995_REG_GC8 0x0a /* Global Control 8 */ 41 #define KS8995_REG_GC9 0x0b /* Global Control 9 */ 42 43 #define KS8995_REG_PC(p, r) ((0x10 * p) + r) /* Port Control */ 44 #define KS8995_REG_PS(p, r) ((0x10 * p) + r + 0xe) /* Port Status */ 45 46 #define KS8995_REG_TPC0 0x60 /* TOS Priority Control 0 */ 47 #define KS8995_REG_TPC1 0x61 /* TOS Priority Control 1 */ 48 #define KS8995_REG_TPC2 0x62 /* TOS Priority Control 2 */ 49 #define KS8995_REG_TPC3 0x63 /* TOS Priority Control 3 */ 50 #define KS8995_REG_TPC4 0x64 /* TOS Priority Control 4 */ 51 #define KS8995_REG_TPC5 0x65 /* TOS Priority Control 5 */ 52 #define KS8995_REG_TPC6 0x66 /* TOS Priority Control 6 */ 53 #define KS8995_REG_TPC7 0x67 /* TOS Priority Control 7 */ 54 55 #define KS8995_REG_MAC0 0x68 /* MAC address 0 */ 56 #define KS8995_REG_MAC1 0x69 /* MAC address 1 */ 57 #define KS8995_REG_MAC2 0x6a /* MAC address 2 */ 58 #define KS8995_REG_MAC3 0x6b /* MAC address 3 */ 59 #define KS8995_REG_MAC4 0x6c /* MAC address 4 */ 60 #define KS8995_REG_MAC5 0x6d /* MAC address 5 */ 61 62 #define KS8995_REG_IAC0 0x6e /* Indirect Access Control 0 */ 63 #define KS8995_REG_IAC1 0x6f /* Indirect Access Control 0 */ 64 #define KS8995_REG_IAD7 0x70 /* Indirect Access Data 7 */ 65 #define KS8995_REG_IAD6 0x71 /* Indirect Access Data 6 */ 66 #define KS8995_REG_IAD5 0x72 /* Indirect Access Data 5 */ 67 #define KS8995_REG_IAD4 0x73 /* Indirect Access Data 4 */ 68 #define KS8995_REG_IAD3 0x74 /* Indirect Access Data 3 */ 69 #define KS8995_REG_IAD2 0x75 /* Indirect Access Data 2 */ 70 #define KS8995_REG_IAD1 0x76 /* Indirect Access Data 1 */ 71 #define KS8995_REG_IAD0 0x77 /* Indirect Access Data 0 */ 72 73 #define KSZ8864_REG_ID1 0xfe /* Chip ID in bit 7 */ 74 75 #define KS8995_REGS_SIZE 0x80 76 #define KSZ8864_REGS_SIZE 0x100 77 #define KSZ8795_REGS_SIZE 0x100 78 79 #define ID1_CHIPID_M 0xf 80 #define ID1_CHIPID_S 4 81 #define ID1_REVISION_M 0x7 82 #define ID1_REVISION_S 1 83 #define ID1_START_SW 1 /* start the switch */ 84 85 #define FAMILY_KS8995 0x95 86 #define FAMILY_KSZ8795 0x87 87 #define CHIPID_M 0 88 #define KS8995_CHIP_ID 0x00 89 #define KSZ8864_CHIP_ID 0x01 90 #define KSZ8795_CHIP_ID 0x09 91 92 #define KS8995_CMD_WRITE 0x02U 93 #define KS8995_CMD_READ 0x03U 94 95 #define KS8995_RESET_DELAY 10 /* usec */ 96 97 enum ks8995_chip_variant { 98 ks8995, 99 ksz8864, 100 ksz8795, 101 max_variant 102 }; 103 104 struct ks8995_chip_params { 105 char *name; 106 int family_id; 107 int chip_id; 108 int regs_size; 109 int addr_width; 110 int addr_shift; 111 }; 112 113 static const struct ks8995_chip_params ks8995_chip[] = { 114 [ks8995] = { 115 .name = "KS8995MA", 116 .family_id = FAMILY_KS8995, 117 .chip_id = KS8995_CHIP_ID, 118 .regs_size = KS8995_REGS_SIZE, 119 .addr_width = 8, 120 .addr_shift = 0, 121 }, 122 [ksz8864] = { 123 .name = "KSZ8864RMN", 124 .family_id = FAMILY_KS8995, 125 .chip_id = KSZ8864_CHIP_ID, 126 .regs_size = KSZ8864_REGS_SIZE, 127 .addr_width = 8, 128 .addr_shift = 0, 129 }, 130 [ksz8795] = { 131 .name = "KSZ8795CLX", 132 .family_id = FAMILY_KSZ8795, 133 .chip_id = KSZ8795_CHIP_ID, 134 .regs_size = KSZ8795_REGS_SIZE, 135 .addr_width = 12, 136 .addr_shift = 1, 137 }, 138 }; 139 140 struct ks8995_pdata { 141 int reset_gpio; 142 enum of_gpio_flags reset_gpio_flags; 143 }; 144 145 struct ks8995_switch { 146 struct spi_device *spi; 147 struct mutex lock; 148 struct ks8995_pdata *pdata; 149 struct bin_attribute regs_attr; 150 const struct ks8995_chip_params *chip; 151 int revision_id; 152 }; 153 154 static const struct spi_device_id ks8995_id[] = { 155 {"ks8995", ks8995}, 156 {"ksz8864", ksz8864}, 157 {"ksz8795", ksz8795}, 158 { } 159 }; 160 MODULE_DEVICE_TABLE(spi, ks8995_id); 161 162 static inline u8 get_chip_id(u8 val) 163 { 164 return (val >> ID1_CHIPID_S) & ID1_CHIPID_M; 165 } 166 167 static inline u8 get_chip_rev(u8 val) 168 { 169 return (val >> ID1_REVISION_S) & ID1_REVISION_M; 170 } 171 172 /* create_spi_cmd - create a chip specific SPI command header 173 * @ks: pointer to switch instance 174 * @cmd: SPI command for switch 175 * @address: register address for command 176 * 177 * Different chip families use different bit pattern to address the switches 178 * registers: 179 * 180 * KS8995: 8bit command + 8bit address 181 * KSZ8795: 3bit command + 12bit address + 1bit TR (?) 182 */ 183 static inline __be16 create_spi_cmd(struct ks8995_switch *ks, int cmd, 184 unsigned address) 185 { 186 u16 result = cmd; 187 188 /* make room for address (incl. address shift) */ 189 result <<= ks->chip->addr_width + ks->chip->addr_shift; 190 /* add address */ 191 result |= address << ks->chip->addr_shift; 192 /* SPI protocol needs big endian */ 193 return cpu_to_be16(result); 194 } 195 /* ------------------------------------------------------------------------ */ 196 static int ks8995_read(struct ks8995_switch *ks, char *buf, 197 unsigned offset, size_t count) 198 { 199 __be16 cmd; 200 struct spi_transfer t[2]; 201 struct spi_message m; 202 int err; 203 204 cmd = create_spi_cmd(ks, KS8995_CMD_READ, offset); 205 spi_message_init(&m); 206 207 memset(&t, 0, sizeof(t)); 208 209 t[0].tx_buf = &cmd; 210 t[0].len = sizeof(cmd); 211 spi_message_add_tail(&t[0], &m); 212 213 t[1].rx_buf = buf; 214 t[1].len = count; 215 spi_message_add_tail(&t[1], &m); 216 217 mutex_lock(&ks->lock); 218 err = spi_sync(ks->spi, &m); 219 mutex_unlock(&ks->lock); 220 221 return err ? err : count; 222 } 223 224 static int ks8995_write(struct ks8995_switch *ks, char *buf, 225 unsigned offset, size_t count) 226 { 227 __be16 cmd; 228 struct spi_transfer t[2]; 229 struct spi_message m; 230 int err; 231 232 cmd = create_spi_cmd(ks, KS8995_CMD_WRITE, offset); 233 spi_message_init(&m); 234 235 memset(&t, 0, sizeof(t)); 236 237 t[0].tx_buf = &cmd; 238 t[0].len = sizeof(cmd); 239 spi_message_add_tail(&t[0], &m); 240 241 t[1].tx_buf = buf; 242 t[1].len = count; 243 spi_message_add_tail(&t[1], &m); 244 245 mutex_lock(&ks->lock); 246 err = spi_sync(ks->spi, &m); 247 mutex_unlock(&ks->lock); 248 249 return err ? err : count; 250 } 251 252 static inline int ks8995_read_reg(struct ks8995_switch *ks, u8 addr, u8 *buf) 253 { 254 return ks8995_read(ks, buf, addr, 1) != 1; 255 } 256 257 static inline int ks8995_write_reg(struct ks8995_switch *ks, u8 addr, u8 val) 258 { 259 char buf = val; 260 261 return ks8995_write(ks, &buf, addr, 1) != 1; 262 } 263 264 /* ------------------------------------------------------------------------ */ 265 266 static int ks8995_stop(struct ks8995_switch *ks) 267 { 268 return ks8995_write_reg(ks, KS8995_REG_ID1, 0); 269 } 270 271 static int ks8995_start(struct ks8995_switch *ks) 272 { 273 return ks8995_write_reg(ks, KS8995_REG_ID1, 1); 274 } 275 276 static int ks8995_reset(struct ks8995_switch *ks) 277 { 278 int err; 279 280 err = ks8995_stop(ks); 281 if (err) 282 return err; 283 284 udelay(KS8995_RESET_DELAY); 285 286 return ks8995_start(ks); 287 } 288 289 static ssize_t ks8995_registers_read(struct file *filp, struct kobject *kobj, 290 struct bin_attribute *bin_attr, char *buf, loff_t off, size_t count) 291 { 292 struct device *dev; 293 struct ks8995_switch *ks8995; 294 295 dev = container_of(kobj, struct device, kobj); 296 ks8995 = dev_get_drvdata(dev); 297 298 return ks8995_read(ks8995, buf, off, count); 299 } 300 301 static ssize_t ks8995_registers_write(struct file *filp, struct kobject *kobj, 302 struct bin_attribute *bin_attr, char *buf, loff_t off, size_t count) 303 { 304 struct device *dev; 305 struct ks8995_switch *ks8995; 306 307 dev = container_of(kobj, struct device, kobj); 308 ks8995 = dev_get_drvdata(dev); 309 310 return ks8995_write(ks8995, buf, off, count); 311 } 312 313 /* ks8995_get_revision - get chip revision 314 * @ks: pointer to switch instance 315 * 316 * Verify chip family and id and get chip revision. 317 */ 318 static int ks8995_get_revision(struct ks8995_switch *ks) 319 { 320 int err; 321 u8 id0, id1, ksz8864_id; 322 323 /* read family id */ 324 err = ks8995_read_reg(ks, KS8995_REG_ID0, &id0); 325 if (err) { 326 err = -EIO; 327 goto err_out; 328 } 329 330 /* verify family id */ 331 if (id0 != ks->chip->family_id) { 332 dev_err(&ks->spi->dev, "chip family id mismatch: expected 0x%02x but 0x%02x read\n", 333 ks->chip->family_id, id0); 334 err = -ENODEV; 335 goto err_out; 336 } 337 338 switch (ks->chip->family_id) { 339 case FAMILY_KS8995: 340 /* try reading chip id at CHIP ID1 */ 341 err = ks8995_read_reg(ks, KS8995_REG_ID1, &id1); 342 if (err) { 343 err = -EIO; 344 goto err_out; 345 } 346 347 /* verify chip id */ 348 if ((get_chip_id(id1) == CHIPID_M) && 349 (get_chip_id(id1) == ks->chip->chip_id)) { 350 /* KS8995MA */ 351 ks->revision_id = get_chip_rev(id1); 352 } else if (get_chip_id(id1) != CHIPID_M) { 353 /* KSZ8864RMN */ 354 err = ks8995_read_reg(ks, KS8995_REG_ID1, &ksz8864_id); 355 if (err) { 356 err = -EIO; 357 goto err_out; 358 } 359 360 if ((ksz8864_id & 0x80) && 361 (ks->chip->chip_id == KSZ8864_CHIP_ID)) { 362 ks->revision_id = get_chip_rev(id1); 363 } 364 365 } else { 366 dev_err(&ks->spi->dev, "unsupported chip id for KS8995 family: 0x%02x\n", 367 id1); 368 err = -ENODEV; 369 } 370 break; 371 case FAMILY_KSZ8795: 372 /* try reading chip id at CHIP ID1 */ 373 err = ks8995_read_reg(ks, KS8995_REG_ID1, &id1); 374 if (err) { 375 err = -EIO; 376 goto err_out; 377 } 378 379 if (get_chip_id(id1) == ks->chip->chip_id) { 380 ks->revision_id = get_chip_rev(id1); 381 } else { 382 dev_err(&ks->spi->dev, "unsupported chip id for KSZ8795 family: 0x%02x\n", 383 id1); 384 err = -ENODEV; 385 } 386 break; 387 default: 388 dev_err(&ks->spi->dev, "unsupported family id: 0x%02x\n", id0); 389 err = -ENODEV; 390 break; 391 } 392 err_out: 393 return err; 394 } 395 396 /* ks8995_parse_dt - setup platform data from devicetree 397 * @ks: pointer to switch instance 398 * 399 * Parses supported DT properties and sets up platform data 400 * accordingly. 401 */ 402 static void ks8995_parse_dt(struct ks8995_switch *ks) 403 { 404 struct device_node *np = ks->spi->dev.of_node; 405 struct ks8995_pdata *pdata = ks->pdata; 406 407 if (!np) 408 return; 409 410 pdata->reset_gpio = of_get_named_gpio_flags(np, "reset-gpios", 0, 411 &pdata->reset_gpio_flags); 412 } 413 414 static const struct bin_attribute ks8995_registers_attr = { 415 .attr = { 416 .name = "registers", 417 .mode = 0600, 418 }, 419 .size = KS8995_REGS_SIZE, 420 .read = ks8995_registers_read, 421 .write = ks8995_registers_write, 422 }; 423 424 /* ------------------------------------------------------------------------ */ 425 static int ks8995_probe(struct spi_device *spi) 426 { 427 struct ks8995_switch *ks; 428 int err; 429 int variant = spi_get_device_id(spi)->driver_data; 430 431 if (variant >= max_variant) { 432 dev_err(&spi->dev, "bad chip variant %d\n", variant); 433 return -ENODEV; 434 } 435 436 ks = devm_kzalloc(&spi->dev, sizeof(*ks), GFP_KERNEL); 437 if (!ks) 438 return -ENOMEM; 439 440 mutex_init(&ks->lock); 441 ks->spi = spi; 442 ks->chip = &ks8995_chip[variant]; 443 444 if (ks->spi->dev.of_node) { 445 ks->pdata = devm_kzalloc(&spi->dev, sizeof(*ks->pdata), 446 GFP_KERNEL); 447 if (!ks->pdata) 448 return -ENOMEM; 449 450 ks->pdata->reset_gpio = -1; 451 452 ks8995_parse_dt(ks); 453 } 454 455 if (!ks->pdata) 456 ks->pdata = spi->dev.platform_data; 457 458 /* de-assert switch reset */ 459 if (ks->pdata && gpio_is_valid(ks->pdata->reset_gpio)) { 460 unsigned long flags; 461 462 flags = (ks->pdata->reset_gpio_flags == OF_GPIO_ACTIVE_LOW ? 463 GPIOF_ACTIVE_LOW : 0); 464 465 err = devm_gpio_request_one(&spi->dev, 466 ks->pdata->reset_gpio, 467 flags, "switch-reset"); 468 if (err) { 469 dev_err(&spi->dev, 470 "failed to get reset-gpios: %d\n", err); 471 return -EIO; 472 } 473 474 gpiod_set_value(gpio_to_desc(ks->pdata->reset_gpio), 0); 475 } 476 477 spi_set_drvdata(spi, ks); 478 479 spi->mode = SPI_MODE_0; 480 spi->bits_per_word = 8; 481 err = spi_setup(spi); 482 if (err) { 483 dev_err(&spi->dev, "spi_setup failed, err=%d\n", err); 484 return err; 485 } 486 487 err = ks8995_get_revision(ks); 488 if (err) 489 return err; 490 491 memcpy(&ks->regs_attr, &ks8995_registers_attr, sizeof(ks->regs_attr)); 492 ks->regs_attr.size = ks->chip->regs_size; 493 494 err = ks8995_reset(ks); 495 if (err) 496 return err; 497 498 sysfs_attr_init(&ks->regs_attr.attr); 499 err = sysfs_create_bin_file(&spi->dev.kobj, &ks->regs_attr); 500 if (err) { 501 dev_err(&spi->dev, "unable to create sysfs file, err=%d\n", 502 err); 503 return err; 504 } 505 506 dev_info(&spi->dev, "%s device found, Chip ID:%x, Revision:%x\n", 507 ks->chip->name, ks->chip->chip_id, ks->revision_id); 508 509 return 0; 510 } 511 512 static int ks8995_remove(struct spi_device *spi) 513 { 514 struct ks8995_switch *ks = spi_get_drvdata(spi); 515 516 sysfs_remove_bin_file(&spi->dev.kobj, &ks->regs_attr); 517 518 /* assert reset */ 519 if (ks->pdata && gpio_is_valid(ks->pdata->reset_gpio)) 520 gpiod_set_value(gpio_to_desc(ks->pdata->reset_gpio), 1); 521 522 return 0; 523 } 524 525 /* ------------------------------------------------------------------------ */ 526 static struct spi_driver ks8995_driver = { 527 .driver = { 528 .name = "spi-ks8995", 529 }, 530 .probe = ks8995_probe, 531 .remove = ks8995_remove, 532 .id_table = ks8995_id, 533 }; 534 535 module_spi_driver(ks8995_driver); 536 537 MODULE_DESCRIPTION(DRV_DESC); 538 MODULE_VERSION(DRV_VERSION); 539 MODULE_AUTHOR("Gabor Juhos <juhosg at openwrt.org>"); 540 MODULE_LICENSE("GPL v2"); 541