[3bb4122] | 1 | /* AHB Status register driver |
---|
| 2 | * |
---|
[c609ccea] | 3 | * COPYRIGHT (c) 2009 - 2017. |
---|
[3bb4122] | 4 | * Cobham Gaisler AB. |
---|
| 5 | * |
---|
| 6 | * The license and distribution terms for this file may be |
---|
| 7 | * found in the file LICENSE in this distribution or at |
---|
[4a7d1026] | 8 | * http://www.rtems.org/license/LICENSE. |
---|
[3bb4122] | 9 | */ |
---|
| 10 | |
---|
[c05d7a9d] | 11 | #include <inttypes.h> |
---|
[b2ed712] | 12 | #include <string.h> |
---|
[c609ccea] | 13 | #include <rtems.h> |
---|
| 14 | #include <rtems/bspIo.h> |
---|
[3bb4122] | 15 | #include <drvmgr/drvmgr.h> |
---|
| 16 | #include <drvmgr/ambapp_bus.h> |
---|
| 17 | |
---|
[5823bae8] | 18 | #include <bsp/ahbstat.h> |
---|
[c609ccea] | 19 | |
---|
[a7267241] | 20 | #include <grlib_impl.h> |
---|
[c609ccea] | 21 | |
---|
| 22 | #define REG_WRITE(addr, val) (*(volatile uint32_t *)(addr) = (uint32_t)(val)) |
---|
| 23 | #define REG_READ(addr) (*(volatile uint32_t *)(addr)) |
---|
[3bb4122] | 24 | |
---|
| 25 | void ahbstat_isr(void *arg); |
---|
| 26 | |
---|
| 27 | /* AHB fail interrupt callback to user. This function is declared weak so that |
---|
| 28 | * the user can define a function pointer variable containing the address |
---|
| 29 | * responsible for handling errors |
---|
| 30 | * |
---|
| 31 | * minor Index of AHBSTAT hardware |
---|
| 32 | * regs Register address of AHBSTAT |
---|
| 33 | * status AHBSTAT status register at IRQ |
---|
| 34 | * failing_address AHBSTAT Failing address register at IRQ |
---|
| 35 | * |
---|
| 36 | * * User return |
---|
| 37 | * 0: print error onto terminal with printk and reenable AHBSTAT |
---|
| 38 | * 1: just re-enable AHBSTAT |
---|
| 39 | * 2: just print error |
---|
| 40 | * 3: do nothing, let user do custom handling |
---|
| 41 | */ |
---|
| 42 | int (*ahbstat_error)( |
---|
| 43 | int minor, |
---|
| 44 | struct ahbstat_regs *regs, |
---|
| 45 | uint32_t status, |
---|
| 46 | uint32_t failing_address |
---|
| 47 | ) __attribute__((weak)) = NULL; |
---|
| 48 | |
---|
| 49 | #define AHBSTAT_STS_CE_BIT 9 |
---|
| 50 | #define AHBSTAT_STS_NE_BIT 8 |
---|
| 51 | #define AHBSTAT_STS_HW_BIT 7 |
---|
| 52 | #define AHBSTAT_STS_HM_BIT 3 |
---|
| 53 | #define AHBSTAT_STS_HS_BIT 0 |
---|
| 54 | |
---|
| 55 | #define AHBSTAT_STS_CE (1 << AHBSTAT_STS_CE_BIT) |
---|
| 56 | #define AHBSTAT_STS_NE (1 << AHBSTAT_STS_NE_BIT) |
---|
| 57 | #define AHBSTAT_STS_HW (1 << AHBSTAT_STS_HW_BIT) |
---|
| 58 | #define AHBSTAT_STS_HM (0xf << AHBSTAT_STS_HM_BIT) |
---|
| 59 | #define AHBSTAT_STS_HS (0x7 << AHBSTAT_STS_HS_BIT) |
---|
| 60 | |
---|
[c609ccea] | 61 | enum { DEVNAME_LEN = 9 }; |
---|
[3bb4122] | 62 | struct ahbstat_priv { |
---|
| 63 | struct drvmgr_dev *dev; |
---|
| 64 | struct ahbstat_regs *regs; |
---|
[c609ccea] | 65 | char devname[DEVNAME_LEN]; |
---|
[3bb4122] | 66 | int minor; |
---|
[c609ccea] | 67 | /* Cached error */ |
---|
[3bb4122] | 68 | uint32_t last_status; |
---|
| 69 | uint32_t last_address; |
---|
[c609ccea] | 70 | /* Spin-lock ISR protection */ |
---|
[a7267241] | 71 | SPIN_DECLARE(devlock); |
---|
[3bb4122] | 72 | }; |
---|
| 73 | |
---|
[0fcd4b33] | 74 | static int ahbstat_init2(struct drvmgr_dev *dev); |
---|
[3bb4122] | 75 | |
---|
| 76 | struct drvmgr_drv_ops ahbstat_ops = |
---|
| 77 | { |
---|
| 78 | .init = {NULL, ahbstat_init2, NULL, NULL}, |
---|
| 79 | .remove = NULL, |
---|
| 80 | .info = NULL |
---|
| 81 | }; |
---|
| 82 | |
---|
| 83 | struct amba_dev_id ahbstat_ids[] = |
---|
| 84 | { |
---|
| 85 | {VENDOR_GAISLER, GAISLER_AHBSTAT}, |
---|
| 86 | {0, 0} /* Mark end of table */ |
---|
| 87 | }; |
---|
| 88 | |
---|
| 89 | struct amba_drv_info ahbstat_drv_info = |
---|
| 90 | { |
---|
| 91 | { |
---|
| 92 | DRVMGR_OBJ_DRV, /* Driver */ |
---|
| 93 | NULL, /* Next driver */ |
---|
| 94 | NULL, /* Device list */ |
---|
| 95 | DRIVER_AMBAPP_GAISLER_AHBSTAT_ID,/* Driver ID */ |
---|
| 96 | "AHBSTAT_DRV", /* Driver Name */ |
---|
| 97 | DRVMGR_BUS_TYPE_AMBAPP, /* Bus Type */ |
---|
| 98 | &ahbstat_ops, |
---|
| 99 | NULL, /* Funcs */ |
---|
| 100 | 0, /* No devices yet */ |
---|
| 101 | sizeof(struct ahbstat_priv), |
---|
| 102 | }, |
---|
| 103 | &ahbstat_ids[0] |
---|
| 104 | }; |
---|
| 105 | |
---|
| 106 | void ahbstat_register_drv (void) |
---|
| 107 | { |
---|
| 108 | drvmgr_drv_register(&ahbstat_drv_info.general); |
---|
| 109 | } |
---|
| 110 | |
---|
[0fcd4b33] | 111 | static int ahbstat_init2(struct drvmgr_dev *dev) |
---|
[3bb4122] | 112 | { |
---|
| 113 | struct ahbstat_priv *priv; |
---|
| 114 | struct amba_dev_info *ambadev; |
---|
| 115 | |
---|
| 116 | priv = dev->priv; |
---|
| 117 | if (!priv) |
---|
| 118 | return DRVMGR_NOMEM; |
---|
| 119 | priv->dev = dev; |
---|
| 120 | |
---|
| 121 | /* Get device information from AMBA PnP information */ |
---|
| 122 | ambadev = (struct amba_dev_info *)dev->businfo; |
---|
| 123 | if (ambadev == NULL) |
---|
| 124 | return DRVMGR_FAIL; |
---|
| 125 | priv->regs = (struct ahbstat_regs *)ambadev->info.apb_slv->start; |
---|
| 126 | priv->minor = dev->minor_drv; |
---|
| 127 | |
---|
[c609ccea] | 128 | strncpy(&priv->devname[0], "ahbstat0", DEVNAME_LEN); |
---|
| 129 | priv->devname[7] += priv->minor; |
---|
| 130 | /* |
---|
| 131 | * Initialize spinlock for AHBSTAT Device. It is used to protect user |
---|
| 132 | * API calls involivng priv structure from updates in ISR. |
---|
| 133 | */ |
---|
[a7267241] | 134 | SPIN_INIT(&priv->devlock, priv->devname); |
---|
[c609ccea] | 135 | |
---|
[3bb4122] | 136 | /* Initialize hardware */ |
---|
[c609ccea] | 137 | REG_WRITE(&priv->regs->status, 0); |
---|
[3bb4122] | 138 | |
---|
| 139 | /* Install IRQ handler */ |
---|
[c609ccea] | 140 | drvmgr_interrupt_register(dev, 0, priv->devname, ahbstat_isr, priv); |
---|
[3bb4122] | 141 | |
---|
| 142 | return DRVMGR_OK; |
---|
| 143 | } |
---|
| 144 | |
---|
| 145 | void ahbstat_isr(void *arg) |
---|
| 146 | { |
---|
| 147 | struct ahbstat_priv *priv = arg; |
---|
| 148 | uint32_t fadr, status; |
---|
| 149 | int rc; |
---|
[a7267241] | 150 | SPIN_ISR_IRQFLAGS(lock_context); |
---|
[3bb4122] | 151 | |
---|
| 152 | /* Get hardware status */ |
---|
[c609ccea] | 153 | status = REG_READ(&priv->regs->status); |
---|
[3bb4122] | 154 | if ((status & AHBSTAT_STS_NE) == 0) |
---|
| 155 | return; |
---|
| 156 | |
---|
| 157 | /* IRQ generated by AHBSTAT core... handle it here */ |
---|
| 158 | |
---|
| 159 | /* Get Failing address */ |
---|
[c609ccea] | 160 | fadr = REG_READ(&priv->regs->failing); |
---|
[3bb4122] | 161 | |
---|
[a7267241] | 162 | SPIN_LOCK(&priv->devlock, lock_context); |
---|
[3bb4122] | 163 | priv->last_status = status; |
---|
| 164 | priv->last_address = fadr; |
---|
[a7267241] | 165 | SPIN_UNLOCK(&priv->devlock, lock_context); |
---|
[3bb4122] | 166 | |
---|
| 167 | /* Let user handle error, default to print the error and reenable HW |
---|
| 168 | * |
---|
| 169 | * User return |
---|
| 170 | * 0: print error and reenable AHBSTAT |
---|
| 171 | * 1: just reenable AHBSTAT |
---|
[c609ccea] | 172 | * 2: just print error |
---|
[3bb4122] | 173 | * 3: do nothing |
---|
| 174 | */ |
---|
| 175 | rc = 0; |
---|
| 176 | if (ahbstat_error != NULL) |
---|
| 177 | rc = ahbstat_error(priv->minor, priv->regs, status, fadr); |
---|
| 178 | |
---|
| 179 | if ((rc & 0x1) == 0) { |
---|
[c05d7a9d] | 180 | printk("\n### AHBSTAT: %s %s error of size %" PRId32 |
---|
| 181 | " by master %" PRId32 " at 0x%08" PRIx32 "\n", |
---|
[3bb4122] | 182 | status & AHBSTAT_STS_CE ? "single" : "non-correctable", |
---|
| 183 | status & AHBSTAT_STS_HW ? "write" : "read", |
---|
[f767fe74] | 184 | (status & AHBSTAT_STS_HS) >> AHBSTAT_STS_HS_BIT, |
---|
| 185 | (status & AHBSTAT_STS_HM) >> AHBSTAT_STS_HM_BIT, |
---|
[3bb4122] | 186 | fadr); |
---|
| 187 | } |
---|
| 188 | |
---|
| 189 | if ((rc & 0x2) == 0) { |
---|
| 190 | /* Trigger new interrupts */ |
---|
[c609ccea] | 191 | REG_WRITE(&priv->regs->status, 0); |
---|
[3bb4122] | 192 | } |
---|
| 193 | } |
---|
| 194 | |
---|
| 195 | /* Get Last received AHB Error |
---|
| 196 | * |
---|
| 197 | * Return |
---|
| 198 | * 0: No error received |
---|
| 199 | * 1: Error Received, last status and address stored into argument pointers |
---|
| 200 | * -1: No such AHBSTAT device |
---|
| 201 | */ |
---|
| 202 | int ahbstat_last_error(int minor, uint32_t *status, uint32_t *address) |
---|
| 203 | { |
---|
| 204 | struct drvmgr_dev *dev; |
---|
| 205 | struct ahbstat_priv *priv; |
---|
[c609ccea] | 206 | uint32_t last_status; |
---|
| 207 | uint32_t last_address; |
---|
[a7267241] | 208 | SPIN_IRQFLAGS(lock_context); |
---|
[3bb4122] | 209 | |
---|
| 210 | if (drvmgr_get_dev(&ahbstat_drv_info.general, minor, &dev)) { |
---|
| 211 | return -1; |
---|
| 212 | } |
---|
| 213 | priv = (struct ahbstat_priv *)dev->priv; |
---|
| 214 | |
---|
[c609ccea] | 215 | /* Read information cached by ISR */ |
---|
[a7267241] | 216 | SPIN_LOCK_IRQ(&priv->devlock, lock_context); |
---|
[c609ccea] | 217 | last_status = REG_READ(&priv->last_status); |
---|
| 218 | last_address = REG_READ(&priv->last_address); |
---|
[a7267241] | 219 | SPIN_UNLOCK_IRQ(&priv->devlock, lock_context); |
---|
[c609ccea] | 220 | |
---|
| 221 | *status = last_status; |
---|
| 222 | *address = last_address; |
---|
[3bb4122] | 223 | |
---|
[c609ccea] | 224 | return (last_status & AHBSTAT_STS_NE) >> AHBSTAT_STS_NE_BIT; |
---|
[3bb4122] | 225 | } |
---|
| 226 | |
---|
| 227 | /* Get AHBSTAT registers address from minor. NULL returned if no such device */ |
---|
| 228 | struct ahbstat_regs *ahbstat_get_regs(int minor) |
---|
| 229 | { |
---|
| 230 | struct drvmgr_dev *dev; |
---|
| 231 | struct ahbstat_priv *priv; |
---|
| 232 | |
---|
| 233 | if (drvmgr_get_dev(&ahbstat_drv_info.general, minor, &dev)) { |
---|
| 234 | return NULL; |
---|
| 235 | } |
---|
| 236 | priv = (struct ahbstat_priv *)dev->priv; |
---|
| 237 | |
---|
| 238 | return priv->regs; |
---|
| 239 | } |
---|