po_hi_driver_rasta_common.c 11.4 KB
Newer Older
1 2 3 4 5
/*
 * This is a part of PolyORB-HI-C distribution, a minimal
 * middleware written for generated code from AADL models.
 * You should use it with the Ocarina toolsuite.
 *
yoogx's avatar
yoogx committed
6
 * For more informations, please visit http://taste.tuxfamily.org/wiki
jhugues's avatar
jhugues committed
7
 *
yoogx's avatar
yoogx committed
8
 * Copyright (C) 2010-2018 ESA & ISAE.
9 10
 */

11 12 13
#include <deployment.h>
/* Generated code header */

14
#if ((defined __PO_HI_NEED_DRIVER_SPACEWIRE_RASTA) || \
yoogx's avatar
yoogx committed
15 16
     (defined __PO_HI_NEED_DRIVER_SERIAL_RASTA) || \
     (defined __PO_HI_NEED_DRIVER_ETH_LEON ))
yoogx's avatar
yoogx committed
17 18 19

#include <stdlib.h>

yoogx's avatar
yoogx committed
20
#include <rtems.h>
21 22 23
#include <rtems/bspIo.h>
#include <ambapp.h>

yoogx's avatar
yoogx committed
24 25
#ifdef GRLEON2
#include <grspw_rasta.h>
26 27 28
#include <pci.h>
#include <rasta.h>
#include <apbuart_rasta.h>
yoogx's avatar
yoogx committed
29 30
#endif

31 32
/* Rasta includes from GAISLER drivers */

33
#if defined GRLEON3 && defined RTEMS412
yoogx's avatar
yoogx committed
34 35 36 37
#include <amba.h>
#include <ambapp.h>
#include <drvmgr/drvmgr.h>
#include <drvmgr/ambapp_bus_grlib.h>
38 39 40 41 42 43 44 45 46 47 48 49

// Things are always moving around in RTEMS - adapt.
// The latest RTEMS (2019/07) has restructured Leon/AMBA
// headers under grlib. Detect this by a combination of checks,
// that depends on the fact that our custom cross build in TASTE
// enabled Ada (which Gaisler's RCC doesn't).
#if ((__RTEMS_ADA__ != 0) && (((__RTEMS_MAJOR__ << 8) | (__RTEMS_MINOR__ << 0)) >= 0x0500))
   #include <grlib/ambapp_bus.h>
#else
   #include <drvmgr/ambapp_bus.h>
#endif

yoogx's avatar
yoogx committed
50 51 52 53
/* GRSPW0 resources  */
struct drvmgr_key grlib_grspw0_res[] =

{
54 55 56
        {"txDesc", DRVMGR_KT_INT, {(unsigned int)16}},
        {"rxDesc", DRVMGR_KT_INT, {(unsigned int)32}},
        DRVMGR_KEY_EMPTY
yoogx's avatar
yoogx committed
57 58 59 60 61
};
/* GRSPW1 and GRSPW2 resources */
struct drvmgr_key grlib_grspw1_res[] =

{
62 63 64
        {"txDesc", DRVMGR_KT_INT, {(unsigned int)16}},
        {"rxDesc", DRVMGR_KT_INT, {(unsigned int)32}},
        DRVMGR_KEY_EMPTY
yoogx's avatar
yoogx committed
65 66 67 68 69
};
/* GRSPW1 and GRSPW2 resources */
struct drvmgr_key grlib_grspw2_res[] =

{
70 71 72
        {"txDesc", DRVMGR_KT_INT, {(unsigned int)16}},
        {"rxDesc", DRVMGR_KT_INT, {(unsigned int)32}},
        DRVMGR_KEY_EMPTY
yoogx's avatar
yoogx committed
73 74 75 76
};
struct drvmgr_key grlib_greth0_res[] =

