Jeremy Kerr | 0508ad1 | 2017-02-01 10:53:41 -0600 | [diff] [blame] | 1 | /* |
| 2 | * FSI core driver |
| 3 | * |
| 4 | * Copyright (C) IBM Corporation 2016 |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License version 2 as |
| 8 | * published by the Free Software Foundation. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | */ |
| 15 | |
Jeremy Kerr | 2b545cd | 2017-06-06 16:08:42 -0500 | [diff] [blame] | 16 | #include <linux/crc4.h> |
Jeremy Kerr | 0508ad1 | 2017-02-01 10:53:41 -0600 | [diff] [blame] | 17 | #include <linux/device.h> |
| 18 | #include <linux/fsi.h> |
Jeremy Kerr | 09aecfa | 2017-06-06 16:08:36 -0500 | [diff] [blame] | 19 | #include <linux/idr.h> |
Jeremy Kerr | 0508ad1 | 2017-02-01 10:53:41 -0600 | [diff] [blame] | 20 | #include <linux/module.h> |
Jeremy Kerr | 2b545cd | 2017-06-06 16:08:42 -0500 | [diff] [blame] | 21 | #include <linux/slab.h> |
Jeremy Kerr | f7ade2a | 2017-06-06 16:08:44 -0500 | [diff] [blame] | 22 | #include <linux/bitops.h> |
Jeremy Kerr | 0508ad1 | 2017-02-01 10:53:41 -0600 | [diff] [blame] | 23 | |
Jeremy Kerr | 09aecfa | 2017-06-06 16:08:36 -0500 | [diff] [blame] | 24 | #include "fsi-master.h" |
| 25 | |
Jeremy Kerr | f7ade2a | 2017-06-06 16:08:44 -0500 | [diff] [blame] | 26 | #define FSI_SLAVE_CONF_NEXT_MASK GENMASK(31, 31) |
| 27 | #define FSI_SLAVE_CONF_SLOTS_MASK GENMASK(23, 16) |
| 28 | #define FSI_SLAVE_CONF_SLOTS_SHIFT 16 |
| 29 | #define FSI_SLAVE_CONF_VERSION_MASK GENMASK(15, 12) |
| 30 | #define FSI_SLAVE_CONF_VERSION_SHIFT 12 |
| 31 | #define FSI_SLAVE_CONF_TYPE_MASK GENMASK(11, 4) |
| 32 | #define FSI_SLAVE_CONF_TYPE_SHIFT 4 |
| 33 | #define FSI_SLAVE_CONF_CRC_SHIFT 4 |
| 34 | #define FSI_SLAVE_CONF_CRC_MASK GENMASK(3, 0) |
| 35 | #define FSI_SLAVE_CONF_DATA_BITS 28 |
| 36 | |
Jeremy Kerr | 4efe37f | 2017-06-06 16:08:45 -0500 | [diff] [blame] | 37 | #define FSI_PEEK_BASE 0x410 |
| 38 | |
Jeremy Kerr | f7ade2a | 2017-06-06 16:08:44 -0500 | [diff] [blame] | 39 | static const int engine_page_size = 0x400; |
| 40 | |
Christopher Bostic | 2b37c3e | 2017-06-06 16:08:43 -0500 | [diff] [blame] | 41 | #define FSI_SLAVE_BASE 0x800 |
| 42 | |
| 43 | /* |
| 44 | * FSI slave engine control register offsets |
| 45 | */ |
| 46 | #define FSI_SMODE 0x0 /* R/W: Mode register */ |
| 47 | |
| 48 | /* |
| 49 | * SMODE fields |
| 50 | */ |
| 51 | #define FSI_SMODE_WSC 0x80000000 /* Warm start done */ |
| 52 | #define FSI_SMODE_ECRC 0x20000000 /* Hw CRC check */ |
| 53 | #define FSI_SMODE_SID_SHIFT 24 /* ID shift */ |
| 54 | #define FSI_SMODE_SID_MASK 3 /* ID Mask */ |
| 55 | #define FSI_SMODE_ED_SHIFT 20 /* Echo delay shift */ |
| 56 | #define FSI_SMODE_ED_MASK 0xf /* Echo delay mask */ |
| 57 | #define FSI_SMODE_SD_SHIFT 16 /* Send delay shift */ |
| 58 | #define FSI_SMODE_SD_MASK 0xf /* Send delay mask */ |
| 59 | #define FSI_SMODE_LBCRR_SHIFT 8 /* Clk ratio shift */ |
| 60 | #define FSI_SMODE_LBCRR_MASK 0xf /* Clk ratio mask */ |
| 61 | |
Jeremy Kerr | 2b545cd | 2017-06-06 16:08:42 -0500 | [diff] [blame] | 62 | #define FSI_SLAVE_SIZE_23b 0x800000 |
| 63 | |
Jeremy Kerr | 09aecfa | 2017-06-06 16:08:36 -0500 | [diff] [blame] | 64 | static DEFINE_IDA(master_ida); |
| 65 | |
Jeremy Kerr | faf0b116 | 2017-06-06 16:08:37 -0500 | [diff] [blame] | 66 | struct fsi_slave { |
| 67 | struct device dev; |
| 68 | struct fsi_master *master; |
| 69 | int id; |
| 70 | int link; |
| 71 | uint32_t size; /* size of slave address space */ |
| 72 | }; |
| 73 | |
Christopher Bostic | cd0fdb5 | 2017-06-06 16:08:46 -0500 | [diff] [blame] | 74 | #define to_fsi_master(d) container_of(d, struct fsi_master, dev) |
Jeremy Kerr | faf0b116 | 2017-06-06 16:08:37 -0500 | [diff] [blame] | 75 | #define to_fsi_slave(d) container_of(d, struct fsi_slave, dev) |
| 76 | |
Jeremy Kerr | 014c2ab | 2017-06-06 16:08:40 -0500 | [diff] [blame] | 77 | static int fsi_master_read(struct fsi_master *master, int link, |
| 78 | uint8_t slave_id, uint32_t addr, void *val, size_t size); |
| 79 | static int fsi_master_write(struct fsi_master *master, int link, |
| 80 | uint8_t slave_id, uint32_t addr, const void *val, size_t size); |
Jeremy Kerr | 4efe37f | 2017-06-06 16:08:45 -0500 | [diff] [blame] | 81 | static int fsi_slave_read(struct fsi_slave *slave, uint32_t addr, |
| 82 | void *val, size_t size); |
| 83 | static int fsi_slave_write(struct fsi_slave *slave, uint32_t addr, |
| 84 | const void *val, size_t size); |
Jeremy Kerr | 014c2ab | 2017-06-06 16:08:40 -0500 | [diff] [blame] | 85 | |
Jeremy Kerr | 4efe37f | 2017-06-06 16:08:45 -0500 | [diff] [blame] | 86 | /* |
| 87 | * fsi_device_read() / fsi_device_write() / fsi_device_peek() |
| 88 | * |
| 89 | * FSI endpoint-device support |
| 90 | * |
| 91 | * Read / write / peek accessors for a client |
| 92 | * |
| 93 | * Parameters: |
| 94 | * dev: Structure passed to FSI client device drivers on probe(). |
| 95 | * addr: FSI address of given device. Client should pass in its base address |
| 96 | * plus desired offset to access its register space. |
| 97 | * val: For read/peek this is the value read at the specified address. For |
| 98 | * write this is value to write to the specified address. |
| 99 | * The data in val must be FSI bus endian (big endian). |
| 100 | * size: Size in bytes of the operation. Sizes supported are 1, 2 and 4 bytes. |
| 101 | * Addresses must be aligned on size boundaries or an error will result. |
| 102 | */ |
| 103 | int fsi_device_read(struct fsi_device *dev, uint32_t addr, void *val, |
| 104 | size_t size) |
| 105 | { |
| 106 | if (addr > dev->size || size > dev->size || addr > dev->size - size) |
| 107 | return -EINVAL; |
| 108 | |
| 109 | return fsi_slave_read(dev->slave, dev->addr + addr, val, size); |
| 110 | } |
| 111 | EXPORT_SYMBOL_GPL(fsi_device_read); |
| 112 | |
| 113 | int fsi_device_write(struct fsi_device *dev, uint32_t addr, const void *val, |
| 114 | size_t size) |
| 115 | { |
| 116 | if (addr > dev->size || size > dev->size || addr > dev->size - size) |
| 117 | return -EINVAL; |
| 118 | |
| 119 | return fsi_slave_write(dev->slave, dev->addr + addr, val, size); |
| 120 | } |
| 121 | EXPORT_SYMBOL_GPL(fsi_device_write); |
| 122 | |
| 123 | int fsi_device_peek(struct fsi_device *dev, void *val) |
| 124 | { |
| 125 | uint32_t addr = FSI_PEEK_BASE + ((dev->unit - 2) * sizeof(uint32_t)); |
| 126 | |
| 127 | return fsi_slave_read(dev->slave, addr, val, sizeof(uint32_t)); |
| 128 | } |
Jeremy Kerr | f7ade2a | 2017-06-06 16:08:44 -0500 | [diff] [blame] | 129 | |
| 130 | static void fsi_device_release(struct device *_device) |
| 131 | { |
| 132 | struct fsi_device *device = to_fsi_dev(_device); |
| 133 | |
| 134 | kfree(device); |
| 135 | } |
| 136 | |
| 137 | static struct fsi_device *fsi_create_device(struct fsi_slave *slave) |
| 138 | { |
| 139 | struct fsi_device *dev; |
| 140 | |
| 141 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); |
| 142 | if (!dev) |
| 143 | return NULL; |
| 144 | |
| 145 | dev->dev.parent = &slave->dev; |
| 146 | dev->dev.bus = &fsi_bus_type; |
| 147 | dev->dev.release = fsi_device_release; |
| 148 | |
| 149 | return dev; |
| 150 | } |
| 151 | |
Jeremy Kerr | 414c102 | 2017-06-06 16:08:38 -0500 | [diff] [blame] | 152 | /* FSI slave support */ |
Jeremy Kerr | 014c2ab | 2017-06-06 16:08:40 -0500 | [diff] [blame] | 153 | static int fsi_slave_calc_addr(struct fsi_slave *slave, uint32_t *addrp, |
| 154 | uint8_t *idp) |
| 155 | { |
| 156 | uint32_t addr = *addrp; |
| 157 | uint8_t id = *idp; |
| 158 | |
| 159 | if (addr > slave->size) |
| 160 | return -EINVAL; |
| 161 | |
| 162 | /* For 23 bit addressing, we encode the extra two bits in the slave |
| 163 | * id (and the slave's actual ID needs to be 0). |
| 164 | */ |
| 165 | if (addr > 0x1fffff) { |
| 166 | if (slave->id != 0) |
| 167 | return -EINVAL; |
| 168 | id = (addr >> 21) & 0x3; |
| 169 | addr &= 0x1fffff; |
| 170 | } |
| 171 | |
| 172 | *addrp = addr; |
| 173 | *idp = id; |
| 174 | return 0; |
| 175 | } |
| 176 | |
| 177 | static int fsi_slave_read(struct fsi_slave *slave, uint32_t addr, |
| 178 | void *val, size_t size) |
| 179 | { |
| 180 | uint8_t id = slave->id; |
| 181 | int rc; |
| 182 | |
| 183 | rc = fsi_slave_calc_addr(slave, &addr, &id); |
| 184 | if (rc) |
| 185 | return rc; |
| 186 | |
| 187 | return fsi_master_read(slave->master, slave->link, id, |
| 188 | addr, val, size); |
| 189 | } |
| 190 | |
| 191 | static int fsi_slave_write(struct fsi_slave *slave, uint32_t addr, |
| 192 | const void *val, size_t size) |
| 193 | { |
| 194 | uint8_t id = slave->id; |
| 195 | int rc; |
| 196 | |
| 197 | rc = fsi_slave_calc_addr(slave, &addr, &id); |
| 198 | if (rc) |
| 199 | return rc; |
| 200 | |
| 201 | return fsi_master_write(slave->master, slave->link, id, |
| 202 | addr, val, size); |
| 203 | } |
| 204 | |
Jeremy Kerr | f7ade2a | 2017-06-06 16:08:44 -0500 | [diff] [blame] | 205 | static int fsi_slave_scan(struct fsi_slave *slave) |
| 206 | { |
| 207 | uint32_t engine_addr; |
| 208 | uint32_t conf; |
| 209 | int rc, i; |
| 210 | |
| 211 | /* |
| 212 | * scan engines |
| 213 | * |
| 214 | * We keep the peek mode and slave engines for the core; so start |
| 215 | * at the third slot in the configuration table. We also need to |
| 216 | * skip the chip ID entry at the start of the address space. |
| 217 | */ |
| 218 | engine_addr = engine_page_size * 3; |
| 219 | for (i = 2; i < engine_page_size / sizeof(uint32_t); i++) { |
| 220 | uint8_t slots, version, type, crc; |
| 221 | struct fsi_device *dev; |
| 222 | |
| 223 | rc = fsi_slave_read(slave, (i + 1) * sizeof(conf), |
| 224 | &conf, sizeof(conf)); |
| 225 | if (rc) { |
| 226 | dev_warn(&slave->dev, |
| 227 | "error reading slave registers\n"); |
| 228 | return -1; |
| 229 | } |
| 230 | conf = be32_to_cpu(conf); |
| 231 | |
| 232 | crc = crc4(0, conf, 32); |
| 233 | if (crc) { |
| 234 | dev_warn(&slave->dev, |
| 235 | "crc error in slave register at 0x%04x\n", |
| 236 | i); |
| 237 | return -1; |
| 238 | } |
| 239 | |
| 240 | slots = (conf & FSI_SLAVE_CONF_SLOTS_MASK) |
| 241 | >> FSI_SLAVE_CONF_SLOTS_SHIFT; |
| 242 | version = (conf & FSI_SLAVE_CONF_VERSION_MASK) |
| 243 | >> FSI_SLAVE_CONF_VERSION_SHIFT; |
| 244 | type = (conf & FSI_SLAVE_CONF_TYPE_MASK) |
| 245 | >> FSI_SLAVE_CONF_TYPE_SHIFT; |
| 246 | |
| 247 | /* |
| 248 | * Unused address areas are marked by a zero type value; this |
| 249 | * skips the defined address areas |
| 250 | */ |
| 251 | if (type != 0 && slots != 0) { |
| 252 | |
| 253 | /* create device */ |
| 254 | dev = fsi_create_device(slave); |
| 255 | if (!dev) |
| 256 | return -ENOMEM; |
| 257 | |
| 258 | dev->slave = slave; |
| 259 | dev->engine_type = type; |
| 260 | dev->version = version; |
| 261 | dev->unit = i; |
| 262 | dev->addr = engine_addr; |
| 263 | dev->size = slots * engine_page_size; |
| 264 | |
| 265 | dev_dbg(&slave->dev, |
| 266 | "engine[%i]: type %x, version %x, addr %x size %x\n", |
| 267 | dev->unit, dev->engine_type, version, |
| 268 | dev->addr, dev->size); |
| 269 | |
| 270 | dev_set_name(&dev->dev, "%02x:%02x:%02x:%02x", |
| 271 | slave->master->idx, slave->link, |
| 272 | slave->id, i - 2); |
| 273 | |
| 274 | rc = device_register(&dev->dev); |
| 275 | if (rc) { |
| 276 | dev_warn(&slave->dev, "add failed: %d\n", rc); |
| 277 | put_device(&dev->dev); |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | engine_addr += slots * engine_page_size; |
| 282 | |
| 283 | if (!(conf & FSI_SLAVE_CONF_NEXT_MASK)) |
| 284 | break; |
| 285 | } |
| 286 | |
| 287 | return 0; |
| 288 | } |
| 289 | |
Christopher Bostic | 2b37c3e | 2017-06-06 16:08:43 -0500 | [diff] [blame] | 290 | /* Encode slave local bus echo delay */ |
| 291 | static inline uint32_t fsi_smode_echodly(int x) |
| 292 | { |
| 293 | return (x & FSI_SMODE_ED_MASK) << FSI_SMODE_ED_SHIFT; |
| 294 | } |
| 295 | |
| 296 | /* Encode slave local bus send delay */ |
| 297 | static inline uint32_t fsi_smode_senddly(int x) |
| 298 | { |
| 299 | return (x & FSI_SMODE_SD_MASK) << FSI_SMODE_SD_SHIFT; |
| 300 | } |
| 301 | |
| 302 | /* Encode slave local bus clock rate ratio */ |
| 303 | static inline uint32_t fsi_smode_lbcrr(int x) |
| 304 | { |
| 305 | return (x & FSI_SMODE_LBCRR_MASK) << FSI_SMODE_LBCRR_SHIFT; |
| 306 | } |
| 307 | |
| 308 | /* Encode slave ID */ |
| 309 | static inline uint32_t fsi_smode_sid(int x) |
| 310 | { |
| 311 | return (x & FSI_SMODE_SID_MASK) << FSI_SMODE_SID_SHIFT; |
| 312 | } |
| 313 | |
| 314 | static const uint32_t fsi_slave_smode(int id) |
| 315 | { |
| 316 | return FSI_SMODE_WSC | FSI_SMODE_ECRC |
| 317 | | fsi_smode_sid(id) |
| 318 | | fsi_smode_echodly(0xf) | fsi_smode_senddly(0xf) |
| 319 | | fsi_smode_lbcrr(0x8); |
| 320 | } |
| 321 | |
| 322 | static int fsi_slave_set_smode(struct fsi_master *master, int link, int id) |
| 323 | { |
| 324 | uint32_t smode; |
| 325 | |
| 326 | /* set our smode register with the slave ID field to 0; this enables |
| 327 | * extended slave addressing |
| 328 | */ |
| 329 | smode = fsi_slave_smode(id); |
| 330 | smode = cpu_to_be32(smode); |
| 331 | |
| 332 | return fsi_master_write(master, link, id, FSI_SLAVE_BASE + FSI_SMODE, |
| 333 | &smode, sizeof(smode)); |
| 334 | } |
| 335 | |
Jeremy Kerr | 2b545cd | 2017-06-06 16:08:42 -0500 | [diff] [blame] | 336 | static void fsi_slave_release(struct device *dev) |
| 337 | { |
| 338 | struct fsi_slave *slave = to_fsi_slave(dev); |
| 339 | |
| 340 | kfree(slave); |
| 341 | } |
| 342 | |
Jeremy Kerr | 414c102 | 2017-06-06 16:08:38 -0500 | [diff] [blame] | 343 | static int fsi_slave_init(struct fsi_master *master, int link, uint8_t id) |
| 344 | { |
Jeremy Kerr | 2b545cd | 2017-06-06 16:08:42 -0500 | [diff] [blame] | 345 | struct fsi_slave *slave; |
| 346 | uint32_t chip_id; |
| 347 | uint8_t crc; |
| 348 | int rc; |
Jeremy Kerr | 414c102 | 2017-06-06 16:08:38 -0500 | [diff] [blame] | 349 | |
Jeremy Kerr | 2b545cd | 2017-06-06 16:08:42 -0500 | [diff] [blame] | 350 | /* Currently, we only support single slaves on a link, and use the |
| 351 | * full 23-bit address range |
| 352 | */ |
| 353 | if (id != 0) |
| 354 | return -EINVAL; |
| 355 | |
| 356 | rc = fsi_master_read(master, link, id, 0, &chip_id, sizeof(chip_id)); |
| 357 | if (rc) { |
| 358 | dev_dbg(&master->dev, "can't read slave %02x:%02x %d\n", |
| 359 | link, id, rc); |
| 360 | return -ENODEV; |
| 361 | } |
| 362 | chip_id = be32_to_cpu(chip_id); |
| 363 | |
| 364 | crc = crc4(0, chip_id, 32); |
| 365 | if (crc) { |
| 366 | dev_warn(&master->dev, "slave %02x:%02x invalid chip id CRC!\n", |
| 367 | link, id); |
| 368 | return -EIO; |
| 369 | } |
| 370 | |
| 371 | dev_info(&master->dev, "fsi: found chip %08x at %02x:%02x:%02x\n", |
| 372 | chip_id, master->idx, link, id); |
| 373 | |
Christopher Bostic | 2b37c3e | 2017-06-06 16:08:43 -0500 | [diff] [blame] | 374 | rc = fsi_slave_set_smode(master, link, id); |
| 375 | if (rc) { |
| 376 | dev_warn(&master->dev, |
| 377 | "can't set smode on slave:%02x:%02x %d\n", |
| 378 | link, id, rc); |
| 379 | return -ENODEV; |
| 380 | } |
| 381 | |
Jeremy Kerr | 2b545cd | 2017-06-06 16:08:42 -0500 | [diff] [blame] | 382 | /* We can communicate with a slave; create the slave device and |
| 383 | * register. |
| 384 | */ |
| 385 | slave = kzalloc(sizeof(*slave), GFP_KERNEL); |
| 386 | if (!slave) |
| 387 | return -ENOMEM; |
| 388 | |
| 389 | slave->master = master; |
| 390 | slave->dev.parent = &master->dev; |
| 391 | slave->dev.release = fsi_slave_release; |
| 392 | slave->link = link; |
| 393 | slave->id = id; |
| 394 | slave->size = FSI_SLAVE_SIZE_23b; |
| 395 | |
| 396 | dev_set_name(&slave->dev, "slave@%02x:%02x", link, id); |
| 397 | rc = device_register(&slave->dev); |
| 398 | if (rc < 0) { |
| 399 | dev_warn(&master->dev, "failed to create slave device: %d\n", |
| 400 | rc); |
| 401 | put_device(&slave->dev); |
| 402 | return rc; |
| 403 | } |
| 404 | |
Jeremy Kerr | f7ade2a | 2017-06-06 16:08:44 -0500 | [diff] [blame] | 405 | rc = fsi_slave_scan(slave); |
| 406 | if (rc) |
| 407 | dev_dbg(&master->dev, "failed during slave scan with: %d\n", |
| 408 | rc); |
Jeremy Kerr | 2b545cd | 2017-06-06 16:08:42 -0500 | [diff] [blame] | 409 | |
| 410 | return rc; |
Jeremy Kerr | 414c102 | 2017-06-06 16:08:38 -0500 | [diff] [blame] | 411 | } |
| 412 | |
Jeremy Kerr | 09aecfa | 2017-06-06 16:08:36 -0500 | [diff] [blame] | 413 | /* FSI master support */ |
Jeremy Kerr | 014c2ab | 2017-06-06 16:08:40 -0500 | [diff] [blame] | 414 | static int fsi_check_access(uint32_t addr, size_t size) |
| 415 | { |
| 416 | if (size != 1 && size != 2 && size != 4) |
| 417 | return -EINVAL; |
| 418 | |
| 419 | if ((addr & 0x3) != (size & 0x3)) |
| 420 | return -EINVAL; |
| 421 | |
| 422 | return 0; |
| 423 | } |
| 424 | |
| 425 | static int fsi_master_read(struct fsi_master *master, int link, |
| 426 | uint8_t slave_id, uint32_t addr, void *val, size_t size) |
| 427 | { |
| 428 | int rc; |
| 429 | |
| 430 | rc = fsi_check_access(addr, size); |
| 431 | if (rc) |
| 432 | return rc; |
| 433 | |
| 434 | return master->read(master, link, slave_id, addr, val, size); |
| 435 | } |
| 436 | |
| 437 | static int fsi_master_write(struct fsi_master *master, int link, |
| 438 | uint8_t slave_id, uint32_t addr, const void *val, size_t size) |
| 439 | { |
| 440 | int rc; |
| 441 | |
| 442 | rc = fsi_check_access(addr, size); |
| 443 | if (rc) |
| 444 | return rc; |
| 445 | |
| 446 | return master->write(master, link, slave_id, addr, val, size); |
| 447 | } |
| 448 | |
Christopher Bostic | 2609528 | 2017-06-06 16:08:41 -0500 | [diff] [blame] | 449 | static int fsi_master_link_enable(struct fsi_master *master, int link) |
| 450 | { |
| 451 | if (master->link_enable) |
| 452 | return master->link_enable(master, link); |
| 453 | |
| 454 | return 0; |
| 455 | } |
| 456 | |
| 457 | /* |
| 458 | * Issue a break command on this link |
| 459 | */ |
| 460 | static int fsi_master_break(struct fsi_master *master, int link) |
| 461 | { |
| 462 | if (master->send_break) |
| 463 | return master->send_break(master, link); |
| 464 | |
| 465 | return 0; |
| 466 | } |
| 467 | |
Jeremy Kerr | 414c102 | 2017-06-06 16:08:38 -0500 | [diff] [blame] | 468 | static int fsi_master_scan(struct fsi_master *master) |
| 469 | { |
Christopher Bostic | 2609528 | 2017-06-06 16:08:41 -0500 | [diff] [blame] | 470 | int link, rc; |
Jeremy Kerr | 414c102 | 2017-06-06 16:08:38 -0500 | [diff] [blame] | 471 | |
Christopher Bostic | 2609528 | 2017-06-06 16:08:41 -0500 | [diff] [blame] | 472 | for (link = 0; link < master->n_links; link++) { |
| 473 | rc = fsi_master_link_enable(master, link); |
| 474 | if (rc) { |
| 475 | dev_dbg(&master->dev, |
| 476 | "enable link %d failed: %d\n", link, rc); |
| 477 | continue; |
| 478 | } |
| 479 | rc = fsi_master_break(master, link); |
| 480 | if (rc) { |
| 481 | dev_dbg(&master->dev, |
| 482 | "break to link %d failed: %d\n", link, rc); |
| 483 | continue; |
| 484 | } |
| 485 | |
Jeremy Kerr | 414c102 | 2017-06-06 16:08:38 -0500 | [diff] [blame] | 486 | fsi_slave_init(master, link, 0); |
Christopher Bostic | 2609528 | 2017-06-06 16:08:41 -0500 | [diff] [blame] | 487 | } |
Jeremy Kerr | 414c102 | 2017-06-06 16:08:38 -0500 | [diff] [blame] | 488 | |
| 489 | return 0; |
| 490 | } |
| 491 | |
Christopher Bostic | cd0fdb5 | 2017-06-06 16:08:46 -0500 | [diff] [blame] | 492 | static int fsi_slave_remove_device(struct device *dev, void *arg) |
| 493 | { |
| 494 | device_unregister(dev); |
| 495 | return 0; |
| 496 | } |
| 497 | |
| 498 | static int fsi_master_remove_slave(struct device *dev, void *arg) |
| 499 | { |
| 500 | device_for_each_child(dev, NULL, fsi_slave_remove_device); |
| 501 | device_unregister(dev); |
| 502 | return 0; |
| 503 | } |
| 504 | |
| 505 | static void fsi_master_unscan(struct fsi_master *master) |
| 506 | { |
| 507 | device_for_each_child(&master->dev, NULL, fsi_master_remove_slave); |
| 508 | } |
| 509 | |
| 510 | static ssize_t master_rescan_store(struct device *dev, |
| 511 | struct device_attribute *attr, const char *buf, size_t count) |
| 512 | { |
| 513 | struct fsi_master *master = to_fsi_master(dev); |
| 514 | int rc; |
| 515 | |
| 516 | fsi_master_unscan(master); |
| 517 | rc = fsi_master_scan(master); |
| 518 | if (rc < 0) |
| 519 | return rc; |
| 520 | |
| 521 | return count; |
| 522 | } |
| 523 | |
| 524 | static DEVICE_ATTR(rescan, 0200, NULL, master_rescan_store); |
| 525 | |
Jeremy Kerr | 09aecfa | 2017-06-06 16:08:36 -0500 | [diff] [blame] | 526 | int fsi_master_register(struct fsi_master *master) |
| 527 | { |
| 528 | int rc; |
| 529 | |
| 530 | if (!master) |
| 531 | return -EINVAL; |
| 532 | |
| 533 | master->idx = ida_simple_get(&master_ida, 0, INT_MAX, GFP_KERNEL); |
| 534 | dev_set_name(&master->dev, "fsi%d", master->idx); |
| 535 | |
| 536 | rc = device_register(&master->dev); |
Jeremy Kerr | 414c102 | 2017-06-06 16:08:38 -0500 | [diff] [blame] | 537 | if (rc) { |
Jeremy Kerr | 09aecfa | 2017-06-06 16:08:36 -0500 | [diff] [blame] | 538 | ida_simple_remove(&master_ida, master->idx); |
Jeremy Kerr | 414c102 | 2017-06-06 16:08:38 -0500 | [diff] [blame] | 539 | return rc; |
| 540 | } |
Jeremy Kerr | 09aecfa | 2017-06-06 16:08:36 -0500 | [diff] [blame] | 541 | |
Christopher Bostic | cd0fdb5 | 2017-06-06 16:08:46 -0500 | [diff] [blame] | 542 | rc = device_create_file(&master->dev, &dev_attr_rescan); |
| 543 | if (rc) { |
| 544 | device_unregister(&master->dev); |
| 545 | ida_simple_remove(&master_ida, master->idx); |
| 546 | return rc; |
| 547 | } |
| 548 | |
Jeremy Kerr | 414c102 | 2017-06-06 16:08:38 -0500 | [diff] [blame] | 549 | fsi_master_scan(master); |
Christopher Bostic | cd0fdb5 | 2017-06-06 16:08:46 -0500 | [diff] [blame] | 550 | |
Jeremy Kerr | 414c102 | 2017-06-06 16:08:38 -0500 | [diff] [blame] | 551 | return 0; |
Jeremy Kerr | 09aecfa | 2017-06-06 16:08:36 -0500 | [diff] [blame] | 552 | } |
| 553 | EXPORT_SYMBOL_GPL(fsi_master_register); |
| 554 | |
| 555 | void fsi_master_unregister(struct fsi_master *master) |
| 556 | { |
| 557 | if (master->idx >= 0) { |
| 558 | ida_simple_remove(&master_ida, master->idx); |
| 559 | master->idx = -1; |
| 560 | } |
| 561 | |
Christopher Bostic | cd0fdb5 | 2017-06-06 16:08:46 -0500 | [diff] [blame] | 562 | fsi_master_unscan(master); |
Jeremy Kerr | 09aecfa | 2017-06-06 16:08:36 -0500 | [diff] [blame] | 563 | device_unregister(&master->dev); |
| 564 | } |
| 565 | EXPORT_SYMBOL_GPL(fsi_master_unregister); |
| 566 | |
Jeremy Kerr | 0508ad1 | 2017-02-01 10:53:41 -0600 | [diff] [blame] | 567 | /* FSI core & Linux bus type definitions */ |
| 568 | |
Jeremy Kerr | dd37eed | 2017-02-01 10:53:43 -0600 | [diff] [blame] | 569 | static int fsi_bus_match(struct device *dev, struct device_driver *drv) |
| 570 | { |
| 571 | struct fsi_device *fsi_dev = to_fsi_dev(dev); |
| 572 | struct fsi_driver *fsi_drv = to_fsi_drv(drv); |
| 573 | const struct fsi_device_id *id; |
| 574 | |
| 575 | if (!fsi_drv->id_table) |
| 576 | return 0; |
| 577 | |
| 578 | for (id = fsi_drv->id_table; id->engine_type; id++) { |
| 579 | if (id->engine_type != fsi_dev->engine_type) |
| 580 | continue; |
| 581 | if (id->version == FSI_VERSION_ANY || |
| 582 | id->version == fsi_dev->version) |
| 583 | return 1; |
| 584 | } |
| 585 | |
| 586 | return 0; |
| 587 | } |
| 588 | |
Christopher Bostic | 356d800 | 2017-06-06 16:08:48 -0500 | [diff] [blame^] | 589 | int fsi_driver_register(struct fsi_driver *fsi_drv) |
| 590 | { |
| 591 | if (!fsi_drv) |
| 592 | return -EINVAL; |
| 593 | if (!fsi_drv->id_table) |
| 594 | return -EINVAL; |
| 595 | |
| 596 | return driver_register(&fsi_drv->drv); |
| 597 | } |
| 598 | EXPORT_SYMBOL_GPL(fsi_driver_register); |
| 599 | |
| 600 | void fsi_driver_unregister(struct fsi_driver *fsi_drv) |
| 601 | { |
| 602 | driver_unregister(&fsi_drv->drv); |
| 603 | } |
| 604 | EXPORT_SYMBOL_GPL(fsi_driver_unregister); |
| 605 | |
Jeremy Kerr | 0508ad1 | 2017-02-01 10:53:41 -0600 | [diff] [blame] | 606 | struct bus_type fsi_bus_type = { |
| 607 | .name = "fsi", |
Jeremy Kerr | dd37eed | 2017-02-01 10:53:43 -0600 | [diff] [blame] | 608 | .match = fsi_bus_match, |
Jeremy Kerr | 0508ad1 | 2017-02-01 10:53:41 -0600 | [diff] [blame] | 609 | }; |
| 610 | EXPORT_SYMBOL_GPL(fsi_bus_type); |
| 611 | |
| 612 | static int fsi_init(void) |
| 613 | { |
| 614 | return bus_register(&fsi_bus_type); |
| 615 | } |
| 616 | |
| 617 | static void fsi_exit(void) |
| 618 | { |
| 619 | bus_unregister(&fsi_bus_type); |
| 620 | } |
| 621 | |
| 622 | module_init(fsi_init); |
| 623 | module_exit(fsi_exit); |