Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-or-later */
0002 /* Copyright (C) 2019 IBM Corp.  */
0003 
0004 #ifndef ASPEED_PINMUX_H
0005 #define ASPEED_PINMUX_H
0006 
0007 #include <linux/regmap.h>
0008 
0009 /*
0010  * The ASPEED SoCs provide typically more than 200 pins for GPIO and other
0011  * functions. The SoC function enabled on a pin is determined on a priority
0012  * basis where a given pin can provide a number of different signal types.
0013  *
0014  * The signal active on a pin is described by both a priority level and
0015  * compound logical expressions involving multiple operators, registers and
0016  * bits. Some difficulty arises as the pin's function bit masks for each
0017  * priority level are frequently not the same (i.e. cannot just flip a bit to
0018  * change from a high to low priority signal), or even in the same register.
0019  * Further, not all signals can be unmuxed, as some expressions depend on
0020  * values in the hardware strapping register (which may be treated as
0021  * read-only).
0022  *
0023  * SoC Multi-function Pin Expression Examples
0024  * ------------------------------------------
0025  *
0026  * Here are some sample mux configurations from the AST2400 and AST2500
0027  * datasheets to illustrate the corner cases, roughly in order of least to most
0028  * corner. The signal priorities are in decending order from P0 (highest).
0029  *
0030  * D6 is a pin with a single function (beside GPIO); a high priority signal
0031  * that participates in one function:
0032  *
0033  * Ball | Default | P0 Signal | P0 Expression               | P1 Signal | P1 Expression | Other
0034  * -----+---------+-----------+-----------------------------+-----------+---------------+----------
0035  *  D6    GPIOA0    MAC1LINK    SCU80[0]=1                                                GPIOA0
0036  * -----+---------+-----------+-----------------------------+-----------+---------------+----------
0037  *
0038  * C5 is a multi-signal pin (high and low priority signals). Here we touch
0039  * different registers for the different functions that enable each signal:
0040  *
0041  * -----+---------+-----------+-----------------------------+-----------+---------------+----------
0042  *  C5    GPIOA4    SCL9        SCU90[22]=1                   TIMER5      SCU80[4]=1      GPIOA4
0043  * -----+---------+-----------+-----------------------------+-----------+---------------+----------
0044  *
0045  * E19 is a single-signal pin with two functions that influence the active
0046  * signal. In this case both bits have the same meaning - enable a dedicated
0047  * LPC reset pin. However it's not always the case that the bits in the
0048  * OR-relationship have the same meaning.
0049  *
0050  * -----+---------+-----------+-----------------------------+-----------+---------------+----------
0051  *  E19   GPIOB4    LPCRST#     SCU80[12]=1 | Strap[14]=1                                 GPIOB4
0052  * -----+---------+-----------+-----------------------------+-----------+---------------+----------
0053  *
0054  * For example, pin B19 has a low-priority signal that's enabled by two
0055  * distinct SoC functions: A specific SIOPBI bit in register SCUA4, and an ACPI
0056  * bit in the STRAP register. The ACPI bit configures signals on pins in
0057  * addition to B19. Both of the low priority functions as well as the high
0058  * priority function must be disabled for GPIOF1 to be used.
0059  *
0060  * Ball | Default | P0 Signal | P0 Expression                           | P1 Signal | P1 Expression                          | Other
0061  * -----+---------+-----------+-----------------------------------------+-----------+----------------------------------------+----------
0062  *  B19   GPIOF1    NDCD4       SCU80[25]=1                               SIOPBI#     SCUA4[12]=1 | Strap[19]=0                GPIOF1
0063  * -----+---------+-----------+-----------------------------------------+-----------+----------------------------------------+----------
0064  *
0065  * For pin E18, the SoC ANDs the expected state of three bits to determine the
0066  * pin's active signal:
0067  *
0068  * * SCU3C[3]: Enable external SOC reset function
0069  * * SCU80[15]: Enable SPICS1# or EXTRST# function pin
0070  * * SCU90[31]: Select SPI interface CS# output
0071  *
0072  * -----+---------+-----------+-----------------------------------------+-----------+----------------------------------------+----------
0073  *  E18   GPIOB7    EXTRST#     SCU3C[3]=1 & SCU80[15]=1 & SCU90[31]=0    SPICS1#     SCU3C[3]=1 & SCU80[15]=1 & SCU90[31]=1   GPIOB7
0074  * -----+---------+-----------+-----------------------------------------+-----------+----------------------------------------+----------
0075  *
0076  * (Bits SCU3C[3] and SCU80[15] appear to only be used in the expressions for
0077  * selecting the signals on pin E18)
0078  *
0079  * Pin T5 is a multi-signal pin with a more complex configuration:
0080  *
0081  * Ball | Default | P0 Signal | P0 Expression                | P1 Signal | P1 Expression | Other
0082  * -----+---------+-----------+------------------------------+-----------+---------------+----------
0083  *  T5    GPIOL1    VPIDE       SCU90[5:4]!=0 & SCU84[17]=1    NDCD1       SCU84[17]=1     GPIOL1
0084  * -----+---------+-----------+------------------------------+-----------+---------------+----------
0085  *
0086  * The high priority signal configuration is best thought of in terms of its
0087  * exploded form, with reference to the SCU90[5:4] bits:
0088  *
0089  * * SCU90[5:4]=00: disable
0090  * * SCU90[5:4]=01: 18 bits (R6/G6/B6) video mode.
0091  * * SCU90[5:4]=10: 24 bits (R8/G8/B8) video mode.
0092  * * SCU90[5:4]=11: 30 bits (R10/G10/B10) video mode.
0093  *
0094  * Re-writing:
0095  *
0096  * -----+---------+-----------+------------------------------+-----------+---------------+----------
0097  *  T5    GPIOL1    VPIDE      (SCU90[5:4]=1 & SCU84[17]=1)    NDCD1       SCU84[17]=1     GPIOL1
0098  *                             | (SCU90[5:4]=2 & SCU84[17]=1)
0099  *                             | (SCU90[5:4]=3 & SCU84[17]=1)
0100  * -----+---------+-----------+------------------------------+-----------+---------------+----------
0101  *
0102  * For reference the SCU84[17] bit configure the "UART1 NDCD1 or Video VPIDE
0103  * function pin", where the signal itself is determined by whether SCU94[5:4]
0104  * is disabled or in one of the 18, 24 or 30bit video modes.
0105  *
0106  * Other video-input-related pins require an explicit state in SCU90[5:4], e.g.
0107  * W1 and U5:
0108  *
0109  * -----+---------+-----------+------------------------------+-----------+---------------+----------
0110  *  W1    GPIOL6    VPIB0       SCU90[5:4]=3 & SCU84[22]=1     TXD1        SCU84[22]=1     GPIOL6
0111  *  U5    GPIOL7    VPIB1       SCU90[5:4]=3 & SCU84[23]=1     RXD1        SCU84[23]=1     GPIOL7
0112  * -----+---------+-----------+------------------------------+-----------+---------------+----------
0113  *
0114  * The examples of T5 and W1 are particularly fertile, as they also demonstrate
0115  * that despite operating as part of the video input bus each signal needs to
0116  * be enabled individually via it's own SCU84 (in the cases of T5 and W1)
0117  * register bit. This is a little crazy if the bus doesn't have optional
0118  * signals, but is used to decent effect with some of the UARTs where not all
0119  * signals are required. However, this isn't done consistently - UART1 is
0120  * enabled on a per-pin basis, and by contrast, all signals for UART6 are
0121  * enabled by a single bit.
0122  *
0123  * Further, the high and low priority signals listed in the table above share
0124  * a configuration bit. The VPI signals should operate in concert in a single
0125  * function, but the UART signals should retain the ability to be configured
0126  * independently. This pushes the implementation down the path of tagging a
0127  * signal's expressions with the function they participate in, rather than
0128  * defining masks affecting multiple signals per function. The latter approach
0129  * fails in this instance where applying the configuration for the UART pin of
0130  * interest will stomp on the state of other UART signals when disabling the
0131  * VPI functions on the current pin.
0132  *
0133  * Ball |  Default   | P0 Signal | P0 Expression             | P1 Signal | P1 Expression | Other
0134  * -----+------------+-----------+---------------------------+-----------+---------------+------------
0135  *  A12   RGMII1TXCK   GPIOT0      SCUA0[0]=1                  RMII1TXEN   Strap[6]=0      RGMII1TXCK
0136  *  B12   RGMII1TXCTL  GPIOT1      SCUA0[1]=1                  –           Strap[6]=0      RGMII1TXCTL
0137  * -----+------------+-----------+---------------------------+-----------+---------------+------------
0138  *
0139  * A12 demonstrates that the "Other" signal isn't always GPIO - in this case
0140  * GPIOT0 is a high-priority signal and RGMII1TXCK is Other. Thus, GPIO
0141  * should be treated like any other signal type with full function expression
0142  * requirements, and not assumed to be the default case. Separately, GPIOT0 and
0143  * GPIOT1's signal descriptor bits are distinct, therefore we must iterate all
0144  * pins in the function's group to disable the higher-priority signals such
0145  * that the signal for the function of interest is correctly enabled.
0146  *
0147  * Finally, three priority levels aren't always enough; the AST2500 brings with
0148  * it 18 pins of five priority levels, however the 18 pins only use three of
0149  * the five priority levels.
0150  *
0151  * Ultimately the requirement to control pins in the examples above drive the
0152  * design:
0153  *
0154  * * Pins provide signals according to functions activated in the mux
0155  *   configuration
0156  *
0157  * * Pins provide up to five signal types in a priority order
0158  *
0159  * * For priorities levels defined on a pin, each priority provides one signal
0160  *
0161  * * Enabling lower priority signals requires higher priority signals be
0162  *   disabled
0163  *
0164  * * A function represents a set of signals; functions are distinct if they
0165  *   do not share a subset of signals (and may be distinct if they are a
0166  *   strict subset).
0167  *
0168  * * Signals participate in one or more functions or groups
0169  *
0170  * * A function is described by an expression of one or more signal
0171  *   descriptors, which compare bit values in a register
0172  *
0173  * * A signal expression is the smallest set of signal descriptors whose
0174  *   comparisons must evaluate 'true' for a signal to be enabled on a pin.
0175  *
0176  * * A signal participating in a function is active on a pin if evaluating all
0177  *   signal descriptors in the pin's signal expression for the function yields
0178  *   a 'true' result
0179  *
0180  * * A signal at a given priority on a given pin is active if any of the
0181  *   functions in which the signal participates are active, and no higher
0182  *   priority signal on the pin is active
0183  *
0184  * * GPIO is configured per-pin
0185  *
0186  * And so:
0187  *
0188  * * To disable a signal, any function(s) activating the signal must be
0189  *   disabled
0190  *
0191  * * Each pin must know the signal expressions of functions in which it
0192  *   participates, for the purpose of enabling the Other function. This is done
0193  *   by deactivating all functions that activate higher priority signals on the
0194  *   pin.
0195  *
0196  * As a concrete example:
0197  *
0198  * * T5 provides three signals types: VPIDE, NDCD1 and GPIO
0199  *
0200  * * The VPIDE signal participates in 3 functions: VPI18, VPI24 and VPI30
0201  *
0202  * * The NDCD1 signal participates in just its own NDCD1 function
0203  *
0204  * * VPIDE is high priority, NDCD1 is low priority, and GPIOL1 is the least
0205  *   prioritised
0206  *
0207  * * The prerequisit for activating the NDCD1 signal is that the VPI18, VPI24
0208  *   and VPI30 functions all be disabled
0209  *
0210  * * Similarly, all of VPI18, VPI24, VPI30 and NDCD1 functions must be disabled
0211  *   to provide GPIOL6
0212  *
0213  * Considerations
0214  * --------------
0215  *
0216  * If pinctrl allows us to allocate a pin we can configure a function without
0217  * concern for the function of already allocated pins, if pin groups are
0218  * created with respect to the SoC functions in which they participate. This is
0219  * intuitive, but it did not feel obvious from the bit/pin relationships.
0220  *
0221  * Conversely, failing to allocate all pins in a group indicates some bits (as
0222  * well as pins) required for the group's configuration will already be in use,
0223  * likely in a way that's inconsistent with the requirements of the failed
0224  * group.
0225  *
0226  * Implementation
0227  * --------------
0228  *
0229  * Beyond the documentation below the various structures and helper macros that
0230  * allow the implementation to hang together are defined. The macros are fairly
0231  * dense, so below we walk through some raw examples of the configuration
0232  * tables in an effort to clarify the concepts.
0233  *
0234  * The complexity of configuring the mux combined with the scale of the pins
0235  * and functions was a concern, so the table design along with the macro jungle
0236  * is an attempt to address it. The rough principles of the approach are:
0237  *
0238  * 1. Use a data-driven solution rather than embedding state into code
0239  * 2. Minimise editing to the specifics of the given mux configuration
0240  * 3. Detect as many errors as possible at compile time
0241  *
0242  * Addressing point 3 leads to naming of symbols in terms of the four
0243  * properties associated with a given mux configuration: The pin, the signal,
0244  * the group and the function. In this way copy/paste errors cause duplicate
0245  * symbols to be defined, which prevents successful compilation. Failing to
0246  * properly parent the tables leads to unused symbol warnings, and use of
0247  * designated initialisers and additional warnings ensures that there are
0248  * no override errors in the pin, group and function arrays.
0249  *
0250  * Addressing point 2 drives the development of the macro jungle, as it
0251  * centralises the definition noise at the cost of taking some time to
0252  * understand.
0253  *
0254  * Here's a complete, concrete "pre-processed" example of the table structures
0255  * used to describe the D6 ball from the examples above:
0256  *
0257  * ```
0258  * static const struct aspeed_sig_desc sig_descs_MAC1LINK_MAC1LINK[] = {
0259  *     {
0260  *         .ip = ASPEED_IP_SCU,
0261  *         .reg = 0x80,
0262  *         .mask = BIT(0),
0263  *         .enable = 1,
0264  *         .disable = 0
0265  *     },
0266  * };
0267  *
0268  * static const struct aspeed_sig_expr sig_expr_MAC1LINK_MAC1LINK = {
0269  *     .signal = "MAC1LINK",
0270  *     .function = "MAC1LINK",
0271  *     .ndescs = ARRAY_SIZE(sig_descs_MAC1LINK_MAC1LINK),
0272  *     .descs = &(sig_descs_MAC1LINK_MAC1LINK)[0],
0273  * };
0274  *
0275  * static const struct aspeed_sig_expr *sig_exprs_MAC1LINK_MAC1LINK[] = {
0276  *     &sig_expr_MAC1LINK_MAC1LINK,
0277  *     NULL,
0278  * };
0279  *
0280  * static const struct aspeed_sig_desc sig_descs_GPIOA0_GPIOA0[] = { };
0281  *
0282  * static const struct aspeed_sig_expr sig_expr_GPIOA0_GPIOA0 = {
0283  *     .signal = "GPIOA0",
0284  *     .function = "GPIOA0",
0285  *     .ndescs = ARRAY_SIZE(sig_descs_GPIOA0_GPIOA0),
0286  *     .descs = &(sig_descs_GPIOA0_GPIOA0)[0],
0287  * };
0288  *
0289  * static const struct aspeed_sig_expr *sig_exprs_GPIOA0_GPIOA0[] = {
0290  *     &sig_expr_GPIOA0_GPIOA0,
0291  *     NULL
0292  * };
0293  *
0294  * static const struct aspeed_sig_expr **pin_exprs_0[] = {
0295  *     sig_exprs_MAC1LINK_MAC1LINK,
0296  *     sig_exprs_GPIOA0_GPIOA0,
0297  *     NULL
0298  * };
0299  *
0300  * static const struct aspeed_pin_desc pin_0 = { "0", (&pin_exprs_0[0]) };
0301  * static const int group_pins_MAC1LINK[] = { 0 };
0302  * static const char *func_groups_MAC1LINK[] = { "MAC1LINK" };
0303  *
0304  * static struct pinctrl_pin_desc aspeed_g4_pins[] = {
0305  *     [0] = { .number = 0, .name = "D6", .drv_data = &pin_0 },
0306  * };
0307  *
0308  * static const struct aspeed_pin_group aspeed_g4_groups[] = {
0309  *     {
0310  *         .name = "MAC1LINK",
0311  *         .pins = &(group_pins_MAC1LINK)[0],
0312  *         .npins = ARRAY_SIZE(group_pins_MAC1LINK),
0313  *     },
0314  * };
0315  *
0316  * static const struct aspeed_pin_function aspeed_g4_functions[] = {
0317  *     {
0318  *         .name = "MAC1LINK",
0319  *         .groups = &func_groups_MAC1LINK[0],
0320  *         .ngroups = ARRAY_SIZE(func_groups_MAC1LINK),
0321  *     },
0322  * };
0323  * ```
0324  *
0325  * At the end of the day much of the above code is compressed into the
0326  * following two lines:
0327  *
0328  * ```
0329  * #define D6 0
0330  * SSSF_PIN_DECL(D6, GPIOA0, MAC1LINK, SIG_DESC_SET(SCU80, 0));
0331  * ```
0332  *
0333  * The two examples below show just the differences from the example above.
0334  *
0335  * Ball E18 demonstrates a function, EXTRST, that requires multiple descriptors
0336  * be set for it to be muxed:
0337  *
0338  * ```
0339  * static const struct aspeed_sig_desc sig_descs_EXTRST_EXTRST[] = {
0340  *     {
0341  *         .ip = ASPEED_IP_SCU,
0342  *         .reg = 0x3C,
0343  *         .mask = BIT(3),
0344  *         .enable = 1,
0345  *         .disable = 0
0346  *     },
0347  *     {
0348  *         .ip = ASPEED_IP_SCU,
0349  *         .reg = 0x80,
0350  *         .mask = BIT(15),
0351  *         .enable = 1,
0352  *         .disable = 0
0353  *     },
0354  *     {
0355  *         .ip = ASPEED_IP_SCU,
0356  *         .reg = 0x90,
0357  *         .mask = BIT(31),
0358  *         .enable = 0,
0359  *         .disable = 1
0360  *     },
0361  * };
0362  *
0363  * static const struct aspeed_sig_expr sig_expr_EXTRST_EXTRST = {
0364  *     .signal = "EXTRST",
0365  *     .function = "EXTRST",
0366  *     .ndescs = ARRAY_SIZE(sig_descs_EXTRST_EXTRST),
0367  *     .descs = &(sig_descs_EXTRST_EXTRST)[0],
0368  * };
0369  * ...
0370  * ```
0371  *
0372  * For ball E19, we have multiple functions enabling a single signal, LPCRST#.
0373  * The data structures look like:
0374  *
0375  * static const struct aspeed_sig_desc sig_descs_LPCRST_LPCRST[] = {
0376  *     {
0377  *         .ip = ASPEED_IP_SCU,
0378  *         .reg = 0x80,
0379  *         .mask = BIT(12),
0380  *         .enable = 1,
0381  *         .disable = 0
0382  *     },
0383  * };
0384  *
0385  * static const struct aspeed_sig_expr sig_expr_LPCRST_LPCRST = {
0386  *     .signal = "LPCRST",
0387  *     .function = "LPCRST",
0388  *     .ndescs = ARRAY_SIZE(sig_descs_LPCRST_LPCRST),
0389  *     .descs = &(sig_descs_LPCRST_LPCRST)[0],
0390  * };
0391  *
0392  * static const struct aspeed_sig_desc sig_descs_LPCRST_LPCRSTS[] = {
0393  *     {
0394  *         .ip = ASPEED_IP_SCU,
0395  *         .reg = 0x70,
0396  *         .mask = BIT(14),
0397  *         .enable = 1,
0398  *         .disable = 0
0399  *     },
0400  * };
0401  *
0402  * static const struct aspeed_sig_expr sig_expr_LPCRST_LPCRSTS = {
0403  *     .signal = "LPCRST",
0404  *     .function = "LPCRSTS",
0405  *     .ndescs = ARRAY_SIZE(sig_descs_LPCRST_LPCRSTS),
0406  *     .descs = &(sig_descs_LPCRST_LPCRSTS)[0],
0407  * };
0408  *
0409  * static const struct aspeed_sig_expr *sig_exprs_LPCRST_LPCRST[] = {
0410  *     &sig_expr_LPCRST_LPCRST,
0411  *     &sig_expr_LPCRST_LPCRSTS,
0412  *     NULL,
0413  * };
0414  * ...
0415  * ```
0416  *
0417  * Both expressions listed in the sig_exprs_LPCRST_LPCRST array need to be set
0418  * to disabled for the associated GPIO to be muxed.
0419  *
0420  */
0421 
0422 #define ASPEED_IP_SCU       0
0423 #define ASPEED_IP_GFX       1
0424 #define ASPEED_IP_LPC       2
0425 #define ASPEED_NR_PINMUX_IPS    3
0426 
0427  /**
0428   * A signal descriptor, which describes the register, bits and the
0429   * enable/disable values that should be compared or written.
0430   *
0431   * @ip: The IP block identifier, used as an index into the regmap array in
0432   *      struct aspeed_pinctrl_data
0433   * @reg: The register offset with respect to the base address of the IP block
0434   * @mask: The mask to apply to the register. The lowest set bit of the mask is
0435   *        used to derive the shift value.
0436   * @enable: The value that enables the function. Value should be in the LSBs,
0437   *          not at the position of the mask.
0438   * @disable: The value that disables the function. Value should be in the
0439   *           LSBs, not at the position of the mask.
0440   */
0441 struct aspeed_sig_desc {
0442     unsigned int ip;
0443     unsigned int reg;
0444     u32 mask;
0445     u32 enable;
0446     u32 disable;
0447 };
0448 
0449 /**
0450  * Describes a signal expression. The expression is evaluated by ANDing the
0451  * evaluation of the descriptors.
0452  *
0453  * @signal: The signal name for the priority level on the pin. If the signal
0454  *          type is GPIO, then the signal name must begin with the
0455  *          prefix "GPI", e.g. GPIOA0, GPIT0 etc.
0456  * @function: The name of the function the signal participates in for the
0457  *            associated expression. For pin-specific GPIO, the function
0458  *            name must match the signal name.
0459  * @ndescs: The number of signal descriptors in the expression
0460  * @descs: Pointer to an array of signal descriptors that comprise the
0461  *         function expression
0462  */
0463 struct aspeed_sig_expr {
0464     const char *signal;
0465     const char *function;
0466     int ndescs;
0467     const struct aspeed_sig_desc *descs;
0468 };
0469 
0470 /**
0471  * A struct capturing the list of expressions enabling signals at each priority
0472  * for a given pin. The signal configuration for a priority level is evaluated
0473  * by ORing the evaluation of the signal expressions in the respective
0474  * priority's list.
0475  *
0476  * @name: A name for the pin
0477  * @prios: A pointer to an array of expression list pointers
0478  *
0479  */
0480 struct aspeed_pin_desc {
0481     const char *name;
0482     const struct aspeed_sig_expr ***prios;
0483 };
0484 
0485 /* Macro hell */
0486 
0487 #define SIG_DESC_IP_BIT(ip, reg, idx, val) \
0488     { ip, reg, BIT_MASK(idx), val, (((val) + 1) & 1) }
0489 
0490 /**
0491  * Short-hand macro for describing an SCU descriptor enabled by the state of
0492  * one bit. The disable value is derived.
0493  *
0494  * @reg: The signal's associated register, offset from base
0495  * @idx: The signal's bit index in the register
0496  * @val: The value (0 or 1) that enables the function
0497  */
0498 #define SIG_DESC_BIT(reg, idx, val) \
0499     SIG_DESC_IP_BIT(ASPEED_IP_SCU, reg, idx, val)
0500 
0501 #define SIG_DESC_IP_SET(ip, reg, idx) SIG_DESC_IP_BIT(ip, reg, idx, 1)
0502 
0503 /**
0504  * A further short-hand macro expanding to an SCU descriptor enabled by a set
0505  * bit.
0506  *
0507  * @reg: The register, offset from base
0508  * @idx: The bit index in the register
0509  */
0510 #define SIG_DESC_SET(reg, idx) SIG_DESC_IP_BIT(ASPEED_IP_SCU, reg, idx, 1)
0511 #define SIG_DESC_CLEAR(reg, idx) { ASPEED_IP_SCU, reg, BIT_MASK(idx), 0, 0 }
0512 
0513 #define SIG_DESC_LIST_SYM(sig, group) sig_descs_ ## sig ## _ ## group
0514 #define SIG_DESC_LIST_DECL(sig, group, ...) \
0515     static const struct aspeed_sig_desc SIG_DESC_LIST_SYM(sig, group)[] = \
0516         { __VA_ARGS__ }
0517 
0518 #define SIG_EXPR_SYM(sig, group) sig_expr_ ## sig ## _ ## group
0519 #define SIG_EXPR_DECL_(sig, group, func) \
0520     static const struct aspeed_sig_expr SIG_EXPR_SYM(sig, group) = \
0521     { \
0522         .signal = #sig, \
0523         .function = #func, \
0524         .ndescs = ARRAY_SIZE(SIG_DESC_LIST_SYM(sig, group)), \
0525         .descs = &(SIG_DESC_LIST_SYM(sig, group))[0], \
0526     }
0527 
0528 /**
0529  * Declare a signal expression.
0530  *
0531  * @sig: A macro symbol name for the signal (is subjected to stringification
0532  *        and token pasting)
0533  * @func: The function in which the signal is participating
0534  * @...: Signal descriptors that define the signal expression
0535  *
0536  * For example, the following declares the ROMD8 signal for the ROM16 function:
0537  *
0538  *     SIG_EXPR_DECL(ROMD8, ROM16, ROM16, SIG_DESC_SET(SCU90, 6));
0539  *
0540  * And with multiple signal descriptors:
0541  *
0542  *     SIG_EXPR_DECL(ROMD8, ROM16S, ROM16S, SIG_DESC_SET(HW_STRAP1, 4),
0543  *              { HW_STRAP1, GENMASK(1, 0), 0, 0 });
0544  */
0545 #define SIG_EXPR_DECL(sig, group, func, ...) \
0546     SIG_DESC_LIST_DECL(sig, group, __VA_ARGS__); \
0547     SIG_EXPR_DECL_(sig, group, func)
0548 
0549 /**
0550  * Declare a pointer to a signal expression
0551  *
0552  * @sig: The macro symbol name for the signal (subjected to token pasting)
0553  * @func: The macro symbol name for the function (subjected to token pasting)
0554  */
0555 #define SIG_EXPR_PTR(sig, group) (&SIG_EXPR_SYM(sig, group))
0556 
0557 #define SIG_EXPR_LIST_SYM(sig, group) sig_exprs_ ## sig ## _ ## group
0558 
0559 /**
0560  * Declare a signal expression list for reference in a struct aspeed_pin_prio.
0561  *
0562  * @sig: A macro symbol name for the signal (is subjected to token pasting)
0563  * @...: Signal expression structure pointers (use SIG_EXPR_PTR())
0564  *
0565  * For example, the 16-bit ROM bus can be enabled by one of two possible signal
0566  * expressions:
0567  *
0568  *     SIG_EXPR_DECL(ROMD8, ROM16, ROM16, SIG_DESC_SET(SCU90, 6));
0569  *     SIG_EXPR_DECL(ROMD8, ROM16S, ROM16S, SIG_DESC_SET(HW_STRAP1, 4),
0570  *              { HW_STRAP1, GENMASK(1, 0), 0, 0 });
0571  *     SIG_EXPR_LIST_DECL(ROMD8, SIG_EXPR_PTR(ROMD8, ROM16),
0572  *              SIG_EXPR_PTR(ROMD8, ROM16S));
0573  */
0574 #define SIG_EXPR_LIST_DECL(sig, group, ...) \
0575     static const struct aspeed_sig_expr *SIG_EXPR_LIST_SYM(sig, group)[] =\
0576         { __VA_ARGS__, NULL }
0577 
0578 #define stringify(x) #x
0579 #define istringify(x) stringify(x)
0580 
0581 /**
0582  * Create an expression symbol alias from (signal, group) to (pin, signal).
0583  *
0584  * @pin: The pin number
0585  * @sig: The signal name
0586  * @group: The name of the group of which the pin is a member that is
0587  *         associated with the function's signal
0588  *
0589  * Using an alias in this way enables detection of copy/paste errors (defining
0590  * the signal for a group multiple times) whilst enabling multiple pin groups
0591  * to exist for a signal without intrusive side-effects on defining the list of
0592  * signals available on a pin.
0593  */
0594 #define SIG_EXPR_LIST_ALIAS(pin, sig, group) \
0595     static const struct aspeed_sig_expr *\
0596         SIG_EXPR_LIST_SYM(pin, sig)[ARRAY_SIZE(SIG_EXPR_LIST_SYM(sig, group))] \
0597         __attribute__((alias(istringify(SIG_EXPR_LIST_SYM(sig, group)))))
0598 
0599 /**
0600  * A short-hand macro for declaring a function expression and an expression
0601  * list with a single expression (SE) and a single group (SG) of pins.
0602  *
0603  * @pin: The pin the signal will be routed to
0604  * @sig: The signal that will be routed to the pin for the function
0605  * @func: A macro symbol name for the function
0606  * @...: Function descriptors that define the function expression
0607  *
0608  * For example, signal NCTS6 participates in its own function with one group:
0609  *
0610  *     SIG_EXPR_LIST_DECL_SINGLE(A18, NCTS6, NCTS6, SIG_DESC_SET(SCU90, 7));
0611  */
0612 #define SIG_EXPR_LIST_DECL_SESG(pin, sig, func, ...) \
0613     SIG_DESC_LIST_DECL(sig, func, __VA_ARGS__); \
0614     SIG_EXPR_DECL_(sig, func, func); \
0615     SIG_EXPR_LIST_DECL(sig, func, SIG_EXPR_PTR(sig, func)); \
0616     SIG_EXPR_LIST_ALIAS(pin, sig, func)
0617 
0618 /**
0619  * Similar to the above, but for pins with a single expression (SE) and
0620  * multiple groups (MG) of pins.
0621  *
0622  * @pin: The pin the signal will be routed to
0623  * @sig: The signal that will be routed to the pin for the function
0624  * @group: The name of the function's pin group in which the pin participates
0625  * @func: A macro symbol name for the function
0626  * @...: Function descriptors that define the function expression
0627  */
0628 #define SIG_EXPR_LIST_DECL_SEMG(pin, sig, group, func, ...) \
0629     SIG_DESC_LIST_DECL(sig, group, __VA_ARGS__); \
0630     SIG_EXPR_DECL_(sig, group, func); \
0631     SIG_EXPR_LIST_DECL(sig, group, SIG_EXPR_PTR(sig, group)); \
0632     SIG_EXPR_LIST_ALIAS(pin, sig, group)
0633 
0634 /**
0635  * Similar to the above, but for pins with a dual expressions (DE)
0636  * and a single group (SG) of pins.
0637  *
0638  * @pin: The pin the signal will be routed to
0639  * @sig: The signal that will be routed to the pin for the function
0640  * @group: The name of the function's pin group in which the pin participates
0641  * @func: A macro symbol name for the function
0642  * @...: Function descriptors that define the function expression
0643  */
0644 #define SIG_EXPR_LIST_DECL_DESG(pin, sig, f0, f1) \
0645     SIG_EXPR_LIST_DECL(sig, f0, \
0646                SIG_EXPR_PTR(sig, f0), \
0647                SIG_EXPR_PTR(sig, f1)); \
0648     SIG_EXPR_LIST_ALIAS(pin, sig, f0)
0649 
0650 #define SIG_EXPR_LIST_PTR(sig, group) SIG_EXPR_LIST_SYM(sig, group)
0651 
0652 #define PIN_EXPRS_SYM(pin) pin_exprs_ ## pin
0653 #define PIN_EXPRS_PTR(pin) (&PIN_EXPRS_SYM(pin)[0])
0654 #define PIN_SYM(pin) pin_ ## pin
0655 
0656 #define PIN_DECL_(pin, ...) \
0657     static const struct aspeed_sig_expr **PIN_EXPRS_SYM(pin)[] = \
0658         { __VA_ARGS__, NULL }; \
0659     static const struct aspeed_pin_desc PIN_SYM(pin) = \
0660         { #pin, PIN_EXPRS_PTR(pin) }
0661 
0662 /**
0663  * Declare a single signal pin
0664  *
0665  * @pin: The pin number
0666  * @other: Macro name for "other" functionality (subjected to stringification)
0667  * @sig: Macro name for the signal (subjected to stringification)
0668  *
0669  * For example:
0670  *
0671  *     #define E3 80
0672  *     SIG_EXPR_LIST_DECL_SINGLE(SCL5, I2C5, I2C5_DESC);
0673  *     PIN_DECL_1(E3, GPIOK0, SCL5);
0674  */
0675 #define PIN_DECL_1(pin, other, sig) \
0676     SIG_EXPR_LIST_DECL_SESG(pin, other, other); \
0677     PIN_DECL_(pin, SIG_EXPR_LIST_PTR(pin, sig), \
0678           SIG_EXPR_LIST_PTR(pin, other))
0679 
0680 /**
0681  * Single signal, single function pin declaration
0682  *
0683  * @pin: The pin number
0684  * @other: Macro name for "other" functionality (subjected to stringification)
0685  * @sig: Macro name for the signal (subjected to stringification)
0686  * @...: Signal descriptors that define the function expression
0687  *
0688  * For example:
0689  *
0690  *    SSSF_PIN_DECL(A4, GPIOA2, TIMER3, SIG_DESC_SET(SCU80, 2));
0691  */
0692 #define SSSF_PIN_DECL(pin, other, sig, ...) \
0693     SIG_EXPR_LIST_DECL_SESG(pin, sig, sig, __VA_ARGS__); \
0694     SIG_EXPR_LIST_DECL_SESG(pin, other, other); \
0695     PIN_DECL_(pin, SIG_EXPR_LIST_PTR(pin, sig), \
0696           SIG_EXPR_LIST_PTR(pin, other)); \
0697     FUNC_GROUP_DECL(sig, pin)
0698 /**
0699  * Declare a two-signal pin
0700  *
0701  * @pin: The pin number
0702  * @other: Macro name for "other" functionality (subjected to stringification)
0703  * @high: Macro name for the highest priority signal functions
0704  * @low: Macro name for the low signal functions
0705  *
0706  * For example:
0707  *
0708  *     #define A8 56
0709  *     SIG_EXPR_DECL(ROMD8, ROM16, SIG_DESC_SET(SCU90, 6));
0710  *     SIG_EXPR_DECL(ROMD8, ROM16S, SIG_DESC_SET(HW_STRAP1, 4),
0711  *              { HW_STRAP1, GENMASK(1, 0), 0, 0 });
0712  *     SIG_EXPR_LIST_DECL(ROMD8, SIG_EXPR_PTR(ROMD8, ROM16),
0713  *              SIG_EXPR_PTR(ROMD8, ROM16S));
0714  *     SIG_EXPR_LIST_DECL_SINGLE(NCTS6, NCTS6, SIG_DESC_SET(SCU90, 7));
0715  *     PIN_DECL_2(A8, GPIOH0, ROMD8, NCTS6);
0716  */
0717 #define PIN_DECL_2(pin, other, high, low) \
0718     SIG_EXPR_LIST_DECL_SESG(pin, other, other); \
0719     PIN_DECL_(pin, \
0720             SIG_EXPR_LIST_PTR(pin, high), \
0721             SIG_EXPR_LIST_PTR(pin, low), \
0722             SIG_EXPR_LIST_PTR(pin, other))
0723 
0724 #define PIN_DECL_3(pin, other, high, medium, low) \
0725     SIG_EXPR_LIST_DECL_SESG(pin, other, other); \
0726     PIN_DECL_(pin, \
0727             SIG_EXPR_LIST_PTR(pin, high), \
0728             SIG_EXPR_LIST_PTR(pin, medium), \
0729             SIG_EXPR_LIST_PTR(pin, low), \
0730             SIG_EXPR_LIST_PTR(pin, other))
0731 
0732 #define PIN_DECL_4(pin, other, prio1, prio2, prio3, prio4) \
0733     SIG_EXPR_LIST_DECL_SESG(pin, other, other); \
0734     PIN_DECL_(pin, \
0735             SIG_EXPR_LIST_PTR(pin, prio1), \
0736             SIG_EXPR_LIST_PTR(pin, prio2), \
0737             SIG_EXPR_LIST_PTR(pin, prio3), \
0738             SIG_EXPR_LIST_PTR(pin, prio4), \
0739             SIG_EXPR_LIST_PTR(pin, other))
0740 
0741 #define GROUP_SYM(group) group_pins_ ## group
0742 #define GROUP_DECL(group, ...) \
0743     static const int GROUP_SYM(group)[] = { __VA_ARGS__ }
0744 
0745 #define FUNC_SYM(func) func_groups_ ## func
0746 #define FUNC_DECL_(func, ...) \
0747     static const char *FUNC_SYM(func)[] = { __VA_ARGS__ }
0748 
0749 #define FUNC_DECL_1(func, group) FUNC_DECL_(func, #group)
0750 #define FUNC_DECL_2(func, one, two) FUNC_DECL_(func, #one, #two)
0751 #define FUNC_DECL_3(func, one, two, three) FUNC_DECL_(func, #one, #two, #three)
0752 
0753 #define FUNC_GROUP_DECL(func, ...) \
0754     GROUP_DECL(func, __VA_ARGS__); \
0755     FUNC_DECL_(func, #func)
0756 
0757 
0758 #define GPIO_PIN_DECL(pin, gpio) \
0759     SIG_EXPR_LIST_DECL_SESG(pin, gpio, gpio); \
0760     PIN_DECL_(pin, SIG_EXPR_LIST_PTR(pin, gpio))
0761 
0762 struct aspeed_pin_group {
0763     const char *name;
0764     const unsigned int *pins;
0765     const unsigned int npins;
0766 };
0767 
0768 #define ASPEED_PINCTRL_GROUP(name_) { \
0769     .name = #name_, \
0770     .pins = &(GROUP_SYM(name_))[0], \
0771     .npins = ARRAY_SIZE(GROUP_SYM(name_)), \
0772 }
0773 
0774 struct aspeed_pin_function {
0775     const char *name;
0776     const char *const *groups;
0777     unsigned int ngroups;
0778 };
0779 
0780 #define ASPEED_PINCTRL_FUNC(name_, ...) { \
0781     .name = #name_, \
0782     .groups = &FUNC_SYM(name_)[0], \
0783     .ngroups = ARRAY_SIZE(FUNC_SYM(name_)), \
0784 }
0785 
0786 struct aspeed_pinmux_data;
0787 
0788 struct aspeed_pinmux_ops {
0789     int (*eval)(struct aspeed_pinmux_data *ctx,
0790             const struct aspeed_sig_expr *expr, bool enabled);
0791     int (*set)(struct aspeed_pinmux_data *ctx,
0792            const struct aspeed_sig_expr *expr, bool enabled);
0793 };
0794 
0795 struct aspeed_pinmux_data {
0796     struct device *dev;
0797     struct regmap *maps[ASPEED_NR_PINMUX_IPS];
0798 
0799     const struct aspeed_pinmux_ops *ops;
0800 
0801     const struct aspeed_pin_group *groups;
0802     const unsigned int ngroups;
0803 
0804     const struct aspeed_pin_function *functions;
0805     const unsigned int nfunctions;
0806 };
0807 
0808 int aspeed_sig_desc_eval(const struct aspeed_sig_desc *desc, bool enabled,
0809              struct regmap *map);
0810 
0811 int aspeed_sig_expr_eval(struct aspeed_pinmux_data *ctx,
0812              const struct aspeed_sig_expr *expr, bool enabled);
0813 
0814 static inline int aspeed_sig_expr_set(struct aspeed_pinmux_data *ctx,
0815                       const struct aspeed_sig_expr *expr,
0816                       bool enabled)
0817 {
0818     return ctx->ops->set(ctx, expr, enabled);
0819 }
0820 
0821 #endif /* ASPEED_PINMUX_H */