| 1 | /***********************license start*********************************** |
| 2 | * Copyright (c) 2003-2017 Cavium Inc. ([email protected]). All rights |
| 3 | * reserved. |
| 4 | * |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions are |
| 8 | * met: |
| 9 | * |
| 10 | * * Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * |
| 13 | * * Redistributions in binary form must reproduce the above |
| 14 | * copyright notice, this list of conditions and the following |
| 15 | * disclaimer in the documentation and/or other materials provided |
| 16 | * with the distribution. |
| 17 | * |
| 18 | * * Neither the name of Cavium Inc. nor the names of |
| 19 | * its contributors may be used to endorse or promote products |
| 20 | * derived from this software without specific prior written |
| 21 | * permission. |
| 22 | * |
| 23 | * This Software, including technical data, may be subject to U.S. export |
| 24 | * control laws, including the U.S. Export Administration Act and its |
| 25 | * associated regulations, and may be subject to export or import |
| 26 | * regulations in other countries. |
| 27 | * |
| 28 | * TO THE MAXIMUM EXTENT PERMITTED BY LAW, THE SOFTWARE IS PROVIDED "AS IS" |
| 29 | * AND WITH ALL FAULTS AND CAVIUM INC. MAKES NO PROMISES, REPRESENTATIONS OR |
| 30 | * WARRANTIES, EITHER EXPRESS, IMPLIED, STATUTORY, OR OTHERWISE, WITH RESPECT |
| 31 | * TO THE SOFTWARE, INCLUDING ITS CONDITION, ITS CONFORMITY TO ANY |
| 32 | * REPRESENTATION OR DESCRIPTION, OR THE EXISTENCE OF ANY LATENT OR PATENT |
| 33 | * DEFECTS, AND CAVIUM SPECIFICALLY DISCLAIMS ALL IMPLIED (IF ANY) WARRANTIES |
| 34 | * OF TITLE, MERCHANTABILITY, NONINFRINGEMENT, FITNESS FOR A PARTICULAR |
| 35 | * PURPOSE, LACK OF VIRUSES, ACCURACY OR COMPLETENESS, QUIET ENJOYMENT, |
| 36 | * QUIET POSSESSION OR CORRESPONDENCE TO DESCRIPTION. THE ENTIRE RISK |
| 37 | * ARISING OUT OF USE OR PERFORMANCE OF THE SOFTWARE LIES WITH YOU. |
| 38 | ***********************license end**************************************/ |
| 39 | #include <bdk.h> |
| 40 | #include "libbdk-arch/bdk-csrs-gpio.h" |
| 41 | #include "libbdk-hal/bdk-gpio.h" |
| 42 | |
| 43 | /* This code is an optional part of the BDK. It is only linked in |
| 44 | if BDK_REQUIRE() needs it */ |
| 45 | BDK_REQUIRE_DEFINE(GPIO); |
| 46 | |
| 47 | /** |
| 48 | * Initialize a single GPIO as either an input or output. If it is |
| 49 | * an output, also set its output value. |
| 50 | * |
| 51 | * @param gpio GPIO to initialize |
| 52 | * @param is_output Non zero if this GPIO should be an output |
| 53 | * @param output_value |
| 54 | * Value of the GPIO if it should be an output. Not used if the |
| 55 | * GPIO isn't an output. |
| 56 | * |
| 57 | * @return Zero on success, negative ob failure |
| 58 | */ |
| 59 | int bdk_gpio_initialize(bdk_node_t node, int gpio, int is_output, int output_value) |
| 60 | { |
| 61 | if ((gpio >= 0) && (gpio < bdk_gpio_get_num())) |
| 62 | { |
| 63 | int gpio_group = gpio >> 6; |
| 64 | int gpio_index = gpio & 63; |
| 65 | if (output_value) |
| 66 | bdk_gpio_set(node, gpio_group, 1ull << gpio_index); |
| 67 | else |
| 68 | bdk_gpio_clear(node, gpio_group, 1ull << gpio_index); |
| 69 | |
| 70 | BDK_CSR_DEFINE(cfg, BDK_GPIO_BIT_CFGX(gpio)); |
| 71 | cfg.u = 0; |
| 72 | cfg.s.tx_oe = !!is_output; |
| 73 | BDK_CSR_WRITE(node, BDK_GPIO_BIT_CFGX(gpio), cfg.u); |
| 74 | } |
| 75 | else |
| 76 | { |
| 77 | bdk_error("bdk_gpio_initialize: Illegal GPIO\n"); |
| 78 | return -1; |
| 79 | } |
| 80 | return 0; |
| 81 | } |
| 82 | |
| 83 | |
| 84 | /** |
| 85 | * GPIO Read Data |
| 86 | * |
| 87 | * @param node Node GPIO block is on |
| 88 | * @param gpio_block GPIO block to access. Each block contains up to 64 GPIOs |
| 89 | * |
| 90 | * @return Status of the GPIO pins for the given block |
| 91 | */ |
| 92 | uint64_t bdk_gpio_read(bdk_node_t node, int gpio_block) |
| 93 | { |
| 94 | bdk_gpio_rx_dat_t gpio_rx_dat; |
| 95 | switch (gpio_block) |
| 96 | { |
| 97 | case 0: |
| 98 | gpio_rx_dat.u = BDK_CSR_READ(node, BDK_GPIO_RX_DAT); |
| 99 | break; |
| 100 | case 1: |
| 101 | gpio_rx_dat.u = BDK_CSR_READ(node, BDK_GPIO_RX1_DAT); |
| 102 | break; |
| 103 | default: |
| 104 | bdk_error("GPIO block %d not supported\n", gpio_block); |
| 105 | gpio_rx_dat.u = 0; |
| 106 | break; |
| 107 | } |
| 108 | return gpio_rx_dat.s.dat; |
| 109 | } |
| 110 | |
| 111 | |
| 112 | /** |
| 113 | * GPIO Clear pin |
| 114 | * |
| 115 | * @param node Node GPIO block is on |
| 116 | * @param gpio_block GPIO block to access. Each block contains up to 64 GPIOs |
| 117 | * @param clear_mask Bit mask to indicate which bits to drive to '0'. |
| 118 | */ |
| 119 | void bdk_gpio_clear(bdk_node_t node, int gpio_block, uint64_t clear_mask) |
| 120 | { |
| 121 | switch (gpio_block) |
| 122 | { |
| 123 | case 0: |
| 124 | BDK_CSR_WRITE(node, BDK_GPIO_TX_CLR, clear_mask); |
| 125 | break; |
| 126 | case 1: |
| 127 | BDK_CSR_WRITE(node, BDK_GPIO_TX1_CLR, clear_mask); |
| 128 | break; |
| 129 | default: |
| 130 | bdk_error("GPIO block %d not supported\n", gpio_block); |
| 131 | break; |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | |
| 136 | /** |
| 137 | * GPIO Set pin |
| 138 | * |
| 139 | * @param node Node GPIO block is on |
| 140 | * @param gpio_block GPIO block to access. Each block contains up to 64 GPIOs |
| 141 | * @param set_mask Bit mask to indicate which bits to drive to '1'. |
| 142 | */ |
| 143 | void bdk_gpio_set(bdk_node_t node, int gpio_block, uint64_t set_mask) |
| 144 | { |
| 145 | switch (gpio_block) |
| 146 | { |
| 147 | case 0: |
| 148 | BDK_CSR_WRITE(node, BDK_GPIO_TX_SET, set_mask); |
| 149 | break; |
| 150 | case 1: |
| 151 | BDK_CSR_WRITE(node, BDK_GPIO_TX1_SET, set_mask); |
| 152 | break; |
| 153 | default: |
| 154 | bdk_error("GPIO block %d not supported\n", gpio_block); |
| 155 | break; |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | |
| 160 | /** GPIO Select pin |
| 161 | * |
| 162 | * @param node CPU node |
| 163 | * @param gpio GPIO number |
| 164 | * @param pin Pin number |
| 165 | */ |
| 166 | void bdk_gpio_select_pin(bdk_node_t node, int gpio, int pin) |
| 167 | { |
| 168 | if ((gpio < 0) || (gpio >= bdk_gpio_get_num())) |
| 169 | { |
| 170 | bdk_warn("bdk_gpio_select_pin: Illegal GPIO %d\n", gpio); |
| 171 | return; |
| 172 | } |
| 173 | |
| 174 | BDK_CSR_MODIFY(c, node, BDK_GPIO_BIT_CFGX(gpio), c.s.pin_sel = pin); |
| 175 | } |
| 176 | |
| 177 | |
| 178 | /** |
| 179 | * Return the number of GPIO pins on this chip |
| 180 | * |
| 181 | * @return Number of GPIO pins |
| 182 | */ |
| 183 | int bdk_gpio_get_num(void) |
| 184 | { |
| 185 | if (CAVIUM_IS_MODEL(CAVIUM_CN88XX)) |
| 186 | return 51; |
| 187 | else if (CAVIUM_IS_MODEL(CAVIUM_CN81XX)) |
| 188 | return 48; |
| 189 | else if (CAVIUM_IS_MODEL(CAVIUM_CN83XX)) |
| 190 | return 80; |
| 191 | else if (CAVIUM_IS_MODEL(CAVIUM_CN93XX)) |
| 192 | return 96; |
| 193 | else |
| 194 | { |
| 195 | bdk_error("bdk_gpio_get_num(): Unsupported chip"); |
| 196 | return 0; |
| 197 | } |
| 198 | } |