Frederic LAMBERT | a8e510f | 2011-12-18 07:33:41 +0000 | [diff] [blame] | 1 | /* |
| 2 | * SPI driver for Micrel/Kendin KS8995M ethernet switch |
| 3 | * |
| 4 | * Copyright (C) 2008 Gabor Juhos <juhosg at openwrt.org> |
| 5 | * |
| 6 | * This file was based on: drivers/spi/at25.c |
| 7 | * Copyright (C) 2006 David Brownell |
| 8 | * |
| 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. |
| 12 | */ |
| 13 | |
Joe Perches | 8d24248 | 2012-06-09 07:49:07 +0000 | [diff] [blame] | 14 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 15 | |
Frederic LAMBERT | a8e510f | 2011-12-18 07:33:41 +0000 | [diff] [blame] | 16 | #include <linux/types.h> |
| 17 | #include <linux/kernel.h> |
| 18 | #include <linux/init.h> |
| 19 | #include <linux/module.h> |
| 20 | #include <linux/delay.h> |
| 21 | #include <linux/device.h> |
| 22 | |
| 23 | #include <linux/spi/spi.h> |
| 24 | |
| 25 | #define DRV_VERSION "0.1.1" |
| 26 | #define DRV_DESC "Micrel KS8995 Ethernet switch SPI driver" |
| 27 | |
| 28 | /* ------------------------------------------------------------------------ */ |
| 29 | |
| 30 | #define KS8995_REG_ID0 0x00 /* Chip ID0 */ |
| 31 | #define KS8995_REG_ID1 0x01 /* Chip ID1 */ |
| 32 | |
| 33 | #define KS8995_REG_GC0 0x02 /* Global Control 0 */ |
| 34 | #define KS8995_REG_GC1 0x03 /* Global Control 1 */ |
| 35 | #define KS8995_REG_GC2 0x04 /* Global Control 2 */ |
| 36 | #define KS8995_REG_GC3 0x05 /* Global Control 3 */ |
| 37 | #define KS8995_REG_GC4 0x06 /* Global Control 4 */ |
| 38 | #define KS8995_REG_GC5 0x07 /* Global Control 5 */ |
| 39 | #define KS8995_REG_GC6 0x08 /* Global Control 6 */ |
| 40 | #define KS8995_REG_GC7 0x09 /* Global Control 7 */ |
| 41 | #define KS8995_REG_GC8 0x0a /* Global Control 8 */ |
| 42 | #define KS8995_REG_GC9 0x0b /* Global Control 9 */ |
| 43 | |
| 44 | #define KS8995_REG_PC(p, r) ((0x10 * p) + r) /* Port Control */ |
| 45 | #define KS8995_REG_PS(p, r) ((0x10 * p) + r + 0xe) /* Port Status */ |
| 46 | |
| 47 | #define KS8995_REG_TPC0 0x60 /* TOS Priority Control 0 */ |
| 48 | #define KS8995_REG_TPC1 0x61 /* TOS Priority Control 1 */ |
| 49 | #define KS8995_REG_TPC2 0x62 /* TOS Priority Control 2 */ |
| 50 | #define KS8995_REG_TPC3 0x63 /* TOS Priority Control 3 */ |
| 51 | #define KS8995_REG_TPC4 0x64 /* TOS Priority Control 4 */ |
| 52 | #define KS8995_REG_TPC5 0x65 /* TOS Priority Control 5 */ |
| 53 | #define KS8995_REG_TPC6 0x66 /* TOS Priority Control 6 */ |
| 54 | #define KS8995_REG_TPC7 0x67 /* TOS Priority Control 7 */ |
| 55 | |
| 56 | #define KS8995_REG_MAC0 0x68 /* MAC address 0 */ |
| 57 | #define KS8995_REG_MAC1 0x69 /* MAC address 1 */ |
| 58 | #define KS8995_REG_MAC2 0x6a /* MAC address 2 */ |
| 59 | #define KS8995_REG_MAC3 0x6b /* MAC address 3 */ |
| 60 | #define KS8995_REG_MAC4 0x6c /* MAC address 4 */ |
| 61 | #define KS8995_REG_MAC5 0x6d /* MAC address 5 */ |
| 62 | |
| 63 | #define KS8995_REG_IAC0 0x6e /* Indirect Access Control 0 */ |
| 64 | #define KS8995_REG_IAC1 0x6f /* Indirect Access Control 0 */ |
| 65 | #define KS8995_REG_IAD7 0x70 /* Indirect Access Data 7 */ |
| 66 | #define KS8995_REG_IAD6 0x71 /* Indirect Access Data 6 */ |
| 67 | #define KS8995_REG_IAD5 0x72 /* Indirect Access Data 5 */ |
| 68 | #define KS8995_REG_IAD4 0x73 /* Indirect Access Data 4 */ |
| 69 | #define KS8995_REG_IAD3 0x74 /* Indirect Access Data 3 */ |
| 70 | #define KS8995_REG_IAD2 0x75 /* Indirect Access Data 2 */ |
| 71 | #define KS8995_REG_IAD1 0x76 /* Indirect Access Data 1 */ |
| 72 | #define KS8995_REG_IAD0 0x77 /* Indirect Access Data 0 */ |
| 73 | |
| 74 | #define KS8995_REGS_SIZE 0x80 |
| 75 | |
| 76 | #define ID1_CHIPID_M 0xf |
| 77 | #define ID1_CHIPID_S 4 |
| 78 | #define ID1_REVISION_M 0x7 |
| 79 | #define ID1_REVISION_S 1 |
| 80 | #define ID1_START_SW 1 /* start the switch */ |
| 81 | |
| 82 | #define FAMILY_KS8995 0x95 |
| 83 | #define CHIPID_M 0 |
| 84 | |
| 85 | #define KS8995_CMD_WRITE 0x02U |
| 86 | #define KS8995_CMD_READ 0x03U |
| 87 | |
| 88 | #define KS8995_RESET_DELAY 10 /* usec */ |
| 89 | |
| 90 | struct ks8995_pdata { |
| 91 | /* not yet implemented */ |
| 92 | }; |
| 93 | |
| 94 | struct ks8995_switch { |
| 95 | struct spi_device *spi; |
| 96 | struct mutex lock; |
| 97 | struct ks8995_pdata *pdata; |
| 98 | }; |
| 99 | |
| 100 | static inline u8 get_chip_id(u8 val) |
| 101 | { |
| 102 | return (val >> ID1_CHIPID_S) & ID1_CHIPID_M; |
| 103 | } |
| 104 | |
| 105 | static inline u8 get_chip_rev(u8 val) |
| 106 | { |
| 107 | return (val >> ID1_REVISION_S) & ID1_REVISION_M; |
| 108 | } |
| 109 | |
| 110 | /* ------------------------------------------------------------------------ */ |
| 111 | static int ks8995_read(struct ks8995_switch *ks, char *buf, |
| 112 | unsigned offset, size_t count) |
| 113 | { |
| 114 | u8 cmd[2]; |
| 115 | struct spi_transfer t[2]; |
| 116 | struct spi_message m; |
| 117 | int err; |
| 118 | |
| 119 | spi_message_init(&m); |
| 120 | |
| 121 | memset(&t, 0, sizeof(t)); |
| 122 | |
| 123 | t[0].tx_buf = cmd; |
| 124 | t[0].len = sizeof(cmd); |
| 125 | spi_message_add_tail(&t[0], &m); |
| 126 | |
| 127 | t[1].rx_buf = buf; |
| 128 | t[1].len = count; |
| 129 | spi_message_add_tail(&t[1], &m); |
| 130 | |
| 131 | cmd[0] = KS8995_CMD_READ; |
| 132 | cmd[1] = offset; |
| 133 | |
| 134 | mutex_lock(&ks->lock); |
| 135 | err = spi_sync(ks->spi, &m); |
| 136 | mutex_unlock(&ks->lock); |
| 137 | |
| 138 | return err ? err : count; |
| 139 | } |
| 140 | |
| 141 | |
| 142 | static int ks8995_write(struct ks8995_switch *ks, char *buf, |
| 143 | unsigned offset, size_t count) |
| 144 | { |
| 145 | u8 cmd[2]; |
| 146 | struct spi_transfer t[2]; |
| 147 | struct spi_message m; |
| 148 | int err; |
| 149 | |
| 150 | spi_message_init(&m); |
| 151 | |
| 152 | memset(&t, 0, sizeof(t)); |
| 153 | |
| 154 | t[0].tx_buf = cmd; |
| 155 | t[0].len = sizeof(cmd); |
| 156 | spi_message_add_tail(&t[0], &m); |
| 157 | |
| 158 | t[1].tx_buf = buf; |
| 159 | t[1].len = count; |
| 160 | spi_message_add_tail(&t[1], &m); |
| 161 | |
| 162 | cmd[0] = KS8995_CMD_WRITE; |
| 163 | cmd[1] = offset; |
| 164 | |
| 165 | mutex_lock(&ks->lock); |
| 166 | err = spi_sync(ks->spi, &m); |
| 167 | mutex_unlock(&ks->lock); |
| 168 | |
| 169 | return err ? err : count; |
| 170 | } |
| 171 | |
| 172 | static inline int ks8995_read_reg(struct ks8995_switch *ks, u8 addr, u8 *buf) |
| 173 | { |
Florian Fainelli | 7aff967 | 2013-12-17 21:38:13 -0800 | [diff] [blame^] | 174 | return ks8995_read(ks, buf, addr, 1) != 1; |
Frederic LAMBERT | a8e510f | 2011-12-18 07:33:41 +0000 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | static inline int ks8995_write_reg(struct ks8995_switch *ks, u8 addr, u8 val) |
| 178 | { |
| 179 | char buf = val; |
| 180 | |
Florian Fainelli | 7aff967 | 2013-12-17 21:38:13 -0800 | [diff] [blame^] | 181 | return ks8995_write(ks, &buf, addr, 1) != 1; |
Frederic LAMBERT | a8e510f | 2011-12-18 07:33:41 +0000 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | /* ------------------------------------------------------------------------ */ |
| 185 | |
| 186 | static int ks8995_stop(struct ks8995_switch *ks) |
| 187 | { |
| 188 | return ks8995_write_reg(ks, KS8995_REG_ID1, 0); |
| 189 | } |
| 190 | |
| 191 | static int ks8995_start(struct ks8995_switch *ks) |
| 192 | { |
| 193 | return ks8995_write_reg(ks, KS8995_REG_ID1, 1); |
| 194 | } |
| 195 | |
| 196 | static int ks8995_reset(struct ks8995_switch *ks) |
| 197 | { |
| 198 | int err; |
| 199 | |
| 200 | err = ks8995_stop(ks); |
| 201 | if (err) |
| 202 | return err; |
| 203 | |
| 204 | udelay(KS8995_RESET_DELAY); |
| 205 | |
| 206 | return ks8995_start(ks); |
| 207 | } |
| 208 | |
| 209 | /* ------------------------------------------------------------------------ */ |
| 210 | |
| 211 | static ssize_t ks8995_registers_read(struct file *filp, struct kobject *kobj, |
| 212 | struct bin_attribute *bin_attr, char *buf, loff_t off, size_t count) |
| 213 | { |
| 214 | struct device *dev; |
| 215 | struct ks8995_switch *ks8995; |
| 216 | |
| 217 | dev = container_of(kobj, struct device, kobj); |
| 218 | ks8995 = dev_get_drvdata(dev); |
| 219 | |
| 220 | if (unlikely(off > KS8995_REGS_SIZE)) |
| 221 | return 0; |
| 222 | |
| 223 | if ((off + count) > KS8995_REGS_SIZE) |
| 224 | count = KS8995_REGS_SIZE - off; |
| 225 | |
| 226 | if (unlikely(!count)) |
| 227 | return count; |
| 228 | |
| 229 | return ks8995_read(ks8995, buf, off, count); |
| 230 | } |
| 231 | |
| 232 | |
| 233 | static ssize_t ks8995_registers_write(struct file *filp, struct kobject *kobj, |
| 234 | struct bin_attribute *bin_attr, char *buf, loff_t off, size_t count) |
| 235 | { |
| 236 | struct device *dev; |
| 237 | struct ks8995_switch *ks8995; |
| 238 | |
| 239 | dev = container_of(kobj, struct device, kobj); |
| 240 | ks8995 = dev_get_drvdata(dev); |
| 241 | |
| 242 | if (unlikely(off >= KS8995_REGS_SIZE)) |
| 243 | return -EFBIG; |
| 244 | |
| 245 | if ((off + count) > KS8995_REGS_SIZE) |
| 246 | count = KS8995_REGS_SIZE - off; |
| 247 | |
| 248 | if (unlikely(!count)) |
| 249 | return count; |
| 250 | |
| 251 | return ks8995_write(ks8995, buf, off, count); |
| 252 | } |
| 253 | |
| 254 | |
| 255 | static struct bin_attribute ks8995_registers_attr = { |
| 256 | .attr = { |
| 257 | .name = "registers", |
| 258 | .mode = S_IRUSR | S_IWUSR, |
| 259 | }, |
| 260 | .size = KS8995_REGS_SIZE, |
| 261 | .read = ks8995_registers_read, |
| 262 | .write = ks8995_registers_write, |
| 263 | }; |
| 264 | |
| 265 | /* ------------------------------------------------------------------------ */ |
| 266 | |
Bill Pemberton | 633d159 | 2012-12-03 09:24:14 -0500 | [diff] [blame] | 267 | static int ks8995_probe(struct spi_device *spi) |
Frederic LAMBERT | a8e510f | 2011-12-18 07:33:41 +0000 | [diff] [blame] | 268 | { |
| 269 | struct ks8995_switch *ks; |
| 270 | struct ks8995_pdata *pdata; |
| 271 | u8 ids[2]; |
| 272 | int err; |
| 273 | |
| 274 | /* Chip description */ |
| 275 | pdata = spi->dev.platform_data; |
| 276 | |
| 277 | ks = kzalloc(sizeof(*ks), GFP_KERNEL); |
Joe Perches | e68ed8f | 2013-02-03 17:28:15 +0000 | [diff] [blame] | 278 | if (!ks) |
Frederic LAMBERT | a8e510f | 2011-12-18 07:33:41 +0000 | [diff] [blame] | 279 | return -ENOMEM; |
Frederic LAMBERT | a8e510f | 2011-12-18 07:33:41 +0000 | [diff] [blame] | 280 | |
| 281 | mutex_init(&ks->lock); |
| 282 | ks->pdata = pdata; |
| 283 | ks->spi = spi_dev_get(spi); |
Jingoo Han | 5d5f184 | 2013-04-05 20:37:07 +0000 | [diff] [blame] | 284 | spi_set_drvdata(spi, ks); |
Frederic LAMBERT | a8e510f | 2011-12-18 07:33:41 +0000 | [diff] [blame] | 285 | |
| 286 | spi->mode = SPI_MODE_0; |
| 287 | spi->bits_per_word = 8; |
| 288 | err = spi_setup(spi); |
| 289 | if (err) { |
| 290 | dev_err(&spi->dev, "spi_setup failed, err=%d\n", err); |
| 291 | goto err_drvdata; |
| 292 | } |
| 293 | |
| 294 | err = ks8995_read(ks, ids, KS8995_REG_ID0, sizeof(ids)); |
| 295 | if (err < 0) { |
| 296 | dev_err(&spi->dev, "unable to read id registers, err=%d\n", |
| 297 | err); |
| 298 | goto err_drvdata; |
| 299 | } |
| 300 | |
| 301 | switch (ids[0]) { |
| 302 | case FAMILY_KS8995: |
| 303 | break; |
| 304 | default: |
| 305 | dev_err(&spi->dev, "unknown family id:%02x\n", ids[0]); |
| 306 | err = -ENODEV; |
| 307 | goto err_drvdata; |
| 308 | } |
| 309 | |
| 310 | err = ks8995_reset(ks); |
| 311 | if (err) |
| 312 | goto err_drvdata; |
| 313 | |
| 314 | err = sysfs_create_bin_file(&spi->dev.kobj, &ks8995_registers_attr); |
| 315 | if (err) { |
| 316 | dev_err(&spi->dev, "unable to create sysfs file, err=%d\n", |
| 317 | err); |
| 318 | goto err_drvdata; |
| 319 | } |
| 320 | |
| 321 | dev_info(&spi->dev, "KS89%02X device found, Chip ID:%01x, " |
| 322 | "Revision:%01x\n", ids[0], |
| 323 | get_chip_id(ids[1]), get_chip_rev(ids[1])); |
| 324 | |
| 325 | return 0; |
| 326 | |
| 327 | err_drvdata: |
Frederic LAMBERT | a8e510f | 2011-12-18 07:33:41 +0000 | [diff] [blame] | 328 | kfree(ks); |
| 329 | return err; |
| 330 | } |
| 331 | |
Bill Pemberton | 633d159 | 2012-12-03 09:24:14 -0500 | [diff] [blame] | 332 | static int ks8995_remove(struct spi_device *spi) |
Frederic LAMBERT | a8e510f | 2011-12-18 07:33:41 +0000 | [diff] [blame] | 333 | { |
| 334 | struct ks8995_data *ks8995; |
| 335 | |
Jingoo Han | 5d5f184 | 2013-04-05 20:37:07 +0000 | [diff] [blame] | 336 | ks8995 = spi_get_drvdata(spi); |
Frederic LAMBERT | a8e510f | 2011-12-18 07:33:41 +0000 | [diff] [blame] | 337 | sysfs_remove_bin_file(&spi->dev.kobj, &ks8995_registers_attr); |
| 338 | |
Frederic LAMBERT | a8e510f | 2011-12-18 07:33:41 +0000 | [diff] [blame] | 339 | kfree(ks8995); |
| 340 | |
| 341 | return 0; |
| 342 | } |
| 343 | |
| 344 | /* ------------------------------------------------------------------------ */ |
| 345 | |
| 346 | static struct spi_driver ks8995_driver = { |
| 347 | .driver = { |
| 348 | .name = "spi-ks8995", |
Frederic LAMBERT | a8e510f | 2011-12-18 07:33:41 +0000 | [diff] [blame] | 349 | .owner = THIS_MODULE, |
| 350 | }, |
| 351 | .probe = ks8995_probe, |
Bill Pemberton | 633d159 | 2012-12-03 09:24:14 -0500 | [diff] [blame] | 352 | .remove = ks8995_remove, |
Frederic LAMBERT | a8e510f | 2011-12-18 07:33:41 +0000 | [diff] [blame] | 353 | }; |
| 354 | |
Sachin Kamat | 1a9561a | 2013-05-27 01:51:39 +0000 | [diff] [blame] | 355 | module_spi_driver(ks8995_driver); |
Frederic LAMBERT | a8e510f | 2011-12-18 07:33:41 +0000 | [diff] [blame] | 356 | |
| 357 | MODULE_DESCRIPTION(DRV_DESC); |
| 358 | MODULE_VERSION(DRV_VERSION); |
| 359 | MODULE_AUTHOR("Gabor Juhos <juhosg at openwrt.org>"); |
| 360 | MODULE_LICENSE("GPL v2"); |