Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * FSI hub master driver
0004  *
0005  * Copyright (C) IBM Corporation 2016
0006  */
0007 
0008 #include <linux/delay.h>
0009 #include <linux/fsi.h>
0010 #include <linux/module.h>
0011 #include <linux/of.h>
0012 #include <linux/slab.h>
0013 
0014 #include "fsi-master.h"
0015 
0016 #define FSI_ENGID_HUB_MASTER        0x1c
0017 
0018 #define FSI_LINK_ENABLE_SETUP_TIME  10  /* in mS */
0019 
0020 /*
0021  * FSI hub master support
0022  *
0023  * A hub master increases the number of potential target devices that the
0024  * primary FSI master can access. For each link a primary master supports,
0025  * each of those links can in turn be chained to a hub master with multiple
0026  * links of its own.
0027  *
0028  * The hub is controlled by a set of control registers exposed as a regular fsi
0029  * device (the hub->upstream device), and provides access to the downstream FSI
0030  * bus as through an address range on the slave itself (->addr and ->size).
0031  *
0032  * [This differs from "cascaded" masters, which expose the entire downstream
0033  * bus entirely through the fsi device address range, and so have a smaller
0034  * accessible address space.]
0035  */
0036 struct fsi_master_hub {
0037     struct fsi_master   master;
0038     struct fsi_device   *upstream;
0039     uint32_t        addr, size; /* slave-relative addr of */
0040                         /* master address space */
0041 };
0042 
0043 #define to_fsi_master_hub(m) container_of(m, struct fsi_master_hub, master)
0044 
0045 static int hub_master_read(struct fsi_master *master, int link,
0046             uint8_t id, uint32_t addr, void *val, size_t size)
0047 {
0048     struct fsi_master_hub *hub = to_fsi_master_hub(master);
0049 
0050     if (id != 0)
0051         return -EINVAL;
0052 
0053     addr += hub->addr + (link * FSI_HUB_LINK_SIZE);
0054     return fsi_slave_read(hub->upstream->slave, addr, val, size);
0055 }
0056 
0057 static int hub_master_write(struct fsi_master *master, int link,
0058             uint8_t id, uint32_t addr, const void *val, size_t size)
0059 {
0060     struct fsi_master_hub *hub = to_fsi_master_hub(master);
0061 
0062     if (id != 0)
0063         return -EINVAL;
0064 
0065     addr += hub->addr + (link * FSI_HUB_LINK_SIZE);
0066     return fsi_slave_write(hub->upstream->slave, addr, val, size);
0067 }
0068 
0069 static int hub_master_break(struct fsi_master *master, int link)
0070 {
0071     uint32_t addr;
0072     __be32 cmd;
0073 
0074     addr = 0x4;
0075     cmd = cpu_to_be32(0xc0de0000);
0076 
0077     return hub_master_write(master, link, 0, addr, &cmd, sizeof(cmd));
0078 }
0079 
0080 static int hub_master_link_enable(struct fsi_master *master, int link,
0081                   bool enable)
0082 {
0083     struct fsi_master_hub *hub = to_fsi_master_hub(master);
0084     int idx, bit;
0085     __be32 reg;
0086     int rc;
0087 
0088     idx = link / 32;
0089     bit = link % 32;
0090 
0091     reg = cpu_to_be32(0x80000000 >> bit);
0092 
0093     if (!enable)
0094         return fsi_device_write(hub->upstream, FSI_MCENP0 + (4 * idx),
0095                     &reg, 4);
0096 
0097     rc = fsi_device_write(hub->upstream, FSI_MSENP0 + (4 * idx), &reg, 4);
0098     if (rc)
0099         return rc;
0100 
0101     mdelay(FSI_LINK_ENABLE_SETUP_TIME);
0102 
0103     return 0;
0104 }
0105 
0106 static void hub_master_release(struct device *dev)
0107 {
0108     struct fsi_master_hub *hub = to_fsi_master_hub(dev_to_fsi_master(dev));
0109 
0110     kfree(hub);
0111 }
0112 
0113 /* mmode encoders */
0114 static inline u32 fsi_mmode_crs0(u32 x)
0115 {
0116     return (x & FSI_MMODE_CRS0MASK) << FSI_MMODE_CRS0SHFT;
0117 }
0118 
0119 static inline u32 fsi_mmode_crs1(u32 x)
0120 {
0121     return (x & FSI_MMODE_CRS1MASK) << FSI_MMODE_CRS1SHFT;
0122 }
0123 
0124 static int hub_master_init(struct fsi_master_hub *hub)
0125 {
0126     struct fsi_device *dev = hub->upstream;
0127     __be32 reg;
0128     int rc;
0129 
0130     reg = cpu_to_be32(FSI_MRESP_RST_ALL_MASTER | FSI_MRESP_RST_ALL_LINK
0131             | FSI_MRESP_RST_MCR | FSI_MRESP_RST_PYE);
0132     rc = fsi_device_write(dev, FSI_MRESP0, &reg, sizeof(reg));
0133     if (rc)
0134         return rc;
0135 
0136     /* Initialize the MFSI (hub master) engine */
0137     reg = cpu_to_be32(FSI_MRESP_RST_ALL_MASTER | FSI_MRESP_RST_ALL_LINK
0138             | FSI_MRESP_RST_MCR | FSI_MRESP_RST_PYE);
0139     rc = fsi_device_write(dev, FSI_MRESP0, &reg, sizeof(reg));
0140     if (rc)
0141         return rc;
0142 
0143     reg = cpu_to_be32(FSI_MECTRL_EOAE | FSI_MECTRL_P8_AUTO_TERM);
0144     rc = fsi_device_write(dev, FSI_MECTRL, &reg, sizeof(reg));
0145     if (rc)
0146         return rc;
0147 
0148     reg = cpu_to_be32(FSI_MMODE_EIP | FSI_MMODE_ECRC | FSI_MMODE_EPC
0149             | fsi_mmode_crs0(1) | fsi_mmode_crs1(1)
0150             | FSI_MMODE_P8_TO_LSB);
0151     rc = fsi_device_write(dev, FSI_MMODE, &reg, sizeof(reg));
0152     if (rc)
0153         return rc;
0154 
0155     reg = cpu_to_be32(0xffff0000);
0156     rc = fsi_device_write(dev, FSI_MDLYR, &reg, sizeof(reg));
0157     if (rc)
0158         return rc;
0159 
0160     reg = cpu_to_be32(~0);
0161     rc = fsi_device_write(dev, FSI_MSENP0, &reg, sizeof(reg));
0162     if (rc)
0163         return rc;
0164 
0165     /* Leave enabled long enough for master logic to set up */
0166     mdelay(FSI_LINK_ENABLE_SETUP_TIME);
0167 
0168     rc = fsi_device_write(dev, FSI_MCENP0, &reg, sizeof(reg));
0169     if (rc)
0170         return rc;
0171 
0172     rc = fsi_device_read(dev, FSI_MAEB, &reg, sizeof(reg));
0173     if (rc)
0174         return rc;
0175 
0176     reg = cpu_to_be32(FSI_MRESP_RST_ALL_MASTER | FSI_MRESP_RST_ALL_LINK);
0177     rc = fsi_device_write(dev, FSI_MRESP0, &reg, sizeof(reg));
0178     if (rc)
0179         return rc;
0180 
0181     rc = fsi_device_read(dev, FSI_MLEVP0, &reg, sizeof(reg));
0182     if (rc)
0183         return rc;
0184 
0185     /* Reset the master bridge */
0186     reg = cpu_to_be32(FSI_MRESB_RST_GEN);
0187     rc = fsi_device_write(dev, FSI_MRESB0, &reg, sizeof(reg));
0188     if (rc)
0189         return rc;
0190 
0191     reg = cpu_to_be32(FSI_MRESB_RST_ERR);
0192     return fsi_device_write(dev, FSI_MRESB0, &reg, sizeof(reg));
0193 }
0194 
0195 static int hub_master_probe(struct device *dev)
0196 {
0197     struct fsi_device *fsi_dev = to_fsi_dev(dev);
0198     struct fsi_master_hub *hub;
0199     uint32_t reg, links;
0200     __be32 __reg;
0201     int rc;
0202 
0203     rc = fsi_device_read(fsi_dev, FSI_MVER, &__reg, sizeof(__reg));
0204     if (rc)
0205         return rc;
0206 
0207     reg = be32_to_cpu(__reg);
0208     links = (reg >> 8) & 0xff;
0209     dev_dbg(dev, "hub version %08x (%d links)\n", reg, links);
0210 
0211     rc = fsi_slave_claim_range(fsi_dev->slave, FSI_HUB_LINK_OFFSET,
0212             FSI_HUB_LINK_SIZE * links);
0213     if (rc) {
0214         dev_err(dev, "can't claim slave address range for links");
0215         return rc;
0216     }
0217 
0218     hub = kzalloc(sizeof(*hub), GFP_KERNEL);
0219     if (!hub) {
0220         rc = -ENOMEM;
0221         goto err_release;
0222     }
0223 
0224     hub->addr = FSI_HUB_LINK_OFFSET;
0225     hub->size = FSI_HUB_LINK_SIZE * links;
0226     hub->upstream = fsi_dev;
0227 
0228     hub->master.dev.parent = dev;
0229     hub->master.dev.release = hub_master_release;
0230     hub->master.dev.of_node = of_node_get(dev_of_node(dev));
0231 
0232     hub->master.n_links = links;
0233     hub->master.read = hub_master_read;
0234     hub->master.write = hub_master_write;
0235     hub->master.send_break = hub_master_break;
0236     hub->master.link_enable = hub_master_link_enable;
0237 
0238     dev_set_drvdata(dev, hub);
0239 
0240     hub_master_init(hub);
0241 
0242     rc = fsi_master_register(&hub->master);
0243     if (rc)
0244         goto err_release;
0245 
0246     /* At this point, fsi_master_register performs the device_initialize(),
0247      * and holds the sole reference on master.dev. This means the device
0248      * will be freed (via ->release) during any subsequent call to
0249      * fsi_master_unregister.  We add our own reference to it here, so we
0250      * can perform cleanup (in _remove()) without it being freed before
0251      * we're ready.
0252      */
0253     get_device(&hub->master.dev);
0254     return 0;
0255 
0256 err_release:
0257     fsi_slave_release_range(fsi_dev->slave, FSI_HUB_LINK_OFFSET,
0258             FSI_HUB_LINK_SIZE * links);
0259     return rc;
0260 }
0261 
0262 static int hub_master_remove(struct device *dev)
0263 {
0264     struct fsi_master_hub *hub = dev_get_drvdata(dev);
0265 
0266     fsi_master_unregister(&hub->master);
0267     fsi_slave_release_range(hub->upstream->slave, hub->addr, hub->size);
0268     of_node_put(hub->master.dev.of_node);
0269 
0270     /*
0271      * master.dev will likely be ->release()ed after this, which free()s
0272      * the hub
0273      */
0274     put_device(&hub->master.dev);
0275 
0276     return 0;
0277 }
0278 
0279 static const struct fsi_device_id hub_master_ids[] = {
0280     {
0281         .engine_type = FSI_ENGID_HUB_MASTER,
0282         .version = FSI_VERSION_ANY,
0283     },
0284     { 0 }
0285 };
0286 
0287 static struct fsi_driver hub_master_driver = {
0288     .id_table = hub_master_ids,
0289     .drv = {
0290         .name = "fsi-master-hub",
0291         .bus = &fsi_bus_type,
0292         .probe = hub_master_probe,
0293         .remove = hub_master_remove,
0294     }
0295 };
0296 
0297 module_fsi_driver(hub_master_driver);
0298 MODULE_LICENSE("GPL");