Mate Kukri | 62c2535 | 2021-06-06 14:00:57 +0100 | [diff] [blame^] | 1 | ;;/* SPDX-License-Identifier: GPL-2.0-only */ |
| 2 | |
| 3 | #include <arch/io.h> |
| 4 | #include <console/console.h> |
| 5 | #include <device/pnp.h> |
| 6 | #include <pc80/keyboard.h> |
| 7 | #include <superio/conf_mode.h> |
| 8 | #include "sch555x.h" |
| 9 | |
| 10 | static void sch555x_init(struct device *dev) |
| 11 | { |
| 12 | if (dev->enabled && dev->path.pnp.device == SCH555x_LDN_8042) |
| 13 | pc_keyboard_init(NO_AUX_DEVICE); |
| 14 | } |
| 15 | |
| 16 | static uint8_t sch555x_ldn_to_bar(uint8_t ldn) |
| 17 | { |
| 18 | switch (ldn) { |
| 19 | case SCH555x_LDN_LPCI: |
| 20 | return SCH555x_LPCI_LPCI_BAR; |
| 21 | case SCH555x_LDN_EMI: |
| 22 | return SCH555x_LPCI_EMI_BAR; |
| 23 | case SCH555x_LDN_UART1: |
| 24 | return SCH555x_LPCI_UART1_BAR; |
| 25 | case SCH555x_LDN_UART2: |
| 26 | return SCH555x_LPCI_UART2_BAR; |
| 27 | case SCH555x_LDN_RUNTIME: |
| 28 | return SCH555x_LPCI_RUNTIME_BAR; |
| 29 | case SCH555x_LDN_8042: |
| 30 | return SCH555x_LPCI_8042_BAR; |
| 31 | case SCH555x_LDN_FDC: |
| 32 | return SCH555x_LPCI_FDC_BAR; |
| 33 | case SCH555x_LDN_PP: |
| 34 | return SCH555x_LPCI_PP_BAR; |
| 35 | default: |
| 36 | return 0; |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | /* |
| 41 | * IO BARs don't live in normal LDN configuration space but in the LPC interface. |
| 42 | * Thus we ignore the index and choose what BAR to set just based on the LDN. |
| 43 | */ |
| 44 | static void sch555x_set_iobase(struct device *lpci, struct device *dev, |
| 45 | uint8_t index, uint16_t iobase) |
| 46 | { |
| 47 | const uint8_t bar = sch555x_ldn_to_bar(dev->path.pnp.device); |
| 48 | if (bar) { |
| 49 | pnp_set_logical_device(lpci); |
| 50 | pnp_unset_and_set_config(lpci, bar + 1, 0, 1 << 7); |
| 51 | pnp_write_config(lpci, bar + 2, iobase & 0xff); |
| 52 | pnp_write_config(lpci, bar + 3, (iobase >> 8) & 0xff); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | /* |
| 57 | * IRQs don't live in normal LDN configuration space but in the LPC interface. |
| 58 | * |
| 59 | * The following fake offsets are used: |
| 60 | * 0x70 => First IRQ |
| 61 | * 0x72 => Second IRQ |
| 62 | */ |
| 63 | static void sch555x_set_irq(struct device *lpci, struct device *dev, |
| 64 | uint8_t index, uint8_t irq) |
| 65 | { |
| 66 | if (index >= PNP_IDX_MSC0) { |
| 67 | pnp_set_logical_device(dev); |
| 68 | pnp_write_config(dev, index, irq); |
| 69 | return; |
| 70 | } |
| 71 | |
| 72 | pnp_set_logical_device(lpci); |
| 73 | switch (index) { |
| 74 | case 0x70: |
| 75 | pnp_write_config(lpci, SCH555x_LPCI_IRQ(irq), dev->path.pnp.device); |
| 76 | break; |
| 77 | case 0x72: |
| 78 | pnp_write_config(lpci, SCH555x_LPCI_IRQ(irq), dev->path.pnp.device | 0x80); |
| 79 | break; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | /* |
| 84 | * DMA channels don't live in normal LDN configuration space but in the LPC interface. |
| 85 | */ |
| 86 | static void sch555x_set_drq(struct device *lpci, struct device *dev, |
| 87 | uint8_t index, uint8_t drq) |
| 88 | { |
| 89 | pnp_set_logical_device(lpci); |
| 90 | pnp_write_config(lpci, SCH555x_LPCI_DMA(drq), dev->path.pnp.device | 0x80); |
| 91 | } |
| 92 | |
| 93 | static void sch555x_set_resources(struct device *dev) |
| 94 | { |
| 95 | struct device *lpci = dev_find_slot_pnp(dev->path.pnp.port, SCH555x_LDN_LPCI); |
| 96 | if (!lpci) { |
| 97 | printk(BIOS_ERR, "SCH555x LPC interface not present in device tree!\n"); |
| 98 | return; |
| 99 | } |
| 100 | |
| 101 | pnp_enter_conf_mode(dev); |
| 102 | for (struct resource *res = dev->resource_list; res; res = res->next) { |
| 103 | if (res->flags & IORESOURCE_IO) |
| 104 | sch555x_set_iobase(lpci, dev, res->index, res->base); |
| 105 | else if (res->flags & IORESOURCE_DRQ) |
| 106 | sch555x_set_drq(lpci, dev, res->index, res->base); |
| 107 | else if (res->flags & IORESOURCE_IRQ) |
| 108 | sch555x_set_irq(lpci, dev, res->index, res->base); |
| 109 | } |
| 110 | pnp_exit_conf_mode(dev); |
| 111 | } |
| 112 | |
| 113 | static void sch555x_enable_dev(struct device *dev) |
| 114 | { |
| 115 | static struct device_operations ops = { |
| 116 | .read_resources = pnp_read_resources, |
| 117 | .set_resources = sch555x_set_resources, |
| 118 | .enable_resources = pnp_enable_resources, |
| 119 | .enable = pnp_alt_enable, |
| 120 | .init = sch555x_init, |
| 121 | .ops_pnp_mode = &pnp_conf_mode_55_aa, |
| 122 | }; |
| 123 | |
| 124 | static struct pnp_info pnp_dev_info[] = { |
| 125 | { NULL, SCH555x_LDN_EMI, PNP_IO0 | PNP_IRQ0 | PNP_IRQ1, 0x0ff0 }, |
| 126 | { NULL, SCH555x_LDN_8042, PNP_IO0 | PNP_IRQ0 | PNP_IRQ1, 0x0fff }, |
| 127 | { NULL, SCH555x_LDN_UART1, PNP_IO0 | PNP_IRQ0 | PNP_MSC0, 0x0ff8 }, |
| 128 | { NULL, SCH555x_LDN_UART2, PNP_IO0 | PNP_IRQ0 | PNP_MSC0, 0x0ff8 }, |
| 129 | { NULL, SCH555x_LDN_LPCI, PNP_IO0, 0x0ffe }, |
| 130 | { NULL, SCH555x_LDN_RUNTIME, PNP_IO0 | PNP_IRQ0 | PNP_IRQ1, 0x0fc0 }, |
| 131 | { NULL, SCH555x_LDN_FDC, PNP_IO0 | PNP_IRQ0 | PNP_DRQ0, 0x0ff8, }, |
| 132 | { NULL, SCH555x_LDN_PP, PNP_IO0 | PNP_IRQ0 | PNP_DRQ0, 0x0ff8 }, |
| 133 | }; |
| 134 | |
| 135 | pnp_enable_devices(dev, &ops, ARRAY_SIZE(pnp_dev_info), pnp_dev_info); |
| 136 | } |
| 137 | |
| 138 | struct chip_operations superio_smsc_sch555x_ops = { |
| 139 | CHIP_NAME("SMSC SCH555x Super I/O") |
| 140 | .enable_dev = sch555x_enable_dev, |
| 141 | }; |