{
77 78 79
        {"txDescs", DRVMGR_KT_INT, {(unsigned int)16}},
        {"rxDescs", DRVMGR_KT_INT, {(unsigned int)32}},
        DRVMGR_KEY_EMPTY
yoogx's avatar
yoogx committed
80 81 82 83
};
struct drvmgr_key grlib_grcan0_res[] =

{
84 85 86
        {"txDescs", DRVMGR_KT_INT, {(unsigned int)16}},
        {"rxDescs", DRVMGR_KT_INT, {(unsigned int)32}},
        DRVMGR_KEY_EMPTY
yoogx's avatar
yoogx committed
87 88 89 90 91
};
/* GRLIB Plug & Play bus driver resources */
struct drvmgr_drv_res grlib_drv_resources[] =

{
92 93 94 95 96 97
        {DRIVER_AMBAPP_GAISLER_GRSPW2_ID, 0, &grlib_grspw0_res[0]},
        {DRIVER_AMBAPP_GAISLER_GRSPW2_ID, 1, &grlib_grspw1_res[0]},
        {DRIVER_AMBAPP_GAISLER_GRSPW2_ID, 2, &grlib_grspw2_res[0]},
        {DRIVER_AMBAPP_GAISLER_GRCAN_ID, 0, &grlib_grcan0_res[0]},
        {DRIVER_AMBAPP_GAISLER_GRETH_ID, 0, &grlib_greth0_res[0]},
        DRVMGR_RES_EMPTY
yoogx's avatar
yoogx committed
98 99
};

100 101 102 103 104 105
/* The following is defined in amba.c, as of RCC 1.3rc4 */
//struct grlib_config grlib_bus_config =
//{
//        &ambapp_plb,		/* AMBAPP bus setup */
//        &grlib_drv_resources,	/* Driver configuration */
//};
yoogx's avatar
yoogx committed
106 107
#endif

108 109 110
#include <po_hi_debug.h>
/* PolyORB-HI-C includes */

111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
#define DBG printk

/* If RASTA_SRAM is defined SRAM will be used, else SDRAM */
/*#define RASTA_SRAM 1*/

#define RASTA_IRQ  5

/* Offset from 0x80000000 (dual bus version) */
#define AHB1_IOAREA_BASE_ADDR 0x80100000
#define APB2_OFFSET    0x200000
#define IRQ_OFFSET     0x200500
#define GRHCAN_OFFSET  0x201000
#define BRM_OFFSET     0x100000
#define SPW_OFFSET     0xa00
#define UART_OFFSET    0x200200
#define GPIO0_OFF      0x200600
#define GPIO1_OFF      0x200700

129 130 131
#ifndef GAISLER_GPIO
#define GAISLER_GPIO         0x01a
#endif
132

133 134
int __po_hi_c_driver_rasta_common_is_init = 0;

yoogx's avatar
yoogx committed
135
#if defined RTEMS48 || defined RTEMS410
136
int apbuart_rasta_register(amba_confarea_type *bus);
137 138 139 140 141 142 143 144
static amba_confarea_type abus;
extern LEON3_IrqCtrl_Regs_Map *irq;
extern LEON_Register_Map      *regs;

amba_confarea_type* __po_hi_driver_rasta_common_get_bus ()
{
   return &abus;
}
145 146 147 148 149 150 151 152 153 154 155
#elif RTEMS411
extern int apbuart_rasta_register(struct ambapp_bus *bus);
static struct ambapp_bus  abus;
/* Why do we have LEON3 specifics here ???
extern LEON3_IrqCtrl_Regs_Map *irq;
extern LEON_Register_Map      *regs;
*/
struct ambapp_bus * __po_hi_driver_rasta_common_get_bus ()
{
   return &abus;
}
yoogx's avatar
yoogx committed
156 157
#elif RTEMS412
 ;
158 159 160
#else
#error "o<"
#endif
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175

void *uart0_int_arg, *uart1_int_arg;
void *spw0_int_arg, *spw1_int_arg, *spw2_int_arg;
void *grcan_int_arg;
void *brm_int_arg;

