Angel Pons | 182dbde | 2020-04-02 23:49:05 +0200 | [diff] [blame^] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
| 2 | /* This file is part of the coreboot project. */ |
Frank Vibrans | 63e62b0 | 2011-02-14 18:38:14 +0000 | [diff] [blame] | 3 | |
| 4 | |
| 5 | #include <arch/io.h> |
| 6 | #include "smbus.h" |
Kerry She | feed329 | 2011-08-18 18:03:44 +0800 | [diff] [blame] | 7 | #include <console/console.h> /* printk */ |
Frank Vibrans | 63e62b0 | 2011-02-14 18:38:14 +0000 | [diff] [blame] | 8 | |
Frank Vibrans | 63e62b0 | 2011-02-14 18:38:14 +0000 | [diff] [blame] | 9 | static int smbus_wait_until_ready(u32 smbus_io_base) |
| 10 | { |
| 11 | u32 loops; |
| 12 | |
| 13 | loops = SMBUS_TIMEOUT; |
| 14 | do { |
| 15 | u8 val; |
| 16 | val = inb(smbus_io_base + SMBHSTSTAT); |
| 17 | val &= 0x1f; |
| 18 | if (val == 0) { /* ready now */ |
| 19 | return 0; |
| 20 | } |
| 21 | outb(val, smbus_io_base + SMBHSTSTAT); |
| 22 | } while (--loops); |
| 23 | |
| 24 | return -2; /* time out */ |
| 25 | } |
| 26 | |
| 27 | static int smbus_wait_until_done(u32 smbus_io_base) |
| 28 | { |
| 29 | u32 loops; |
| 30 | |
| 31 | loops = SMBUS_TIMEOUT; |
| 32 | do { |
| 33 | u8 val; |
| 34 | |
| 35 | val = inb(smbus_io_base + SMBHSTSTAT); |
| 36 | val &= 0x1f; /* mask off reserved bits */ |
| 37 | if (val & 0x1c) { |
| 38 | return -5; /* error */ |
| 39 | } |
| 40 | if (val == 0x02) { |
| 41 | outb(val, smbus_io_base + SMBHSTSTAT); /* clear status */ |
| 42 | return 0; |
| 43 | } |
| 44 | } while (--loops); |
| 45 | |
| 46 | return -3; /* timeout */ |
| 47 | } |
| 48 | |
| 49 | int do_smbus_recv_byte(u32 smbus_io_base, u32 device) |
| 50 | { |
| 51 | u8 byte; |
| 52 | |
| 53 | if (smbus_wait_until_ready(smbus_io_base) < 0) { |
Elyes HAOUAS | ba28e8d | 2016-08-31 19:22:16 +0200 | [diff] [blame] | 54 | printk(BIOS_DEBUG, "SB800 - Smbus.c - do_smbus_recv_byte - smbus not ready.\n"); |
Frank Vibrans | 63e62b0 | 2011-02-14 18:38:14 +0000 | [diff] [blame] | 55 | return -2; /* not ready */ |
| 56 | } |
| 57 | |
Kerry She | feed329 | 2011-08-18 18:03:44 +0800 | [diff] [blame] | 58 | printk(BIOS_DEBUG, "SB800 - Smbus.c - do_smbus_recv_byte - Start.\n"); |
Jonathan Neuschäfer | 7090377 | 2017-09-23 21:39:02 +0200 | [diff] [blame] | 59 | /* set the device I'm talking to */ |
Frank Vibrans | 63e62b0 | 2011-02-14 18:38:14 +0000 | [diff] [blame] | 60 | outb(((device & 0x7f) << 1) | 1, smbus_io_base + SMBHSTADDR); |
| 61 | |
| 62 | byte = inb(smbus_io_base + SMBHSTCTRL); |
| 63 | byte &= 0xe3; /* Clear [4:2] */ |
| 64 | byte |= (1 << 2) | (1 << 6); /* Byte data read/write command, start the command */ |
| 65 | outb(byte, smbus_io_base + SMBHSTCTRL); |
| 66 | |
| 67 | /* poll for transaction completion */ |
| 68 | if (smbus_wait_until_done(smbus_io_base) < 0) { |
| 69 | return -3; /* timeout or error */ |
| 70 | } |
| 71 | |
| 72 | /* read results of transaction */ |
| 73 | byte = inb(smbus_io_base + SMBHSTCMD); |
| 74 | |
Kerry She | feed329 | 2011-08-18 18:03:44 +0800 | [diff] [blame] | 75 | printk(BIOS_DEBUG, "SB800 - Smbus.c - do_smbus_recv_byte - End.\n"); |
Frank Vibrans | 63e62b0 | 2011-02-14 18:38:14 +0000 | [diff] [blame] | 76 | return byte; |
| 77 | } |
| 78 | |
| 79 | int do_smbus_send_byte(u32 smbus_io_base, u32 device, u8 val) |
| 80 | { |
| 81 | u8 byte; |
| 82 | |
| 83 | if (smbus_wait_until_ready(smbus_io_base) < 0) { |
Elyes HAOUAS | ba28e8d | 2016-08-31 19:22:16 +0200 | [diff] [blame] | 84 | printk(BIOS_DEBUG, "SB800 - Smbus.c - do_smbus_send_byte - smbus not ready.\n"); |
Frank Vibrans | 63e62b0 | 2011-02-14 18:38:14 +0000 | [diff] [blame] | 85 | return -2; /* not ready */ |
| 86 | } |
| 87 | |
Kerry She | feed329 | 2011-08-18 18:03:44 +0800 | [diff] [blame] | 88 | printk(BIOS_DEBUG, "SB800 - Smbus.c - do_smbus_send_byte - Start.\n"); |
Frank Vibrans | 63e62b0 | 2011-02-14 18:38:14 +0000 | [diff] [blame] | 89 | /* set the command... */ |
| 90 | outb(val, smbus_io_base + SMBHSTCMD); |
| 91 | |
Jonathan Neuschäfer | 7090377 | 2017-09-23 21:39:02 +0200 | [diff] [blame] | 92 | /* set the device I'm talking to */ |
Frank Vibrans | 63e62b0 | 2011-02-14 18:38:14 +0000 | [diff] [blame] | 93 | outb(((device & 0x7f) << 1) | 0, smbus_io_base + SMBHSTADDR); |
| 94 | |
| 95 | byte = inb(smbus_io_base + SMBHSTCTRL); |
| 96 | byte &= 0xe3; /* Clear [4:2] */ |
| 97 | byte |= (1 << 2) | (1 << 6); /* Byte data read/write command, start the command */ |
| 98 | outb(byte, smbus_io_base + SMBHSTCTRL); |
| 99 | |
| 100 | /* poll for transaction completion */ |
| 101 | if (smbus_wait_until_done(smbus_io_base) < 0) { |
| 102 | return -3; /* timeout or error */ |
| 103 | } |
| 104 | |
Kerry She | feed329 | 2011-08-18 18:03:44 +0800 | [diff] [blame] | 105 | printk(BIOS_DEBUG, "SB800 - Smbus.c - do_smbus_send_byte - End.\n"); |
Frank Vibrans | 63e62b0 | 2011-02-14 18:38:14 +0000 | [diff] [blame] | 106 | return 0; |
| 107 | } |
| 108 | |
| 109 | int do_smbus_read_byte(u32 smbus_io_base, u32 device, u32 address) |
| 110 | { |
| 111 | u8 byte; |
| 112 | |
| 113 | if (smbus_wait_until_ready(smbus_io_base) < 0) { |
Elyes HAOUAS | ba28e8d | 2016-08-31 19:22:16 +0200 | [diff] [blame] | 114 | printk(BIOS_DEBUG, "SB800 - Smbus.c - do_smbus_read_byte - smbus not ready.\n"); |
Frank Vibrans | 63e62b0 | 2011-02-14 18:38:14 +0000 | [diff] [blame] | 115 | return -2; /* not ready */ |
| 116 | } |
| 117 | |
Kerry She | feed329 | 2011-08-18 18:03:44 +0800 | [diff] [blame] | 118 | printk(BIOS_DEBUG, "SB800 - Smbus.c - do_smbus_read_byte - Start.\n"); |
Frank Vibrans | 63e62b0 | 2011-02-14 18:38:14 +0000 | [diff] [blame] | 119 | /* set the command/address... */ |
| 120 | outb(address & 0xff, smbus_io_base + SMBHSTCMD); |
| 121 | |
Jonathan Neuschäfer | 7090377 | 2017-09-23 21:39:02 +0200 | [diff] [blame] | 122 | /* set the device I'm talking to */ |
Frank Vibrans | 63e62b0 | 2011-02-14 18:38:14 +0000 | [diff] [blame] | 123 | outb(((device & 0x7f) << 1) | 1, smbus_io_base + SMBHSTADDR); |
| 124 | |
| 125 | byte = inb(smbus_io_base + SMBHSTCTRL); |
| 126 | byte &= 0xe3; /* Clear [4:2] */ |
| 127 | byte |= (1 << 3) | (1 << 6); /* Byte data read/write command, start the command */ |
| 128 | outb(byte, smbus_io_base + SMBHSTCTRL); |
| 129 | |
| 130 | /* poll for transaction completion */ |
| 131 | if (smbus_wait_until_done(smbus_io_base) < 0) { |
| 132 | return -3; /* timeout or error */ |
| 133 | } |
| 134 | |
| 135 | /* read results of transaction */ |
| 136 | byte = inb(smbus_io_base + SMBHSTDAT0); |
| 137 | |
Kerry She | feed329 | 2011-08-18 18:03:44 +0800 | [diff] [blame] | 138 | printk(BIOS_DEBUG, "SB800 - Smbus.c - do_smbus_read_byte - End.\n"); |
Frank Vibrans | 63e62b0 | 2011-02-14 18:38:14 +0000 | [diff] [blame] | 139 | return byte; |
| 140 | } |
| 141 | |
| 142 | int do_smbus_write_byte(u32 smbus_io_base, u32 device, u32 address, u8 val) |
| 143 | { |
| 144 | u8 byte; |
| 145 | |
| 146 | if (smbus_wait_until_ready(smbus_io_base) < 0) { |
Elyes HAOUAS | ba28e8d | 2016-08-31 19:22:16 +0200 | [diff] [blame] | 147 | printk(BIOS_DEBUG, "SB800 - Smbus.c - do_smbus_write_byte - smbus not ready.\n"); |
Frank Vibrans | 63e62b0 | 2011-02-14 18:38:14 +0000 | [diff] [blame] | 148 | return -2; /* not ready */ |
| 149 | } |
| 150 | |
Kerry She | feed329 | 2011-08-18 18:03:44 +0800 | [diff] [blame] | 151 | printk(BIOS_DEBUG, "SB800 - Smbus.c - do_smbus_write_byte - Start.\n"); |
Frank Vibrans | 63e62b0 | 2011-02-14 18:38:14 +0000 | [diff] [blame] | 152 | /* set the command/address... */ |
| 153 | outb(address & 0xff, smbus_io_base + SMBHSTCMD); |
| 154 | |
Jonathan Neuschäfer | 7090377 | 2017-09-23 21:39:02 +0200 | [diff] [blame] | 155 | /* set the device I'm talking to */ |
Frank Vibrans | 63e62b0 | 2011-02-14 18:38:14 +0000 | [diff] [blame] | 156 | outb(((device & 0x7f) << 1) | 0, smbus_io_base + SMBHSTADDR); |
| 157 | |
| 158 | /* output value */ |
| 159 | outb(val, smbus_io_base + SMBHSTDAT0); |
| 160 | |
| 161 | byte = inb(smbus_io_base + SMBHSTCTRL); |
| 162 | byte &= 0xe3; /* Clear [4:2] */ |
| 163 | byte |= (1 << 3) | (1 << 6); /* Byte data read/write command, start the command */ |
| 164 | outb(byte, smbus_io_base + SMBHSTCTRL); |
| 165 | |
| 166 | /* poll for transaction completion */ |
| 167 | if (smbus_wait_until_done(smbus_io_base) < 0) { |
| 168 | return -3; /* timeout or error */ |
| 169 | } |
| 170 | |
Kerry She | feed329 | 2011-08-18 18:03:44 +0800 | [diff] [blame] | 171 | printk(BIOS_DEBUG, "SB800 - Smbus.c - do_smbus_write_byte - End.\n"); |
Frank Vibrans | 63e62b0 | 2011-02-14 18:38:14 +0000 | [diff] [blame] | 172 | return 0; |
| 173 | } |
| 174 | |
| 175 | void alink_ab_indx(u32 reg_space, u32 reg_addr, u32 mask, u32 val) |
| 176 | { |
| 177 | u32 tmp; |
| 178 | |
Kerry She | feed329 | 2011-08-18 18:03:44 +0800 | [diff] [blame] | 179 | printk(BIOS_DEBUG, "SB800 - Smbus.c - alink_ab_indx - Start.\n"); |
Frank Vibrans | 63e62b0 | 2011-02-14 18:38:14 +0000 | [diff] [blame] | 180 | outl((reg_space & 0x7) << 29 | reg_addr, AB_INDX); |
| 181 | tmp = inl(AB_DATA); |
| 182 | /* rpr 4.2 |
| 183 | * For certain revisions of the chip, the ABCFG registers, |
| 184 | * with an address of 0x100NN (where 'N' is any hexadecimal |
| 185 | * number), require an extra programming step.*/ |
| 186 | outl(0, AB_INDX); |
| 187 | |
| 188 | tmp &= ~mask; |
| 189 | tmp |= val; |
| 190 | |
| 191 | /* printk(BIOS_DEBUG, "about write %x, index=%x", tmp, (reg_space&0x3)<<29 | reg_addr); */ |
Martin Roth | 3c3a50c | 2014-12-16 20:50:26 -0700 | [diff] [blame] | 192 | outl((reg_space & 0x7) << 29 | reg_addr, AB_INDX); /* probably we don't have to do it again. */ |
Frank Vibrans | 63e62b0 | 2011-02-14 18:38:14 +0000 | [diff] [blame] | 193 | outl(tmp, AB_DATA); |
| 194 | outl(0, AB_INDX); |
Kerry She | feed329 | 2011-08-18 18:03:44 +0800 | [diff] [blame] | 195 | printk(BIOS_DEBUG, "SB800 - Smbus.c - alink_ab_indx - End.\n"); |
Frank Vibrans | 63e62b0 | 2011-02-14 18:38:14 +0000 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | void alink_rc_indx(u32 reg_space, u32 reg_addr, u32 port, u32 mask, u32 val) |
| 199 | { |
| 200 | u32 tmp; |
| 201 | |
Kerry She | feed329 | 2011-08-18 18:03:44 +0800 | [diff] [blame] | 202 | printk(BIOS_DEBUG, "SB800 - Smbus.c - alink_rc_indx - Start.\n"); |
Frank Vibrans | 63e62b0 | 2011-02-14 18:38:14 +0000 | [diff] [blame] | 203 | outl((reg_space & 0x7) << 29 | (port & 3) << 24 | reg_addr, AB_INDX); |
| 204 | tmp = inl(AB_DATA); |
| 205 | /* rpr 4.2 |
| 206 | * For certain revisions of the chip, the ABCFG registers, |
| 207 | * with an address of 0x100NN (where 'N' is any hexadecimal |
| 208 | * number), require an extra programming step.*/ |
| 209 | outl(0, AB_INDX); |
| 210 | |
| 211 | tmp &= ~mask; |
| 212 | tmp |= val; |
| 213 | |
| 214 | //printk(BIOS_DEBUG, "about write %x, index=%x", tmp, (reg_space&0x3)<<29 | (port&3) << 24 | reg_addr); |
Martin Roth | 3c3a50c | 2014-12-16 20:50:26 -0700 | [diff] [blame] | 215 | outl((reg_space & 0x7) << 29 | (port & 3) << 24 | reg_addr, AB_INDX); /* probably we don't have to do it again. */ |
Frank Vibrans | 63e62b0 | 2011-02-14 18:38:14 +0000 | [diff] [blame] | 216 | outl(tmp, AB_DATA); |
| 217 | outl(0, AB_INDX); |
Kerry She | feed329 | 2011-08-18 18:03:44 +0800 | [diff] [blame] | 218 | printk(BIOS_DEBUG, "SB800 - Smbus.c - alink_rc_indx - End.\n"); |
Frank Vibrans | 63e62b0 | 2011-02-14 18:38:14 +0000 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | /* space = 0: AX_INDXC, AX_DATAC |
| 222 | * space = 1: AX_INDXP, AX_DATAP |
| 223 | */ |
Elyes HAOUAS | a342f39 | 2018-10-17 10:56:26 +0200 | [diff] [blame] | 224 | void alink_ax_indx(u32 space /*c or p? */, u32 axindc, u32 mask, u32 val) |
Frank Vibrans | 63e62b0 | 2011-02-14 18:38:14 +0000 | [diff] [blame] | 225 | { |
| 226 | u32 tmp; |
| 227 | |
Kerry She | feed329 | 2011-08-18 18:03:44 +0800 | [diff] [blame] | 228 | printk(BIOS_DEBUG, "SB800 - Smbus.c - alink_ax_indx - Start.\n"); |
Frank Vibrans | 63e62b0 | 2011-02-14 18:38:14 +0000 | [diff] [blame] | 229 | /* read axindc to tmp */ |
| 230 | outl(space << 29 | space << 3 | 0x30, AB_INDX); |
| 231 | outl(axindc, AB_DATA); |
| 232 | outl(0, AB_INDX); |
| 233 | outl(space << 29 | space << 3 | 0x34, AB_INDX); |
| 234 | tmp = inl(AB_DATA); |
| 235 | outl(0, AB_INDX); |
| 236 | |
| 237 | tmp &= ~mask; |
| 238 | tmp |= val; |
| 239 | |
| 240 | /* write tmp */ |
| 241 | outl(space << 29 | space << 3 | 0x30, AB_INDX); |
| 242 | outl(axindc, AB_DATA); |
| 243 | outl(0, AB_INDX); |
| 244 | outl(space << 29 | space << 3 | 0x34, AB_INDX); |
| 245 | outl(tmp, AB_DATA); |
| 246 | outl(0, AB_INDX); |
Kerry She | feed329 | 2011-08-18 18:03:44 +0800 | [diff] [blame] | 247 | printk(BIOS_DEBUG, "SB800 - Smbus.c - alink_ax_indx - End.\n"); |
Frank Vibrans | 63e62b0 | 2011-02-14 18:38:14 +0000 | [diff] [blame] | 248 | } |