summaryrefslogtreecommitdiff
path: root/FreeRTOS/Demo/RISC-V_RV64_PolarFire_SoftConsole/polarfire_hal/platform/drivers/mss/mss_gpio/mss_gpio.c
blob: 1c01e51c5bb420705fcc005999bb879758a82ec1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
/*******************************************************************************
 * Copyright 2019-2020 Microchip FPGA Embedded Systems Solutions.
 *
 * SPDX-License-Identifier: MIT
 * 
 * PolarFire SoC microprocessor subsystem GPIO bare metal driver implementation.
 *
 * This driver is based on SmartFusion2 MSS GPIO driver v2.1.102
 *
 */

#include <drivers/mss/mss_gpio/mss_gpio.h>
#include "mpfs_hal/mss_hal.h"

#ifdef __cplusplus
extern "C" {
#endif 

/*-------------------------------------------------------------------------*//**
 * Defines.
 */
#define GPIO_INT_ENABLE_MASK                ((uint32_t)0x00000008)
#define OUTPUT_BUFFER_ENABLE_MASK           ((uint32_t)0x00000004)

/*These constants define the number of GPIO bits available on each GPIO
 * hardware block*/
#define NB_OF_GPIO_GPIO0                    ((uint32_t)14)
#define NB_OF_GPIO_GPIO1                    ((uint32_t)24)
#define NB_OF_GPIO_GPIO2                    ((uint32_t)32)

/*This constant indicates the total number of GPIO interrupt inputs at the PLIC
 * (includes the direct and non-direct GPIO interrupts)*/
#define NB_OF_GPIO_INTR                     ((uint32_t)41)

/*-------------------------------------------------------------------------*//**
 * Lookup table of GPIO interrupt number indexed on GPIO ID.
 * The GPIO interrupts are multiplexed. Total GPIO interrupts are 41.
 * 41 = (14 from GPIO0 + 24 from GPIO1 + 3 non direct interrupts)
 * GPIO2 interrupts are not available by default. Setting the corresponding bit
 * in GPIO_INTERRUPT_FAB_CR(31:0) will enable GPIO2(31:0) corresponding
 * interrupt on PLIC.
 *
 * PLIC       GPIO_INTERRUPT_FAB_CR
                0               1
    0       GPIO0 bit 0     GPIO2 bit 0
    1       GPIO0 bit 1     GPIO2 bit 1
    .
    .
    12      GPIO0 bit 12    GPIO2 bit 12
    13      GPIO0 bit 13    GPIO2 bit 13
    14      GPIO1 bit 0     GPIO2 bit 14
    15      GPIO1 bit 1     GPIO2 bit 15
    .
    .
    .
    30      GPIO1 bit 16    GPIO2 bit 30
    31      GPIO1 bit 17    GPIO2 bit 31
    32          GPIO1 bit 18
    33          GPIO1 bit 19
    34          GPIO1 bit 20
    35          GPIO1 bit 21
    36          GPIO1 bit 22
    37          GPIO1 bit 23
    38  Or of all GPIO0 interrupts who do not have a direct connection enabled
    39  Or of all GPIO1 interrupts who do not have a direct connection enabled
    40  Or of all GPIO2 interrupts who do not have a direct connection enabled
 *
 */
static const PLIC_IRQn_Type g_gpio_irqn_lut[NB_OF_GPIO_INTR] =
{
    GPIO0_BIT0_or_GPIO2_BIT0_PLIC_0,
    GPIO0_BIT1_or_GPIO2_BIT1_PLIC_1,
    GPIO0_BIT2_or_GPIO2_BIT2_PLIC_2,
    GPIO0_BIT3_or_GPIO2_BIT3_PLIC_3,
    GPIO0_BIT4_or_GPIO2_BIT4_PLIC_4,
    GPIO0_BIT5_or_GPIO2_BIT5_PLIC_5,
    GPIO0_BIT6_or_GPIO2_BIT6_PLIC_6,
    GPIO0_BIT7_or_GPIO2_BIT7_PLIC_7,
    GPIO0_BIT8_or_GPIO2_BIT8_PLIC_8,
    GPIO0_BIT9_or_GPIO2_BIT9_PLIC_9,
    GPIO0_BIT10_or_GPIO2_BIT10_PLIC_10,
    GPIO0_BIT11_or_GPIO2_BIT11_PLIC_11,
    GPIO0_BIT12_or_GPIO2_BIT12_PLIC_12,
    GPIO0_BIT13_or_GPIO2_BIT13_PLIC_13,

    GPIO1_BIT0_or_GPIO2_BIT14_PLIC_14,
    GPIO1_BIT1_or_GPIO2_BIT15_PLIC_15,
    GPIO1_BIT2_or_GPIO2_BIT16_PLIC_16,
    GPIO1_BIT3_or_GPIO2_BIT17_PLIC_17,
    GPIO1_BIT4_or_GPIO2_BIT18_PLIC_18,
    GPIO1_BIT5_or_GPIO2_BIT19_PLIC_19,
    GPIO1_BIT6_or_GPIO2_BIT20_PLIC_20,
    GPIO1_BIT7_or_GPIO2_BIT21_PLIC_21,
    GPIO1_BIT8_or_GPIO2_BIT22_PLIC_22,
    GPIO1_BIT9_or_GPIO2_BIT23_PLIC_23,
    GPIO1_BIT10_or_GPIO2_BIT24_PLIC_24,
    GPIO1_BIT11_or_GPIO2_BIT25_PLIC_25,
    GPIO1_BIT12_or_GPIO2_BIT26_PLIC_26,
    GPIO1_BIT13_or_GPIO2_BIT27_PLIC_27,
    GPIO1_BIT14_or_GPIO2_BIT28_PLIC_28,
    GPIO1_BIT15_or_GPIO2_BIT29_PLIC_29,
    GPIO1_BIT16_or_GPIO2_BIT30_PLIC_30,
    GPIO1_BIT17_or_GPIO2_BIT31_PLIC_31,

    GPIO1_BIT18_PLIC_32,
    GPIO1_BIT19_PLIC_33,
    GPIO1_BIT20_PLIC_34,
    GPIO1_BIT21_PLIC_35,
    GPIO1_BIT22_PLIC_36,
    GPIO1_BIT23_PLIC_37,

    GPIO0_NON_DIRECT_PLIC,
    GPIO1_NON_DIRECT_PLIC,
    GPIO2_NON_DIRECT_PLIC
};

/*-------------------------------------------------------------------------*//**
 * Local functions
 */
static uint8_t gpio_number_validate(GPIO_TypeDef const * gpio, mss_gpio_id_t gpio_idx);

/*-------------------------------------------------------------------------*//**
 * MSS_GPIO_init
 * See "mss_gpio.h" for details of how to use this function.
 */
void
MSS_GPIO_init
(
    GPIO_TypeDef * gpio
)
{
    /* clear all pending interrupts*/
    gpio->GPIO_IRQ = 0xFFFFFFFFU;
}

/*-------------------------------------------------------------------------*//**
 * MSS_GPIO_config
 * See "mss_gpio.h" for details of how to use this function.
 */
void MSS_GPIO_config
(
    GPIO_TypeDef * gpio,
    mss_gpio_id_t port_id,
    uint32_t config
)
{
    if (0U == gpio_number_validate(gpio, port_id))
    {
        gpio->GPIO_CFG[port_id] = config;
    }
    else
    {
        ASSERT(0); /*LDRA warning*/
    }
}

/*-------------------------------------------------------------------------*//**
 * MSS_GPIO_config_byte
 * See "mss_gpio.h" for details of how to use this function.
 */
void MSS_GPIO_config_byte
(
    GPIO_TypeDef * gpio,
    mss_gpio_byte_num_t byte_num,
    uint32_t config
)
{
    if (((GPIO0_LO == gpio) || (GPIO0_HI == gpio)) &&
                                                 (byte_num >= MSS_GPIO_BYTE_1))
    {
        ASSERT(0);
    }
    else if (((GPIO1_LO == gpio) || (GPIO1_HI == gpio)) &&
                                                  (byte_num > MSS_GPIO_BYTE_2))
    {
        ASSERT(0);
    }
    else if (((GPIO2_LO == gpio) || (GPIO2_HI == gpio)) &&
                                                  (byte_num > MSS_GPIO_BYTE_3))
    {
        ASSERT(0);
    }
    else
    {
        gpio->GPIO_CFG_BYTE[byte_num] = config;
    }
}

/*-------------------------------------------------------------------------*//**
 * MSS_GPIO_config_all
 * See "mss_gpio.h" for details of how to use this function.
 */
void MSS_GPIO_config_all
(
    GPIO_TypeDef * gpio,
    uint32_t config
)
{
    gpio->GPIO_CFG_ALL = config;
}

/*-------------------------------------------------------------------------*//**
 * MSS_GPIO_set_output
 * See "mss_gpio.h" for details of how to use this function.
 */
void MSS_GPIO_set_output
(
    GPIO_TypeDef * gpio,
    mss_gpio_id_t port_id,
    uint8_t value
)
{
    uint32_t gpio_setting;
    
    if (0U == gpio_number_validate(gpio, port_id))
    {
        /* Setting the bit in GPIO_SET_BITS (offset 0xA4) sets the corresponding
         * output port.
         * Setting the bit in GPIO_CLR_BITS (offset 0xA0) clears the
         * corresponding output port.*/

        if (value > 0u)
        {
            gpio->GPIO_SET_BITS = ((uint32_t)0x01 << port_id);
        }
        else
        {
            gpio->GPIO_CLR_BITS = ((uint32_t)0x01 << port_id);
        }
    }
    else
    {
        ASSERT(0); /*LDRA warning*/
    }
}

/*-------------------------------------------------------------------------*//**
 * MSS_GPIO_drive_inout
 * See "mss_gpio.h" for details of how to use this function.
 */
void MSS_GPIO_drive_inout
(
    GPIO_TypeDef * gpio,
    mss_gpio_id_t port_id,
    mss_gpio_inout_state_t inout_state
)
{
    uint32_t outputs_state;
    uint32_t config;
    
    if (0U == gpio_number_validate(gpio, port_id))
    {
        switch (inout_state)
        {
            case MSS_GPIO_DRIVE_HIGH:
                /* Set output high */
                gpio->GPIO_SET_BITS = ((uint32_t)1 << port_id);

                /* Enable output buffer */
                config = gpio->GPIO_CFG[port_id];
                config |= OUTPUT_BUFFER_ENABLE_MASK;
                gpio->GPIO_CFG[port_id] = config;
            break;

            case MSS_GPIO_DRIVE_LOW:
                /* Set output low */
                gpio->GPIO_CLR_BITS = (uint32_t)1 << port_id;
                /* Enable output buffer */
                config = gpio->GPIO_CFG[port_id];
                config |= OUTPUT_BUFFER_ENABLE_MASK;
                gpio->GPIO_CFG[port_id] = config;
            break;

            case MSS_GPIO_HIGH_Z:
                /* Disable output buffer */
                config = gpio->GPIO_CFG[port_id];
                config &= ~OUTPUT_BUFFER_ENABLE_MASK;
                gpio->GPIO_CFG[port_id] = config;
            break;

            default:
                ASSERT(0);
            break;
        }
    }
    else
    {
        ASSERT(0); /*LDRA warning*/
    }
}

/*-------------------------------------------------------------------------*//**
 * MSS_GPIO_enable_irq
 * See "mss_gpio.h" for details of how to use this function.
 */
void MSS_GPIO_enable_irq
(
    GPIO_TypeDef * gpio,
    mss_gpio_id_t port_id
)
{
    uint32_t cfg_value;

    if (0U == gpio_number_validate(gpio, port_id))
    {
        cfg_value = gpio->GPIO_CFG[(uint8_t)port_id];
        gpio->GPIO_CFG[(uint8_t)port_id] = (cfg_value | GPIO_INT_ENABLE_MASK);

        if ((GPIO0_LO == gpio) || (GPIO0_HI == gpio))
        {
            PLIC_EnableIRQ(g_gpio_irqn_lut[port_id]);
        }
        else if ((GPIO1_LO == gpio) || (GPIO1_HI == gpio))
        {
            PLIC_EnableIRQ(g_gpio_irqn_lut[port_id +
                                           GPIO1_BIT0_or_GPIO2_BIT14_PLIC_14]);
        }
        else if ((GPIO2_LO == gpio) || (GPIO2_HI == gpio))
        {
            PLIC_EnableIRQ(g_gpio_irqn_lut[port_id]);
        }
        else
        {
            ASSERT(0); /*LDRA warning*/
        }
    }
    else
    {
        ASSERT(0); /*LDRA warning*/
    }
}

/*-------------------------------------------------------------------------*//**
 * MSS_GPIO_disable_irq
 * See "mss_gpio.h" for details of how to use this function.
 */

void MSS_GPIO_disable_irq
(
    GPIO_TypeDef * gpio,
    mss_gpio_id_t port_id
)
{
    uint32_t cfg_value;

    if (0U == gpio_number_validate(gpio, port_id))
    {
        cfg_value = gpio->GPIO_CFG[(uint8_t)port_id];
        gpio->GPIO_CFG[(uint8_t)port_id] = (cfg_value & (~GPIO_INT_ENABLE_MASK));

        if ((GPIO0_LO == gpio) || (GPIO0_HI == gpio))
        {
            PLIC_DisableIRQ(g_gpio_irqn_lut[port_id]);
        }
        else if ((GPIO1_LO == gpio) || (GPIO1_HI == gpio))
        {
            PLIC_DisableIRQ(g_gpio_irqn_lut[port_id +
                                            GPIO1_BIT0_or_GPIO2_BIT14_PLIC_14]);
        }
        else if ((GPIO2_LO == gpio) || (GPIO2_HI == gpio))
        {
            PLIC_DisableIRQ(GPIO2_NON_DIRECT_PLIC);
        }
        else
        {
            ASSERT(0); /*LDRA warning*/
        }
    }
    else
    {
        ASSERT(0); /*LDRA warning*/
    }
}

/*-------------------------------------------------------------------------*//**
 * MSS_GPIO_enable_nondirect_irq
 * See "mss_gpio.h" for details of how to use this function.
 */
void
MSS_GPIO_enable_nondirect_irq
(
    GPIO_TypeDef const * gpio
)
{
    if ((GPIO0_LO == gpio) || (GPIO0_HI == gpio))
    {
        PLIC_EnableIRQ(GPIO0_NON_DIRECT_PLIC);
    }
    else if ((GPIO1_LO == gpio) || (GPIO1_HI == gpio))
    {
        PLIC_EnableIRQ(GPIO1_NON_DIRECT_PLIC);
    }
    else if ((GPIO2_LO == gpio) || (GPIO2_HI == gpio))
    {
        PLIC_EnableIRQ(GPIO2_NON_DIRECT_PLIC);
    }
    else
    {
        ASSERT(0); /*LDRA warning*/
    }
}

/*-------------------------------------------------------------------------*//**
 * MSS_GPIO_disable_nondirect_irq
 * See "mss_gpio.h" for details of how to use this function.
 */
void
MSS_GPIO_disable_nondirect_irq
(
    GPIO_TypeDef const * gpio
)
{
    if ((GPIO0_LO == gpio) || (GPIO0_HI == gpio))
    {
        PLIC_DisableIRQ(GPIO0_NON_DIRECT_PLIC);
    }
    else if ((GPIO1_LO == gpio) || (GPIO1_HI == gpio))
    {
        PLIC_DisableIRQ(GPIO1_NON_DIRECT_PLIC);
    }
    else if ((GPIO2_LO == gpio) || (GPIO2_HI == gpio))
    {
        PLIC_DisableIRQ(GPIO2_NON_DIRECT_PLIC);
    }
    else
    {
        ASSERT(0); /*LDRA warning*/
    }
}

/*-------------------------------------------------------------------------*//**
 * MSS_GPIO_clear_irq
 * See "mss_gpio.h" for details of how to use this function.
 */
void MSS_GPIO_clear_irq
(
    GPIO_TypeDef * gpio,
    mss_gpio_id_t port_id
)
{
    if (0U == gpio_number_validate(gpio, port_id))
    {
        gpio->GPIO_IRQ = ((uint32_t)1) << port_id;
        __asm("fence");
    }
    else
    {
        ASSERT(0); /*LDRA warning*/
    }
}

static uint8_t gpio_number_validate(GPIO_TypeDef const * gpio, mss_gpio_id_t gpio_idx)
{
    uint8_t ret;

    if (((GPIO0_LO == gpio) || (GPIO0_HI == gpio)) &&
                                                (gpio_idx >= NB_OF_GPIO_GPIO0))
    {
        ret = 1u;
    }
    else if (((GPIO1_LO == gpio) || (GPIO1_HI == gpio)) &&
                                                (gpio_idx >= NB_OF_GPIO_GPIO1))
    {
        ret = 1u;
    }
    else if (((GPIO2_LO == gpio) || (GPIO2_HI == gpio)) &&
                                                (gpio_idx >= NB_OF_GPIO_GPIO2))
    {
        ret = 1u;
    }
    else
    {
        ret = 0u;
    }

    return ret;
}

#ifdef __cplusplus
}
#endif