extern void (*uart0_int_handler)(int irq, void *arg);
extern void (*uart1_int_handler)(int irq, void *arg);
extern void (*spw0_int_handler)(int irq, void *arg);
extern void (*spw1_int_handler)(int irq, void *arg);
extern void (*spw2_int_handler)(int irq, void *arg);
extern void (*grcan_int_handler)(int irq, void *arg);
extern void (*brm_int_handler)(int irq, void *arg);


176

177
unsigned int __po_hi_driver_rasta_bar0, __po_hi_driver_rasta_bar1;
178

179
/*
180 181 182 183 184 185 186 187

void __po_hi_rasta_interrrupt_register(void *handler, int irqno, void *arg)
{
  DBG("RASTA: Registering irq %d\n",irqno);
  if ( irqno == UART0_IRQNO ){
    DBG("RASTA: Registering uart0 handler: 0x%x, arg: 0x%x\n",handler,arg);
    uart0_int_handler = handler;
    uart0_int_arg = arg;
188

189 190 191
    irq->iclear = UART0_IRQ;
    irq->mask[0] |= UART0_IRQ;
  }
192

193 194 195 196
  if ( irqno == UART1_IRQNO ){
    DBG("RASTA: Registering uart1 handler: 0x%x, arg: 0x%x\n",handler,arg);
    uart1_int_handler = handler;
    uart1_int_arg = arg;
197

198 199 200
    irq->iclear = UART1_IRQ;
    irq->mask[0] |= UART1_IRQ;
  }
201

202 203 204 205
  if ( irqno == SPW0_IRQNO ){
    DBG("RASTA: Registering spw0 handler: 0x%x, arg: 0x%x\n",handler,arg);
    spw0_int_handler = handler;
    spw0_int_arg = arg;
206

207 208 209 210 211 212 213 214
    irq->iclear = SPW0_IRQ;
    irq->mask[0] |= SPW0_IRQ;
  }

  if ( irqno == SPW1_IRQNO ){
    DBG("RASTA: Registering spw1 handler: 0x%x, arg: 0x%x\n",handler,arg);
    spw1_int_handler = handler;
    spw1_int_arg = arg;
215

216 217 218
    irq->iclear = SPW1_IRQ;
    irq->mask[0] |= SPW1_IRQ;
  }
219

220 221 222 223
  if ( irqno == SPW2_IRQNO ){
    DBG("RASTA: Registering spw2 handler: 0x%x, arg: 0x%x\n",handler,arg);
    spw2_int_handler = handler;
    spw2_int_arg = arg;
224

225 226 227
    irq->iclear = SPW2_IRQ;
    irq->mask[0] |= SPW2_IRQ;
  }
228

229 230 231 232
  if ( irqno == GRCAN_IRQNO ){
    DBG("RASTA: Registering GRCAN handler: 0x%x, arg: 0x%x\n",handler,arg);
    grcan_int_handler = handler;
    grcan_int_arg = arg;
233

234 235 236
    irq->iclear = GRCAN_IRQ;
    irq->mask[0] |= GRCAN_IRQ;
  }
237

238 239 240 241
  if ( irqno == BRM_IRQNO ){
    DBG("RASTA: Registering BRM handler: 0x%x, arg: 0x%x\n",handler,arg);
    brm_int_handler = handler;
    brm_int_arg = arg;
242

243 244
    irq->iclear = BRM_IRQ;
    irq->mask[0] |= BRM_IRQ;
245
  }
246 247 248
}


249
static rtems_isr __po_hi_rasta_interrupt_handler (rtems_vector_number v)
250
{
251
    unsigned int status;
252

253
    status = irq->ipend;
254

255 256 257
    if ( (status & GRCAN_IRQ) && grcan_int_handler ) {
      grcan_int_handler(GRCAN_IRQNO,grcan_int_arg);
    }
258

259 260 261 262
    if (status & SPW_IRQ) {
      if ( (status & SPW0_IRQ) && spw0_int_handler ){
        spw0_int_handler(SPW0_IRQNO,spw0_int_arg);
      }
263

264 265 266
      if ( (status & SPW1_IRQ) && spw1_int_handler ){
        spw1_int_handler(SPW1_IRQNO,spw1_int_arg);
      }
267

268 269 270 271
      if ( (status & SPW2_IRQ) && spw2_int_handler ){
        spw2_int_handler(SPW2_IRQNO,spw2_int_arg);
      }
    }
272
    if ((status & BRM_IRQ) && brm_int_handler ){
273 274 275 276 277 278 279 280
        brm_int_handler(BRM_IRQNO,brm_int_arg);
    }
    if ( (status & UART0_IRQ) && uart0_int_handler ) {
      uart0_int_handler(UART0_IRQNO,uart0_int_arg);
    }
    if ( (status & UART1_IRQ) && uart1_int_handler) {
      uart1_int_handler(UART1_IRQNO,uart1_int_arg);
    }
281

282 283
    DBG("RASTA-IRQ: 0x%x\n",status);
    irq->iclear = status;
284

285
}
286 287


288
int __po_hi_rasta_register(void)
289
{
290 291
       unsigned int bar0, bar1, data;

292 293 294
    unsigned int *page0 = NULL;
    unsigned int *apb_base = NULL;
    int found=0;
295

296 297

    DBG("Searching for RASTA board ...");
298

299 300 301
    if (BSP_pciFindDevice(0x1AC8, 0x0010, 0, &bus, &dev, &fun) == 0) {
      found = 1;
    }
302

303 304 305
    if ( (!found) && (BSP_pciFindDevice(0x16E3, 0x0210, 0, &bus, &dev, &fun) == 0) ) {
      found = 1;
    }
306

307 308
    if ( !found )
      return -1;
309

310 311
    DBG(" found it (dev/fun: %d/%d).\n", dev, fun);

312 313
    pci_read_config_dword(bus, dev, fun, 0x10, &bar0);
    pci_read_config_dword(bus, dev, fun, 0x14, &bar1);
314

315
    page0 = (unsigned int *)(bar0 + 0x400000);
316
    *page0 = 0x80000000;
317

318
    apb_base = (unsigned int *)(bar0+APB2_OFFSET);
319 320 321 322 323

#ifdef RASTA_SRAM
    apb_base[0] = 0x000002ff;
    apb_base[1] = 0x00001260;
    apb_base[2] = 0x000e8000;
324
#else
325 326 327 328
    apb_base[0] = 0x000002ff;
    apb_base[1] = 0x82206000;
    apb_base[2] = 0x000e8000;
#endif
329
    irq = (LEON3_IrqCtrl_Regs_Map *) (bar0+IRQ_OFFSET);
330 331 332 333 334
    irq->iclear = 0xffff;
    irq->ilevel = 0;
    irq->mask[0] = 0xffff & ~(UART0_IRQ|UART1_IRQ|SPW0_IRQ|SPW1_IRQ|SPW2_IRQ|GRCAN_IRQ|BRM_IRQ);

    regs->PIO_Direction &= ~(1<<7);
335
    regs->PIO_Interrupt |= (0x87<<8);
336

337 338
    apb_base[0x100] |= 0x40000000;
    apb_base[0x104] =  0x40000000;
339

340

341 342
    pci_read_config_dword(bus, dev, fun, 0x4, &data);
    pci_write_config_dword(bus, dev, fun, 0x4, data|0x40);
343

344 345 346 347 348
    pci_master_enable(bus, dev, fun);

    set_vector(__po_hi_rasta_interrupt_handler, RASTA_IRQ+0x10, 1);

    amba_maps[0].size = 0x10000000;
349
    amba_maps[0].cpu_adr = bar0;
350 351 352
    amba_maps[0].remote_amba_adr = 0x80000000;

    amba_maps[1].size = 0x10000000;
353
    amba_maps[1].cpu_adr = bar1;
354 355 356 357 358
    amba_maps[1].remote_amba_adr = 0x40000000;

    amba_maps[2].size=0;
    amba_maps[2].cpu_adr = 0;
    amba_maps[2].remote_amba_adr = 0;
359

360
    memset(&abus,0,sizeof(abus));
361

362
    amba_scan(&abus,bar0+(AHB1_IOAREA_BASE_ADDR&~0xf0000000),&amba_maps[0]);
363

364
    printk("Registering RASTA BRM driver\n\r");
365

366 367 368 369 370
    b1553brm_rasta_int_reg=__po_hi_rasta_interrrupt_register;
          if ( b1553brm_rasta_register(&abus,2,0,3,bar1,0x40000000) ){
      printk("Failed to register BRM RASTA driver\n");
      return -1;
    }
371

372 373 374 375
    grspw_rasta_int_reg=__po_hi_rasta_interrrupt_register;
    if ( grspw_rasta_register(&abus,bar1) ){
      printk("Failed to register RASTA GRSPW driver\n\r");
      return -1;
376
    }
377

378 379 380 381 382
    apbuart_rasta_int_reg=__po_hi_rasta_interrrupt_register;
    if ( apbuart_rasta_register(&abus) ){
      printk("Failed to register RASTA APBUART driver\n\r");
      return -1;
    }
383

384 385
    if ( rasta_get_gpio(&abus,0,&gpio0,NULL) ){
      printk("Failed to get address for RASTA GPIO0\n\r");
386 387
      return -1;
    }
388

389 390 391 392
    if ( rasta_get_gpio(&abus,1,&gpio1,NULL) ){
      printk("Failed to get address for RASTA GPIO1\n\r");
      return -1;
    }
393

394 395
    return 0;
}
396 397
*/

