Marek Vasut | cbde9de | 2019-05-04 14:17:10 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * R7S72100 processor support |
| 4 | * |
| 5 | * Copyright (C) 2019 Marek Vasut <marek.vasut@gmail.com> |
| 6 | */ |
| 7 | |
| 8 | #include <common.h> |
| 9 | #include <dm.h> |
| 10 | #include <dm/lists.h> |
| 11 | #include <dm/pinctrl.h> |
| 12 | #include <linux/io.h> |
| 13 | #include <linux/err.h> |
| 14 | |
| 15 | #define P(bank) (0x0000 + (bank) * 4) |
| 16 | #define PSR(bank) (0x0100 + (bank) * 4) |
| 17 | #define PPR(bank) (0x0200 + (bank) * 4) |
| 18 | #define PM(bank) (0x0300 + (bank) * 4) |
| 19 | #define PMC(bank) (0x0400 + (bank) * 4) |
| 20 | #define PFC(bank) (0x0500 + (bank) * 4) |
| 21 | #define PFCE(bank) (0x0600 + (bank) * 4) |
| 22 | #define PNOT(bank) (0x0700 + (bank) * 4) |
| 23 | #define PMSR(bank) (0x0800 + (bank) * 4) |
| 24 | #define PMCSR(bank) (0x0900 + (bank) * 4) |
| 25 | #define PFCAE(bank) (0x0A00 + (bank) * 4) |
| 26 | #define PIBC(bank) (0x4000 + (bank) * 4) |
| 27 | #define PBDC(bank) (0x4100 + (bank) * 4) |
| 28 | #define PIPC(bank) (0x4200 + (bank) * 4) |
| 29 | |
| 30 | #define RZA1_PINS_PER_PORT 16 |
| 31 | |
| 32 | DECLARE_GLOBAL_DATA_PTR; |
| 33 | |
| 34 | struct r7s72100_pfc_platdata { |
| 35 | void __iomem *base; |
| 36 | }; |
| 37 | |
| 38 | static void r7s72100_pfc_set_function(struct udevice *dev, u16 bank, u16 line, |
| 39 | u16 func, u16 inbuf, u16 bidir) |
| 40 | { |
| 41 | struct r7s72100_pfc_platdata *plat = dev_get_platdata(dev); |
| 42 | |
| 43 | clrsetbits_le16(plat->base + PFCAE(bank), BIT(line), |
| 44 | (func & BIT(2)) ? BIT(line) : 0); |
| 45 | clrsetbits_le16(plat->base + PFCE(bank), BIT(line), |
| 46 | (func & BIT(1)) ? BIT(line) : 0); |
| 47 | clrsetbits_le16(plat->base + PFC(bank), BIT(line), |
| 48 | (func & BIT(0)) ? BIT(line) : 0); |
| 49 | |
| 50 | clrsetbits_le16(plat->base + PIBC(bank), BIT(line), |
| 51 | inbuf ? BIT(line) : 0); |
| 52 | clrsetbits_le16(plat->base + PBDC(bank), BIT(line), |
| 53 | bidir ? BIT(line) : 0); |
| 54 | |
| 55 | setbits_le32(plat->base + PMCSR(bank), BIT(line + 16) | BIT(line)); |
| 56 | |
| 57 | setbits_le16(plat->base + PIPC(bank), BIT(line)); |
| 58 | } |
| 59 | |
| 60 | static int r7s72100_pfc_set_state(struct udevice *dev, struct udevice *config) |
| 61 | { |
| 62 | const void *blob = gd->fdt_blob; |
| 63 | int node = dev_of_offset(config); |
| 64 | u32 cells[32]; |
| 65 | u16 bank, line, func; |
| 66 | int i, count, bidir; |
| 67 | |
| 68 | count = fdtdec_get_int_array_count(blob, node, "pinmux", |
| 69 | cells, ARRAY_SIZE(cells)); |
| 70 | if (count < 0) { |
| 71 | printf("%s: bad pinmux array %d\n", __func__, count); |
| 72 | return -EINVAL; |
| 73 | } |
| 74 | |
| 75 | if (count > ARRAY_SIZE(cells)) { |
| 76 | printf("%s: unsupported pinmux array count %d\n", |
| 77 | __func__, count); |
| 78 | return -EINVAL; |
| 79 | } |
| 80 | |
| 81 | for (i = 0 ; i < count; i++) { |
| 82 | func = (cells[i] >> 16) & 0xf; |
| 83 | if (func == 0 || func > 8) { |
| 84 | printf("Invalid cell %i in node %s!\n", |
| 85 | count, ofnode_get_name(dev_ofnode(config))); |
| 86 | continue; |
| 87 | } |
| 88 | |
| 89 | func = (func - 1) & 0x7; |
| 90 | |
| 91 | bank = (cells[i] / RZA1_PINS_PER_PORT) & 0xff; |
| 92 | line = cells[i] % RZA1_PINS_PER_PORT; |
| 93 | |
| 94 | bidir = 0; |
| 95 | if (bank == 3 && line == 3 && func == 1) |
| 96 | bidir = 1; |
| 97 | |
| 98 | r7s72100_pfc_set_function(dev, bank, line, func, 0, bidir); |
| 99 | } |
| 100 | |
| 101 | return 0; |
| 102 | } |
| 103 | |
| 104 | const struct pinctrl_ops r7s72100_pfc_ops = { |
| 105 | .set_state = r7s72100_pfc_set_state, |
| 106 | }; |
| 107 | |
| 108 | static int r7s72100_pfc_probe(struct udevice *dev) |
| 109 | { |
| 110 | struct r7s72100_pfc_platdata *plat = dev_get_platdata(dev); |
| 111 | fdt_addr_t addr_base; |
| 112 | ofnode node; |
| 113 | |
| 114 | addr_base = devfdt_get_addr(dev); |
| 115 | if (addr_base == FDT_ADDR_T_NONE) |
| 116 | return -EINVAL; |
| 117 | |
| 118 | plat->base = (void __iomem *)addr_base; |
| 119 | |
| 120 | dev_for_each_subnode(node, dev) { |
| 121 | struct udevice *cdev; |
| 122 | |
| 123 | if (!ofnode_read_bool(node, "gpio-controller")) |
| 124 | continue; |
| 125 | |
| 126 | device_bind_driver_to_node(dev, "r7s72100-gpio", |
| 127 | ofnode_get_name(node), |
| 128 | node, &cdev); |
| 129 | } |
| 130 | |
| 131 | return 0; |
| 132 | } |
| 133 | |
| 134 | static const struct udevice_id r7s72100_pfc_match[] = { |
| 135 | { .compatible = "renesas,r7s72100-ports" }, |
| 136 | {} |
| 137 | }; |
| 138 | |
| 139 | U_BOOT_DRIVER(r7s72100_pfc) = { |
| 140 | .name = "r7s72100_pfc", |
| 141 | .id = UCLASS_PINCTRL, |
| 142 | .of_match = r7s72100_pfc_match, |
| 143 | .probe = r7s72100_pfc_probe, |
| 144 | .platdata_auto_alloc_size = sizeof(struct r7s72100_pfc_platdata), |
| 145 | .ops = &r7s72100_pfc_ops, |
| 146 | }; |