source: rtems/c/src/lib/libbsp/sparc/shared/amba/ahbstat.c @ b2ed712

5
Last change on this file since b2ed712 was b2ed712, checked in by Sebastian Huber <sebastian.huber@…>, on 08/25/17 at 08:58:58

Include missing <string.h>

Update #2133.

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