398

399 400 401 402
void __po_hi_c_driver_rasta_common_init ()
{
   if (__po_hi_c_driver_rasta_common_is_init == 1)
   {
403
      __PO_HI_DEBUG_DEBUG ("[RASTA COMMON] RASTA already initialized, pass init\n");
404 405 406
      return;
   }

yoogx's avatar
yoogx committed
407 408
#if defined GRLEON3 && defined RTEMS412
   /* Register GRLIB root bus */
409
   //   ambapp_grlib_root_register(&grlib_bus_config);
yoogx's avatar
yoogx committed
410

411
   /* Initializing Driver Manager if not already performed by BSP */
yoogx's avatar
yoogx committed
412 413 414 415 416 417 418 419 420 421 422 423
   __PO_HI_DEBUG_DEBUG("Initializing Driver manager...\n");
   if ( drvmgr_init() ) {
        __PO_HI_DEBUG_DEBUG ("Driver manager Failed to initialize\n");
        exit(-1);
        }
#if __PO_HI_DEBUG_LEVEL >= __PO_HI_DEBUG_LEVEL_DEBUG
   /* Print Driver manager drivers and their assigned devices */
   __PO_HI_DEBUG_DEBUG("[drvmgr] ");
   drvmgr_summary();
#endif

#elif GRLEON2
424

425
   __PO_HI_DEBUG_DEBUG ("[RASTA COMMON] Init\n");
426
   init_pci();
427
   __PO_HI_DEBUG_DEBUG ("[RASTA COMMON] Initializing RASTA ...");
428
  /*
429
  if  (__po_hi_rasta_register() ){
430
    __PO_HI_DEBUG_DEBUG (" ERROR !\n");
431 432
    return;
  }
433
  */
434

435
  if  (rasta_register() ){
436
    __PO_HI_DEBUG_DEBUG(" ERROR !\n");
437 438
    return;
  }
439
#endif
440
    __PO_HI_DEBUG_DEBUG (" OK !\n");
441 442 443 444 445

   __po_hi_c_driver_rasta_common_is_init = 1;
}

#endif