pinmux-aspeed.h 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /* Copyright (C) 2019 IBM Corp. */
  3. #ifndef ASPEED_PINMUX_H
  4. #define ASPEED_PINMUX_H
  5. #include <linux/regmap.h>
  6. /*
  7. * The ASPEED SoCs provide typically more than 200 pins for GPIO and other
  8. * functions. The SoC function enabled on a pin is determined on a priority
  9. * basis where a given pin can provide a number of different signal types.
  10. *
  11. * The signal active on a pin is described by both a priority level and
  12. * compound logical expressions involving multiple operators, registers and
  13. * bits. Some difficulty arises as the pin's function bit masks for each
  14. * priority level are frequently not the same (i.e. cannot just flip a bit to
  15. * change from a high to low priority signal), or even in the same register.
  16. * Further, not all signals can be unmuxed, as some expressions depend on
  17. * values in the hardware strapping register (which may be treated as
  18. * read-only).
  19. *
  20. * SoC Multi-function Pin Expression Examples
  21. * ------------------------------------------
  22. *
  23. * Here are some sample mux configurations from the AST2400 and AST2500
  24. * datasheets to illustrate the corner cases, roughly in order of least to most
  25. * corner. The signal priorities are in decending order from P0 (highest).
  26. *
  27. * D6 is a pin with a single function (beside GPIO); a high priority signal
  28. * that participates in one function:
  29. *
  30. * Ball | Default | P0 Signal | P0 Expression | P1 Signal | P1 Expression | Other
  31. * -----+---------+-----------+-----------------------------+-----------+---------------+----------
  32. * D6 GPIOA0 MAC1LINK SCU80[0]=1 GPIOA0
  33. * -----+---------+-----------+-----------------------------+-----------+---------------+----------
  34. *
  35. * C5 is a multi-signal pin (high and low priority signals). Here we touch
  36. * different registers for the different functions that enable each signal:
  37. *
  38. * -----+---------+-----------+-----------------------------+-----------+---------------+----------
  39. * C5 GPIOA4 SCL9 SCU90[22]=1 TIMER5 SCU80[4]=1 GPIOA4
  40. * -----+---------+-----------+-----------------------------+-----------+---------------+----------
  41. *
  42. * E19 is a single-signal pin with two functions that influence the active
  43. * signal. In this case both bits have the same meaning - enable a dedicated
  44. * LPC reset pin. However it's not always the case that the bits in the
  45. * OR-relationship have the same meaning.
  46. *
  47. * -----+---------+-----------+-----------------------------+-----------+---------------+----------
  48. * E19 GPIOB4 LPCRST# SCU80[12]=1 | Strap[14]=1 GPIOB4
  49. * -----+---------+-----------+-----------------------------+-----------+---------------+----------
  50. *
  51. * For example, pin B19 has a low-priority signal that's enabled by two
  52. * distinct SoC functions: A specific SIOPBI bit in register SCUA4, and an ACPI
  53. * bit in the STRAP register. The ACPI bit configures signals on pins in
  54. * addition to B19. Both of the low priority functions as well as the high
  55. * priority function must be disabled for GPIOF1 to be used.
  56. *
  57. * Ball | Default | P0 Signal | P0 Expression | P1 Signal | P1 Expression | Other
  58. * -----+---------+-----------+-----------------------------------------+-----------+----------------------------------------+----------
  59. * B19 GPIOF1 NDCD4 SCU80[25]=1 SIOPBI# SCUA4[12]=1 | Strap[19]=0 GPIOF1
  60. * -----+---------+-----------+-----------------------------------------+-----------+----------------------------------------+----------
  61. *
  62. * For pin E18, the SoC ANDs the expected state of three bits to determine the
  63. * pin's active signal:
  64. *
  65. * * SCU3C[3]: Enable external SOC reset function
  66. * * SCU80[15]: Enable SPICS1# or EXTRST# function pin
  67. * * SCU90[31]: Select SPI interface CS# output
  68. *
  69. * -----+---------+-----------+-----------------------------------------+-----------+----------------------------------------+----------
  70. * E18 GPIOB7 EXTRST# SCU3C[3]=1 & SCU80[15]=1 & SCU90[31]=0 SPICS1# SCU3C[3]=1 & SCU80[15]=1 & SCU90[31]=1 GPIOB7
  71. * -----+---------+-----------+-----------------------------------------+-----------+----------------------------------------+----------
  72. *
  73. * (Bits SCU3C[3] and SCU80[15] appear to only be used in the expressions for
  74. * selecting the signals on pin E18)
  75. *
  76. * Pin T5 is a multi-signal pin with a more complex configuration:
  77. *
  78. * Ball | Default | P0 Signal | P0 Expression | P1 Signal | P1 Expression | Other
  79. * -----+---------+-----------+------------------------------+-----------+---------------+----------
  80. * T5 GPIOL1 VPIDE SCU90[5:4]!=0 & SCU84[17]=1 NDCD1 SCU84[17]=1 GPIOL1
  81. * -----+---------+-----------+------------------------------+-----------+---------------+----------
  82. *
  83. * The high priority signal configuration is best thought of in terms of its
  84. * exploded form, with reference to the SCU90[5:4] bits:
  85. *
  86. * * SCU90[5:4]=00: disable
  87. * * SCU90[5:4]=01: 18 bits (R6/G6/B6) video mode.
  88. * * SCU90[5:4]=10: 24 bits (R8/G8/B8) video mode.
  89. * * SCU90[5:4]=11: 30 bits (R10/G10/B10) video mode.
  90. *
  91. * Re-writing:
  92. *
  93. * -----+---------+-----------+------------------------------+-----------+---------------+----------
  94. * T5 GPIOL1 VPIDE (SCU90[5:4]=1 & SCU84[17]=1) NDCD1 SCU84[17]=1 GPIOL1
  95. * | (SCU90[5:4]=2 & SCU84[17]=1)
  96. * | (SCU90[5:4]=3 & SCU84[17]=1)
  97. * -----+---------+-----------+------------------------------+-----------+---------------+----------
  98. *
  99. * For reference the SCU84[17] bit configure the "UART1 NDCD1 or Video VPIDE
  100. * function pin", where the signal itself is determined by whether SCU94[5:4]
  101. * is disabled or in one of the 18, 24 or 30bit video modes.
  102. *
  103. * Other video-input-related pins require an explicit state in SCU90[5:4], e.g.
  104. * W1 and U5:
  105. *
  106. * -----+---------+-----------+------------------------------+-----------+---------------+----------
  107. * W1 GPIOL6 VPIB0 SCU90[5:4]=3 & SCU84[22]=1 TXD1 SCU84[22]=1 GPIOL6
  108. * U5 GPIOL7 VPIB1 SCU90[5:4]=3 & SCU84[23]=1 RXD1 SCU84[23]=1 GPIOL7
  109. * -----+---------+-----------+------------------------------+-----------+---------------+----------
  110. *
  111. * The examples of T5 and W1 are particularly fertile, as they also demonstrate
  112. * that despite operating as part of the video input bus each signal needs to
  113. * be enabled individually via it's own SCU84 (in the cases of T5 and W1)
  114. * register bit. This is a little crazy if the bus doesn't have optional
  115. * signals, but is used to decent effect with some of the UARTs where not all
  116. * signals are required. However, this isn't done consistently - UART1 is
  117. * enabled on a per-pin basis, and by contrast, all signals for UART6 are
  118. * enabled by a single bit.
  119. *
  120. * Further, the high and low priority signals listed in the table above share
  121. * a configuration bit. The VPI signals should operate in concert in a single
  122. * function, but the UART signals should retain the ability to be configured
  123. * independently. This pushes the implementation down the path of tagging a
  124. * signal's expressions with the function they participate in, rather than
  125. * defining masks affecting multiple signals per function. The latter approach
  126. * fails in this instance where applying the configuration for the UART pin of
  127. * interest will stomp on the state of other UART signals when disabling the
  128. * VPI functions on the current pin.
  129. *
  130. * Ball | Default | P0 Signal | P0 Expression | P1 Signal | P1 Expression | Other
  131. * -----+------------+-----------+---------------------------+-----------+---------------+------------
  132. * A12 RGMII1TXCK GPIOT0 SCUA0[0]=1 RMII1TXEN Strap[6]=0 RGMII1TXCK
  133. * B12 RGMII1TXCTL GPIOT1 SCUA0[1]=1 – Strap[6]=0 RGMII1TXCTL
  134. * -----+------------+-----------+---------------------------+-----------+---------------+------------
  135. *
  136. * A12 demonstrates that the "Other" signal isn't always GPIO - in this case
  137. * GPIOT0 is a high-priority signal and RGMII1TXCK is Other. Thus, GPIO
  138. * should be treated like any other signal type with full function expression
  139. * requirements, and not assumed to be the default case. Separately, GPIOT0 and
  140. * GPIOT1's signal descriptor bits are distinct, therefore we must iterate all
  141. * pins in the function's group to disable the higher-priority signals such
  142. * that the signal for the function of interest is correctly enabled.
  143. *
  144. * Finally, three priority levels aren't always enough; the AST2500 brings with
  145. * it 18 pins of five priority levels, however the 18 pins only use three of
  146. * the five priority levels.
  147. *
  148. * Ultimately the requirement to control pins in the examples above drive the
  149. * design:
  150. *
  151. * * Pins provide signals according to functions activated in the mux
  152. * configuration
  153. *
  154. * * Pins provide up to five signal types in a priority order
  155. *
  156. * * For priorities levels defined on a pin, each priority provides one signal
  157. *
  158. * * Enabling lower priority signals requires higher priority signals be
  159. * disabled
  160. *
  161. * * A function represents a set of signals; functions are distinct if they
  162. * do not share a subset of signals (and may be distinct if they are a
  163. * strict subset).
  164. *
  165. * * Signals participate in one or more functions or groups
  166. *
  167. * * A function is described by an expression of one or more signal
  168. * descriptors, which compare bit values in a register
  169. *
  170. * * A signal expression is the smallest set of signal descriptors whose
  171. * comparisons must evaluate 'true' for a signal to be enabled on a pin.
  172. *
  173. * * A signal participating in a function is active on a pin if evaluating all
  174. * signal descriptors in the pin's signal expression for the function yields
  175. * a 'true' result
  176. *
  177. * * A signal at a given priority on a given pin is active if any of the
  178. * functions in which the signal participates are active, and no higher
  179. * priority signal on the pin is active
  180. *
  181. * * GPIO is configured per-pin
  182. *
  183. * And so:
  184. *
  185. * * To disable a signal, any function(s) activating the signal must be
  186. * disabled
  187. *
  188. * * Each pin must know the signal expressions of functions in which it
  189. * participates, for the purpose of enabling the Other function. This is done
  190. * by deactivating all functions that activate higher priority signals on the
  191. * pin.
  192. *
  193. * As a concrete example:
  194. *
  195. * * T5 provides three signals types: VPIDE, NDCD1 and GPIO
  196. *
  197. * * The VPIDE signal participates in 3 functions: VPI18, VPI24 and VPI30
  198. *
  199. * * The NDCD1 signal participates in just its own NDCD1 function
  200. *
  201. * * VPIDE is high priority, NDCD1 is low priority, and GPIOL1 is the least
  202. * prioritised
  203. *
  204. * * The prerequisit for activating the NDCD1 signal is that the VPI18, VPI24
  205. * and VPI30 functions all be disabled
  206. *
  207. * * Similarly, all of VPI18, VPI24, VPI30 and NDCD1 functions must be disabled
  208. * to provide GPIOL6
  209. *
  210. * Considerations
  211. * --------------
  212. *
  213. * If pinctrl allows us to allocate a pin we can configure a function without
  214. * concern for the function of already allocated pins, if pin groups are
  215. * created with respect to the SoC functions in which they participate. This is
  216. * intuitive, but it did not feel obvious from the bit/pin relationships.
  217. *
  218. * Conversely, failing to allocate all pins in a group indicates some bits (as
  219. * well as pins) required for the group's configuration will already be in use,
  220. * likely in a way that's inconsistent with the requirements of the failed
  221. * group.
  222. *
  223. * Implementation
  224. * --------------
  225. *
  226. * Beyond the documentation below the various structures and helper macros that
  227. * allow the implementation to hang together are defined. The macros are fairly
  228. * dense, so below we walk through some raw examples of the configuration
  229. * tables in an effort to clarify the concepts.
  230. *
  231. * The complexity of configuring the mux combined with the scale of the pins
  232. * and functions was a concern, so the table design along with the macro jungle
  233. * is an attempt to address it. The rough principles of the approach are:
  234. *
  235. * 1. Use a data-driven solution rather than embedding state into code
  236. * 2. Minimise editing to the specifics of the given mux configuration
  237. * 3. Detect as many errors as possible at compile time
  238. *
  239. * Addressing point 3 leads to naming of symbols in terms of the four
  240. * properties associated with a given mux configuration: The pin, the signal,
  241. * the group and the function. In this way copy/paste errors cause duplicate
  242. * symbols to be defined, which prevents successful compilation. Failing to
  243. * properly parent the tables leads to unused symbol warnings, and use of
  244. * designated initialisers and additional warnings ensures that there are
  245. * no override errors in the pin, group and function arrays.
  246. *
  247. * Addressing point 2 drives the development of the macro jungle, as it
  248. * centralises the definition noise at the cost of taking some time to
  249. * understand.
  250. *
  251. * Here's a complete, concrete "pre-processed" example of the table structures
  252. * used to describe the D6 ball from the examples above:
  253. *
  254. * ```
  255. * static const struct aspeed_sig_desc sig_descs_MAC1LINK_MAC1LINK[] = {
  256. * {
  257. * .ip = ASPEED_IP_SCU,
  258. * .reg = 0x80,
  259. * .mask = BIT(0),
  260. * .enable = 1,
  261. * .disable = 0
  262. * },
  263. * };
  264. *
  265. * static const struct aspeed_sig_expr sig_expr_MAC1LINK_MAC1LINK = {
  266. * .signal = "MAC1LINK",
  267. * .function = "MAC1LINK",
  268. * .ndescs = ARRAY_SIZE(sig_descs_MAC1LINK_MAC1LINK),
  269. * .descs = &(sig_descs_MAC1LINK_MAC1LINK)[0],
  270. * };
  271. *
  272. * static const struct aspeed_sig_expr *sig_exprs_MAC1LINK_MAC1LINK[] = {
  273. * &sig_expr_MAC1LINK_MAC1LINK,
  274. * NULL,
  275. * };
  276. *
  277. * static const struct aspeed_sig_desc sig_descs_GPIOA0_GPIOA0[] = { };
  278. *
  279. * static const struct aspeed_sig_expr sig_expr_GPIOA0_GPIOA0 = {
  280. * .signal = "GPIOA0",
  281. * .function = "GPIOA0",
  282. * .ndescs = ARRAY_SIZE(sig_descs_GPIOA0_GPIOA0),
  283. * .descs = &(sig_descs_GPIOA0_GPIOA0)[0],
  284. * };
  285. *
  286. * static const struct aspeed_sig_expr *sig_exprs_GPIOA0_GPIOA0[] = {
  287. * &sig_expr_GPIOA0_GPIOA0,
  288. * NULL
  289. * };
  290. *
  291. * static const struct aspeed_sig_expr **pin_exprs_0[] = {
  292. * sig_exprs_MAC1LINK_MAC1LINK,
  293. * sig_exprs_GPIOA0_GPIOA0,
  294. * NULL
  295. * };
  296. *
  297. * static const struct aspeed_pin_desc pin_0 = { "0", (&pin_exprs_0[0]) };
  298. * static const int group_pins_MAC1LINK[] = { 0 };
  299. * static const char *func_groups_MAC1LINK[] = { "MAC1LINK" };
  300. *
  301. * static struct pinctrl_pin_desc aspeed_g4_pins[] = {
  302. * [0] = { .number = 0, .name = "D6", .drv_data = &pin_0 },
  303. * };
  304. *
  305. * static const struct aspeed_pin_group aspeed_g4_groups[] = {
  306. * {
  307. * .name = "MAC1LINK",
  308. * .pins = &(group_pins_MAC1LINK)[0],
  309. * .npins = ARRAY_SIZE(group_pins_MAC1LINK),
  310. * },
  311. * };
  312. *
  313. * static const struct aspeed_pin_function aspeed_g4_functions[] = {
  314. * {
  315. * .name = "MAC1LINK",
  316. * .groups = &func_groups_MAC1LINK[0],
  317. * .ngroups = ARRAY_SIZE(func_groups_MAC1LINK),
  318. * },
  319. * };
  320. * ```
  321. *
  322. * At the end of the day much of the above code is compressed into the
  323. * following two lines:
  324. *
  325. * ```
  326. * #define D6 0
  327. * SSSF_PIN_DECL(D6, GPIOA0, MAC1LINK, SIG_DESC_SET(SCU80, 0));
  328. * ```
  329. *
  330. * The two examples below show just the differences from the example above.
  331. *
  332. * Ball E18 demonstrates a function, EXTRST, that requires multiple descriptors
  333. * be set for it to be muxed:
  334. *
  335. * ```
  336. * static const struct aspeed_sig_desc sig_descs_EXTRST_EXTRST[] = {
  337. * {
  338. * .ip = ASPEED_IP_SCU,
  339. * .reg = 0x3C,
  340. * .mask = BIT(3),
  341. * .enable = 1,
  342. * .disable = 0
  343. * },
  344. * {
  345. * .ip = ASPEED_IP_SCU,
  346. * .reg = 0x80,
  347. * .mask = BIT(15),
  348. * .enable = 1,
  349. * .disable = 0
  350. * },
  351. * {
  352. * .ip = ASPEED_IP_SCU,
  353. * .reg = 0x90,
  354. * .mask = BIT(31),
  355. * .enable = 0,
  356. * .disable = 1
  357. * },
  358. * };
  359. *
  360. * static const struct aspeed_sig_expr sig_expr_EXTRST_EXTRST = {
  361. * .signal = "EXTRST",
  362. * .function = "EXTRST",
  363. * .ndescs = ARRAY_SIZE(sig_descs_EXTRST_EXTRST),
  364. * .descs = &(sig_descs_EXTRST_EXTRST)[0],
  365. * };
  366. * ...
  367. * ```
  368. *
  369. * For ball E19, we have multiple functions enabling a single signal, LPCRST#.
  370. * The data structures look like:
  371. *
  372. * static const struct aspeed_sig_desc sig_descs_LPCRST_LPCRST[] = {
  373. * {
  374. * .ip = ASPEED_IP_SCU,
  375. * .reg = 0x80,
  376. * .mask = BIT(12),
  377. * .enable = 1,
  378. * .disable = 0
  379. * },
  380. * };
  381. *
  382. * static const struct aspeed_sig_expr sig_expr_LPCRST_LPCRST = {
  383. * .signal = "LPCRST",
  384. * .function = "LPCRST",
  385. * .ndescs = ARRAY_SIZE(sig_descs_LPCRST_LPCRST),
  386. * .descs = &(sig_descs_LPCRST_LPCRST)[0],
  387. * };
  388. *
  389. * static const struct aspeed_sig_desc sig_descs_LPCRST_LPCRSTS[] = {
  390. * {
  391. * .ip = ASPEED_IP_SCU,
  392. * .reg = 0x70,
  393. * .mask = BIT(14),
  394. * .enable = 1,
  395. * .disable = 0
  396. * },
  397. * };
  398. *
  399. * static const struct aspeed_sig_expr sig_expr_LPCRST_LPCRSTS = {
  400. * .signal = "LPCRST",
  401. * .function = "LPCRSTS",
  402. * .ndescs = ARRAY_SIZE(sig_descs_LPCRST_LPCRSTS),
  403. * .descs = &(sig_descs_LPCRST_LPCRSTS)[0],
  404. * };
  405. *
  406. * static const struct aspeed_sig_expr *sig_exprs_LPCRST_LPCRST[] = {
  407. * &sig_expr_LPCRST_LPCRST,
  408. * &sig_expr_LPCRST_LPCRSTS,
  409. * NULL,
  410. * };
  411. * ...
  412. * ```
  413. *
  414. * Both expressions listed in the sig_exprs_LPCRST_LPCRST array need to be set
  415. * to disabled for the associated GPIO to be muxed.
  416. *
  417. */
  418. #define ASPEED_IP_SCU 0
  419. #define ASPEED_IP_GFX 1
  420. #define ASPEED_IP_LPC 2
  421. #define ASPEED_NR_PINMUX_IPS 3
  422. /**
  423. * A signal descriptor, which describes the register, bits and the
  424. * enable/disable values that should be compared or written.
  425. *
  426. * @ip: The IP block identifier, used as an index into the regmap array in
  427. * struct aspeed_pinctrl_data
  428. * @reg: The register offset with respect to the base address of the IP block
  429. * @mask: The mask to apply to the register. The lowest set bit of the mask is
  430. * used to derive the shift value.
  431. * @enable: The value that enables the function. Value should be in the LSBs,
  432. * not at the position of the mask.
  433. * @disable: The value that disables the function. Value should be in the
  434. * LSBs, not at the position of the mask.
  435. */
  436. struct aspeed_sig_desc {
  437. unsigned int ip;
  438. unsigned int reg;
  439. u32 mask;
  440. u32 enable;
  441. u32 disable;
  442. };
  443. /**
  444. * Describes a signal expression. The expression is evaluated by ANDing the
  445. * evaluation of the descriptors.
  446. *
  447. * @signal: The signal name for the priority level on the pin. If the signal
  448. * type is GPIO, then the signal name must begin with the
  449. * prefix "GPI", e.g. GPIOA0, GPIT0 etc.
  450. * @function: The name of the function the signal participates in for the
  451. * associated expression. For pin-specific GPIO, the function
  452. * name must match the signal name.
  453. * @ndescs: The number of signal descriptors in the expression
  454. * @descs: Pointer to an array of signal descriptors that comprise the
  455. * function expression
  456. */
  457. struct aspeed_sig_expr {
  458. const char *signal;
  459. const char *function;
  460. int ndescs;
  461. const struct aspeed_sig_desc *descs;
  462. };
  463. /**
  464. * A struct capturing the list of expressions enabling signals at each priority
  465. * for a given pin. The signal configuration for a priority level is evaluated
  466. * by ORing the evaluation of the signal expressions in the respective
  467. * priority's list.
  468. *
  469. * @name: A name for the pin
  470. * @prios: A pointer to an array of expression list pointers
  471. *
  472. */
  473. struct aspeed_pin_desc {
  474. const char *name;
  475. const struct aspeed_sig_expr ***prios;
  476. };
  477. /* Macro hell */
  478. #define SIG_DESC_IP_BIT(ip, reg, idx, val) \
  479. { ip, reg, BIT_MASK(idx), val, (((val) + 1) & 1) }
  480. /**
  481. * Short-hand macro for describing an SCU descriptor enabled by the state of
  482. * one bit. The disable value is derived.
  483. *
  484. * @reg: The signal's associated register, offset from base
  485. * @idx: The signal's bit index in the register
  486. * @val: The value (0 or 1) that enables the function
  487. */
  488. #define SIG_DESC_BIT(reg, idx, val) \
  489. SIG_DESC_IP_BIT(ASPEED_IP_SCU, reg, idx, val)
  490. #define SIG_DESC_IP_SET(ip, reg, idx) SIG_DESC_IP_BIT(ip, reg, idx, 1)
  491. /**
  492. * A further short-hand macro expanding to an SCU descriptor enabled by a set
  493. * bit.
  494. *
  495. * @reg: The register, offset from base
  496. * @idx: The bit index in the register
  497. */
  498. #define SIG_DESC_SET(reg, idx) SIG_DESC_IP_BIT(ASPEED_IP_SCU, reg, idx, 1)
  499. #define SIG_DESC_CLEAR(reg, idx) { ASPEED_IP_SCU, reg, BIT_MASK(idx), 0, 0 }
  500. #define SIG_DESC_LIST_SYM(sig, group) sig_descs_ ## sig ## _ ## group
  501. #define SIG_DESC_LIST_DECL(sig, group, ...) \
  502. static const struct aspeed_sig_desc SIG_DESC_LIST_SYM(sig, group)[] = \
  503. { __VA_ARGS__ }
  504. #define SIG_EXPR_SYM(sig, group) sig_expr_ ## sig ## _ ## group
  505. #define SIG_EXPR_DECL_(sig, group, func) \
  506. static const struct aspeed_sig_expr SIG_EXPR_SYM(sig, group) = \
  507. { \
  508. .signal = #sig, \
  509. .function = #func, \
  510. .ndescs = ARRAY_SIZE(SIG_DESC_LIST_SYM(sig, group)), \
  511. .descs = &(SIG_DESC_LIST_SYM(sig, group))[0], \
  512. }
  513. /**
  514. * Declare a signal expression.
  515. *
  516. * @sig: A macro symbol name for the signal (is subjected to stringification
  517. * and token pasting)
  518. * @func: The function in which the signal is participating
  519. * @...: Signal descriptors that define the signal expression
  520. *
  521. * For example, the following declares the ROMD8 signal for the ROM16 function:
  522. *
  523. * SIG_EXPR_DECL(ROMD8, ROM16, ROM16, SIG_DESC_SET(SCU90, 6));
  524. *
  525. * And with multiple signal descriptors:
  526. *
  527. * SIG_EXPR_DECL(ROMD8, ROM16S, ROM16S, SIG_DESC_SET(HW_STRAP1, 4),
  528. * { HW_STRAP1, GENMASK(1, 0), 0, 0 });
  529. */
  530. #define SIG_EXPR_DECL(sig, group, func, ...) \
  531. SIG_DESC_LIST_DECL(sig, group, __VA_ARGS__); \
  532. SIG_EXPR_DECL_(sig, group, func)
  533. /**
  534. * Declare a pointer to a signal expression
  535. *
  536. * @sig: The macro symbol name for the signal (subjected to token pasting)
  537. * @func: The macro symbol name for the function (subjected to token pasting)
  538. */
  539. #define SIG_EXPR_PTR(sig, group) (&SIG_EXPR_SYM(sig, group))
  540. #define SIG_EXPR_LIST_SYM(sig, group) sig_exprs_ ## sig ## _ ## group
  541. /**
  542. * Declare a signal expression list for reference in a struct aspeed_pin_prio.
  543. *
  544. * @sig: A macro symbol name for the signal (is subjected to token pasting)
  545. * @...: Signal expression structure pointers (use SIG_EXPR_PTR())
  546. *
  547. * For example, the 16-bit ROM bus can be enabled by one of two possible signal
  548. * expressions:
  549. *
  550. * SIG_EXPR_DECL(ROMD8, ROM16, ROM16, SIG_DESC_SET(SCU90, 6));
  551. * SIG_EXPR_DECL(ROMD8, ROM16S, ROM16S, SIG_DESC_SET(HW_STRAP1, 4),
  552. * { HW_STRAP1, GENMASK(1, 0), 0, 0 });
  553. * SIG_EXPR_LIST_DECL(ROMD8, SIG_EXPR_PTR(ROMD8, ROM16),
  554. * SIG_EXPR_PTR(ROMD8, ROM16S));
  555. */
  556. #define SIG_EXPR_LIST_DECL(sig, group, ...) \
  557. static const struct aspeed_sig_expr *SIG_EXPR_LIST_SYM(sig, group)[] =\
  558. { __VA_ARGS__, NULL }
  559. #define stringify(x) #x
  560. #define istringify(x) stringify(x)
  561. /**
  562. * Create an expression symbol alias from (signal, group) to (pin, signal).
  563. *
  564. * @pin: The pin number
  565. * @sig: The signal name
  566. * @group: The name of the group of which the pin is a member that is
  567. * associated with the function's signal
  568. *
  569. * Using an alias in this way enables detection of copy/paste errors (defining
  570. * the signal for a group multiple times) whilst enabling multiple pin groups
  571. * to exist for a signal without intrusive side-effects on defining the list of
  572. * signals available on a pin.
  573. */
  574. #define SIG_EXPR_LIST_ALIAS(pin, sig, group) \
  575. static const struct aspeed_sig_expr *\
  576. SIG_EXPR_LIST_SYM(pin, sig)[ARRAY_SIZE(SIG_EXPR_LIST_SYM(sig, group))] \
  577. __attribute__((alias(istringify(SIG_EXPR_LIST_SYM(sig, group)))))
  578. /**
  579. * A short-hand macro for declaring a function expression and an expression
  580. * list with a single expression (SE) and a single group (SG) of pins.
  581. *
  582. * @pin: The pin the signal will be routed to
  583. * @sig: The signal that will be routed to the pin for the function
  584. * @func: A macro symbol name for the function
  585. * @...: Function descriptors that define the function expression
  586. *
  587. * For example, signal NCTS6 participates in its own function with one group:
  588. *
  589. * SIG_EXPR_LIST_DECL_SINGLE(A18, NCTS6, NCTS6, SIG_DESC_SET(SCU90, 7));
  590. */
  591. #define SIG_EXPR_LIST_DECL_SESG(pin, sig, func, ...) \
  592. SIG_DESC_LIST_DECL(sig, func, __VA_ARGS__); \
  593. SIG_EXPR_DECL_(sig, func, func); \
  594. SIG_EXPR_LIST_DECL(sig, func, SIG_EXPR_PTR(sig, func)); \
  595. SIG_EXPR_LIST_ALIAS(pin, sig, func)
  596. /**
  597. * Similar to the above, but for pins with a single expression (SE) and
  598. * multiple groups (MG) of pins.
  599. *
  600. * @pin: The pin the signal will be routed to
  601. * @sig: The signal that will be routed to the pin for the function
  602. * @group: The name of the function's pin group in which the pin participates
  603. * @func: A macro symbol name for the function
  604. * @...: Function descriptors that define the function expression
  605. */
  606. #define SIG_EXPR_LIST_DECL_SEMG(pin, sig, group, func, ...) \
  607. SIG_DESC_LIST_DECL(sig, group, __VA_ARGS__); \
  608. SIG_EXPR_DECL_(sig, group, func); \
  609. SIG_EXPR_LIST_DECL(sig, group, SIG_EXPR_PTR(sig, group)); \
  610. SIG_EXPR_LIST_ALIAS(pin, sig, group)
  611. /**
  612. * Similar to the above, but for pins with a dual expressions (DE)
  613. * and a single group (SG) of pins.
  614. *
  615. * @pin: The pin the signal will be routed to
  616. * @sig: The signal that will be routed to the pin for the function
  617. * @group: The name of the function's pin group in which the pin participates
  618. * @func: A macro symbol name for the function
  619. * @...: Function descriptors that define the function expression
  620. */
  621. #define SIG_EXPR_LIST_DECL_DESG(pin, sig, f0, f1) \
  622. SIG_EXPR_LIST_DECL(sig, f0, \
  623. SIG_EXPR_PTR(sig, f0), \
  624. SIG_EXPR_PTR(sig, f1)); \
  625. SIG_EXPR_LIST_ALIAS(pin, sig, f0)
  626. #define SIG_EXPR_LIST_PTR(sig, group) SIG_EXPR_LIST_SYM(sig, group)
  627. #define PIN_EXPRS_SYM(pin) pin_exprs_ ## pin
  628. #define PIN_EXPRS_PTR(pin) (&PIN_EXPRS_SYM(pin)[0])
  629. #define PIN_SYM(pin) pin_ ## pin
  630. #define PIN_DECL_(pin, ...) \
  631. static const struct aspeed_sig_expr **PIN_EXPRS_SYM(pin)[] = \
  632. { __VA_ARGS__, NULL }; \
  633. static const struct aspeed_pin_desc PIN_SYM(pin) = \
  634. { #pin, PIN_EXPRS_PTR(pin) }
  635. /**
  636. * Declare a single signal pin
  637. *
  638. * @pin: The pin number
  639. * @other: Macro name for "other" functionality (subjected to stringification)
  640. * @sig: Macro name for the signal (subjected to stringification)
  641. *
  642. * For example:
  643. *
  644. * #define E3 80
  645. * SIG_EXPR_LIST_DECL_SINGLE(SCL5, I2C5, I2C5_DESC);
  646. * PIN_DECL_1(E3, GPIOK0, SCL5);
  647. */
  648. #define PIN_DECL_1(pin, other, sig) \
  649. SIG_EXPR_LIST_DECL_SESG(pin, other, other); \
  650. PIN_DECL_(pin, SIG_EXPR_LIST_PTR(pin, sig), \
  651. SIG_EXPR_LIST_PTR(pin, other))
  652. /**
  653. * Single signal, single function pin declaration
  654. *
  655. * @pin: The pin number
  656. * @other: Macro name for "other" functionality (subjected to stringification)
  657. * @sig: Macro name for the signal (subjected to stringification)
  658. * @...: Signal descriptors that define the function expression
  659. *
  660. * For example:
  661. *
  662. * SSSF_PIN_DECL(A4, GPIOA2, TIMER3, SIG_DESC_SET(SCU80, 2));
  663. */
  664. #define SSSF_PIN_DECL(pin, other, sig, ...) \
  665. SIG_EXPR_LIST_DECL_SESG(pin, sig, sig, __VA_ARGS__); \
  666. SIG_EXPR_LIST_DECL_SESG(pin, other, other); \
  667. PIN_DECL_(pin, SIG_EXPR_LIST_PTR(pin, sig), \
  668. SIG_EXPR_LIST_PTR(pin, other)); \
  669. FUNC_GROUP_DECL(sig, pin)
  670. /**
  671. * Declare a two-signal pin
  672. *
  673. * @pin: The pin number
  674. * @other: Macro name for "other" functionality (subjected to stringification)
  675. * @high: Macro name for the highest priority signal functions
  676. * @low: Macro name for the low signal functions
  677. *
  678. * For example:
  679. *
  680. * #define A8 56
  681. * SIG_EXPR_DECL(ROMD8, ROM16, SIG_DESC_SET(SCU90, 6));
  682. * SIG_EXPR_DECL(ROMD8, ROM16S, SIG_DESC_SET(HW_STRAP1, 4),
  683. * { HW_STRAP1, GENMASK(1, 0), 0, 0 });
  684. * SIG_EXPR_LIST_DECL(ROMD8, SIG_EXPR_PTR(ROMD8, ROM16),
  685. * SIG_EXPR_PTR(ROMD8, ROM16S));
  686. * SIG_EXPR_LIST_DECL_SINGLE(NCTS6, NCTS6, SIG_DESC_SET(SCU90, 7));
  687. * PIN_DECL_2(A8, GPIOH0, ROMD8, NCTS6);
  688. */
  689. #define PIN_DECL_2(pin, other, high, low) \
  690. SIG_EXPR_LIST_DECL_SESG(pin, other, other); \
  691. PIN_DECL_(pin, \
  692. SIG_EXPR_LIST_PTR(pin, high), \
  693. SIG_EXPR_LIST_PTR(pin, low), \
  694. SIG_EXPR_LIST_PTR(pin, other))
  695. #define PIN_DECL_3(pin, other, high, medium, low) \
  696. SIG_EXPR_LIST_DECL_SESG(pin, other, other); \
  697. PIN_DECL_(pin, \
  698. SIG_EXPR_LIST_PTR(pin, high), \
  699. SIG_EXPR_LIST_PTR(pin, medium), \
  700. SIG_EXPR_LIST_PTR(pin, low), \
  701. SIG_EXPR_LIST_PTR(pin, other))
  702. #define PIN_DECL_4(pin, other, prio1, prio2, prio3, prio4) \
  703. SIG_EXPR_LIST_DECL_SESG(pin, other, other); \
  704. PIN_DECL_(pin, \
  705. SIG_EXPR_LIST_PTR(pin, prio1), \
  706. SIG_EXPR_LIST_PTR(pin, prio2), \
  707. SIG_EXPR_LIST_PTR(pin, prio3), \
  708. SIG_EXPR_LIST_PTR(pin, prio4), \
  709. SIG_EXPR_LIST_PTR(pin, other))
  710. #define GROUP_SYM(group) group_pins_ ## group
  711. #define GROUP_DECL(group, ...) \
  712. static const int GROUP_SYM(group)[] = { __VA_ARGS__ }
  713. #define FUNC_SYM(func) func_groups_ ## func
  714. #define FUNC_DECL_(func, ...) \
  715. static const char *FUNC_SYM(func)[] = { __VA_ARGS__ }
  716. #define FUNC_DECL_1(func, group) FUNC_DECL_(func, #group)
  717. #define FUNC_DECL_2(func, one, two) FUNC_DECL_(func, #one, #two)
  718. #define FUNC_DECL_3(func, one, two, three) FUNC_DECL_(func, #one, #two, #three)
  719. #define FUNC_GROUP_DECL(func, ...) \
  720. GROUP_DECL(func, __VA_ARGS__); \
  721. FUNC_DECL_(func, #func)
  722. #define GPIO_PIN_DECL(pin, gpio) \
  723. SIG_EXPR_LIST_DECL_SESG(pin, gpio, gpio); \
  724. PIN_DECL_(pin, SIG_EXPR_LIST_PTR(pin, gpio))
  725. struct aspeed_pin_group {
  726. const char *name;
  727. const unsigned int *pins;
  728. const unsigned int npins;
  729. };
  730. #define ASPEED_PINCTRL_GROUP(name_) { \
  731. .name = #name_, \
  732. .pins = &(GROUP_SYM(name_))[0], \
  733. .npins = ARRAY_SIZE(GROUP_SYM(name_)), \
  734. }
  735. struct aspeed_pin_function {
  736. const char *name;
  737. const char *const *groups;
  738. unsigned int ngroups;
  739. };
  740. #define ASPEED_PINCTRL_FUNC(name_, ...) { \
  741. .name = #name_, \
  742. .groups = &FUNC_SYM(name_)[0], \
  743. .ngroups = ARRAY_SIZE(FUNC_SYM(name_)), \
  744. }
  745. struct aspeed_pinmux_data;
  746. struct aspeed_pinmux_ops {
  747. int (*eval)(struct aspeed_pinmux_data *ctx,
  748. const struct aspeed_sig_expr *expr, bool enabled);
  749. int (*set)(struct aspeed_pinmux_data *ctx,
  750. const struct aspeed_sig_expr *expr, bool enabled);
  751. };
  752. struct aspeed_pinmux_data {
  753. struct device *dev;
  754. struct regmap *maps[ASPEED_NR_PINMUX_IPS];
  755. const struct aspeed_pinmux_ops *ops;
  756. const struct aspeed_pin_group *groups;
  757. const unsigned int ngroups;
  758. const struct aspeed_pin_function *functions;
  759. const unsigned int nfunctions;
  760. };
  761. int aspeed_sig_desc_eval(const struct aspeed_sig_desc *desc, bool enabled,
  762. struct regmap *map);
  763. int aspeed_sig_expr_eval(struct aspeed_pinmux_data *ctx,
  764. const struct aspeed_sig_expr *expr, bool enabled);
  765. static inline int aspeed_sig_expr_set(struct aspeed_pinmux_data *ctx,
  766. const struct aspeed_sig_expr *expr,
  767. bool enabled)
  768. {
  769. return ctx->ops->set(ctx, expr, enabled);
  770. }
  771. #endif /* ASPEED_PINMUX_H */