Frank Vibrans | 63e62b0 | 2011-02-14 18:38:14 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
| 4 | * Copyright (C) 2011 Advanced Micro Devices, Inc. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; version 2 of the License. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
Paul Menzel | a46a712 | 2013-02-23 18:37:27 +0100 | [diff] [blame] | 17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
Frank Vibrans | 63e62b0 | 2011-02-14 18:38:14 +0000 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | |
| 21 | #include <arch/io.h> |
| 22 | #include "smbus.h" |
Kerry She | feed329 | 2011-08-18 18:03:44 +0800 | [diff] [blame] | 23 | #include <console/console.h> /* printk */ |
Frank Vibrans | 63e62b0 | 2011-02-14 18:38:14 +0000 | [diff] [blame] | 24 | |
Frank Vibrans | 63e62b0 | 2011-02-14 18:38:14 +0000 | [diff] [blame] | 25 | static int smbus_wait_until_ready(u32 smbus_io_base) |
| 26 | { |
| 27 | u32 loops; |
| 28 | |
| 29 | loops = SMBUS_TIMEOUT; |
| 30 | do { |
| 31 | u8 val; |
| 32 | val = inb(smbus_io_base + SMBHSTSTAT); |
| 33 | val &= 0x1f; |
| 34 | if (val == 0) { /* ready now */ |
| 35 | return 0; |
| 36 | } |
| 37 | outb(val, smbus_io_base + SMBHSTSTAT); |
| 38 | } while (--loops); |
| 39 | |
| 40 | return -2; /* time out */ |
| 41 | } |
| 42 | |
| 43 | static int smbus_wait_until_done(u32 smbus_io_base) |
| 44 | { |
| 45 | u32 loops; |
| 46 | |
| 47 | loops = SMBUS_TIMEOUT; |
| 48 | do { |
| 49 | u8 val; |
| 50 | |
| 51 | val = inb(smbus_io_base + SMBHSTSTAT); |
| 52 | val &= 0x1f; /* mask off reserved bits */ |
| 53 | if (val & 0x1c) { |
| 54 | return -5; /* error */ |
| 55 | } |
| 56 | if (val == 0x02) { |
| 57 | outb(val, smbus_io_base + SMBHSTSTAT); /* clear status */ |
| 58 | return 0; |
| 59 | } |
| 60 | } while (--loops); |
| 61 | |
| 62 | return -3; /* timeout */ |
| 63 | } |
| 64 | |
| 65 | int do_smbus_recv_byte(u32 smbus_io_base, u32 device) |
| 66 | { |
| 67 | u8 byte; |
| 68 | |
| 69 | if (smbus_wait_until_ready(smbus_io_base) < 0) { |
Kerry She | feed329 | 2011-08-18 18:03:44 +0800 | [diff] [blame] | 70 | 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] | 71 | return -2; /* not ready */ |
| 72 | } |
| 73 | |
Kerry She | feed329 | 2011-08-18 18:03:44 +0800 | [diff] [blame] | 74 | printk(BIOS_DEBUG, "SB800 - Smbus.c - do_smbus_recv_byte - Start.\n"); |
Frank Vibrans | 63e62b0 | 2011-02-14 18:38:14 +0000 | [diff] [blame] | 75 | /* set the device I'm talking too */ |
| 76 | outb(((device & 0x7f) << 1) | 1, smbus_io_base + SMBHSTADDR); |
| 77 | |
| 78 | byte = inb(smbus_io_base + SMBHSTCTRL); |
| 79 | byte &= 0xe3; /* Clear [4:2] */ |
| 80 | byte |= (1 << 2) | (1 << 6); /* Byte data read/write command, start the command */ |
| 81 | outb(byte, smbus_io_base + SMBHSTCTRL); |
| 82 | |
| 83 | /* poll for transaction completion */ |
| 84 | if (smbus_wait_until_done(smbus_io_base) < 0) { |
| 85 | return -3; /* timeout or error */ |
| 86 | } |
| 87 | |
| 88 | /* read results of transaction */ |
| 89 | byte = inb(smbus_io_base + SMBHSTCMD); |
| 90 | |
Kerry She | feed329 | 2011-08-18 18:03:44 +0800 | [diff] [blame] | 91 | printk(BIOS_DEBUG, "SB800 - Smbus.c - do_smbus_recv_byte - End.\n"); |
Frank Vibrans | 63e62b0 | 2011-02-14 18:38:14 +0000 | [diff] [blame] | 92 | return byte; |
| 93 | } |
| 94 | |
| 95 | int do_smbus_send_byte(u32 smbus_io_base, u32 device, u8 val) |
| 96 | { |
| 97 | u8 byte; |
| 98 | |
| 99 | if (smbus_wait_until_ready(smbus_io_base) < 0) { |
Kerry She | feed329 | 2011-08-18 18:03:44 +0800 | [diff] [blame] | 100 | 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] | 101 | return -2; /* not ready */ |
| 102 | } |
| 103 | |
Kerry She | feed329 | 2011-08-18 18:03:44 +0800 | [diff] [blame] | 104 | printk(BIOS_DEBUG, "SB800 - Smbus.c - do_smbus_send_byte - Start.\n"); |
Frank Vibrans | 63e62b0 | 2011-02-14 18:38:14 +0000 | [diff] [blame] | 105 | /* set the command... */ |
| 106 | outb(val, smbus_io_base + SMBHSTCMD); |
| 107 | |
| 108 | /* set the device I'm talking too */ |
| 109 | outb(((device & 0x7f) << 1) | 0, smbus_io_base + SMBHSTADDR); |
| 110 | |
| 111 | byte = inb(smbus_io_base + SMBHSTCTRL); |
| 112 | byte &= 0xe3; /* Clear [4:2] */ |
| 113 | byte |= (1 << 2) | (1 << 6); /* Byte data read/write command, start the command */ |
| 114 | outb(byte, smbus_io_base + SMBHSTCTRL); |
| 115 | |
| 116 | /* poll for transaction completion */ |
| 117 | if (smbus_wait_until_done(smbus_io_base) < 0) { |
| 118 | return -3; /* timeout or error */ |
| 119 | } |
| 120 | |
Kerry She | feed329 | 2011-08-18 18:03:44 +0800 | [diff] [blame] | 121 | printk(BIOS_DEBUG, "SB800 - Smbus.c - do_smbus_send_byte - End.\n"); |
Frank Vibrans | 63e62b0 | 2011-02-14 18:38:14 +0000 | [diff] [blame] | 122 | return 0; |
| 123 | } |
| 124 | |
| 125 | int do_smbus_read_byte(u32 smbus_io_base, u32 device, u32 address) |
| 126 | { |
| 127 | u8 byte; |
| 128 | |
| 129 | if (smbus_wait_until_ready(smbus_io_base) < 0) { |
Kerry She | feed329 | 2011-08-18 18:03:44 +0800 | [diff] [blame] | 130 | 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] | 131 | return -2; /* not ready */ |
| 132 | } |
| 133 | |
Kerry She | feed329 | 2011-08-18 18:03:44 +0800 | [diff] [blame] | 134 | printk(BIOS_DEBUG, "SB800 - Smbus.c - do_smbus_read_byte - Start.\n"); |
Frank Vibrans | 63e62b0 | 2011-02-14 18:38:14 +0000 | [diff] [blame] | 135 | /* set the command/address... */ |
| 136 | outb(address & 0xff, smbus_io_base + SMBHSTCMD); |
| 137 | |
| 138 | /* set the device I'm talking too */ |
| 139 | outb(((device & 0x7f) << 1) | 1, smbus_io_base + SMBHSTADDR); |
| 140 | |
| 141 | byte = inb(smbus_io_base + SMBHSTCTRL); |
| 142 | byte &= 0xe3; /* Clear [4:2] */ |
| 143 | byte |= (1 << 3) | (1 << 6); /* Byte data read/write command, start the command */ |
| 144 | outb(byte, smbus_io_base + SMBHSTCTRL); |
| 145 | |
| 146 | /* poll for transaction completion */ |
| 147 | if (smbus_wait_until_done(smbus_io_base) < 0) { |
| 148 | return -3; /* timeout or error */ |
| 149 | } |
| 150 | |
| 151 | /* read results of transaction */ |
| 152 | byte = inb(smbus_io_base + SMBHSTDAT0); |
| 153 | |
Kerry She | feed329 | 2011-08-18 18:03:44 +0800 | [diff] [blame] | 154 | printk(BIOS_DEBUG, "SB800 - Smbus.c - do_smbus_read_byte - End.\n"); |
Frank Vibrans | 63e62b0 | 2011-02-14 18:38:14 +0000 | [diff] [blame] | 155 | return byte; |
| 156 | } |
| 157 | |
| 158 | int do_smbus_write_byte(u32 smbus_io_base, u32 device, u32 address, u8 val) |
| 159 | { |
| 160 | u8 byte; |
| 161 | |
| 162 | if (smbus_wait_until_ready(smbus_io_base) < 0) { |
Kerry She | feed329 | 2011-08-18 18:03:44 +0800 | [diff] [blame] | 163 | 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] | 164 | return -2; /* not ready */ |
| 165 | } |
| 166 | |
Kerry She | feed329 | 2011-08-18 18:03:44 +0800 | [diff] [blame] | 167 | printk(BIOS_DEBUG, "SB800 - Smbus.c - do_smbus_write_byte - Start.\n"); |
Frank Vibrans | 63e62b0 | 2011-02-14 18:38:14 +0000 | [diff] [blame] | 168 | /* set the command/address... */ |
| 169 | outb(address & 0xff, smbus_io_base + SMBHSTCMD); |
| 170 | |
| 171 | /* set the device I'm talking too */ |
| 172 | outb(((device & 0x7f) << 1) | 0, smbus_io_base + SMBHSTADDR); |
| 173 | |
| 174 | /* output value */ |
| 175 | outb(val, smbus_io_base + SMBHSTDAT0); |
| 176 | |
| 177 | byte = inb(smbus_io_base + SMBHSTCTRL); |
| 178 | byte &= 0xe3; /* Clear [4:2] */ |
| 179 | byte |= (1 << 3) | (1 << 6); /* Byte data read/write command, start the command */ |
| 180 | outb(byte, smbus_io_base + SMBHSTCTRL); |
| 181 | |
| 182 | /* poll for transaction completion */ |
| 183 | if (smbus_wait_until_done(smbus_io_base) < 0) { |
| 184 | return -3; /* timeout or error */ |
| 185 | } |
| 186 | |
Kerry She | feed329 | 2011-08-18 18:03:44 +0800 | [diff] [blame] | 187 | printk(BIOS_DEBUG, "SB800 - Smbus.c - do_smbus_write_byte - End.\n"); |
Frank Vibrans | 63e62b0 | 2011-02-14 18:38:14 +0000 | [diff] [blame] | 188 | return 0; |
| 189 | } |
| 190 | |
| 191 | void alink_ab_indx(u32 reg_space, u32 reg_addr, u32 mask, u32 val) |
| 192 | { |
| 193 | u32 tmp; |
| 194 | |
Kerry She | feed329 | 2011-08-18 18:03:44 +0800 | [diff] [blame] | 195 | printk(BIOS_DEBUG, "SB800 - Smbus.c - alink_ab_indx - Start.\n"); |
Frank Vibrans | 63e62b0 | 2011-02-14 18:38:14 +0000 | [diff] [blame] | 196 | outl((reg_space & 0x7) << 29 | reg_addr, AB_INDX); |
| 197 | tmp = inl(AB_DATA); |
| 198 | /* rpr 4.2 |
| 199 | * For certain revisions of the chip, the ABCFG registers, |
| 200 | * with an address of 0x100NN (where 'N' is any hexadecimal |
| 201 | * number), require an extra programming step.*/ |
| 202 | outl(0, AB_INDX); |
| 203 | |
| 204 | tmp &= ~mask; |
| 205 | tmp |= val; |
| 206 | |
| 207 | /* 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^] | 208 | 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] | 209 | outl(tmp, AB_DATA); |
| 210 | outl(0, AB_INDX); |
Kerry She | feed329 | 2011-08-18 18:03:44 +0800 | [diff] [blame] | 211 | printk(BIOS_DEBUG, "SB800 - Smbus.c - alink_ab_indx - End.\n"); |
Frank Vibrans | 63e62b0 | 2011-02-14 18:38:14 +0000 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | void alink_rc_indx(u32 reg_space, u32 reg_addr, u32 port, u32 mask, u32 val) |
| 215 | { |
| 216 | u32 tmp; |
| 217 | |
Kerry She | feed329 | 2011-08-18 18:03:44 +0800 | [diff] [blame] | 218 | printk(BIOS_DEBUG, "SB800 - Smbus.c - alink_rc_indx - Start.\n"); |
Frank Vibrans | 63e62b0 | 2011-02-14 18:38:14 +0000 | [diff] [blame] | 219 | outl((reg_space & 0x7) << 29 | (port & 3) << 24 | reg_addr, AB_INDX); |
| 220 | tmp = inl(AB_DATA); |
| 221 | /* rpr 4.2 |
| 222 | * For certain revisions of the chip, the ABCFG registers, |
| 223 | * with an address of 0x100NN (where 'N' is any hexadecimal |
| 224 | * number), require an extra programming step.*/ |
| 225 | outl(0, AB_INDX); |
| 226 | |
| 227 | tmp &= ~mask; |
| 228 | tmp |= val; |
| 229 | |
| 230 | //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^] | 231 | 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] | 232 | outl(tmp, AB_DATA); |
| 233 | outl(0, AB_INDX); |
Kerry She | feed329 | 2011-08-18 18:03:44 +0800 | [diff] [blame] | 234 | printk(BIOS_DEBUG, "SB800 - Smbus.c - alink_rc_indx - End.\n"); |
Frank Vibrans | 63e62b0 | 2011-02-14 18:38:14 +0000 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | /* space = 0: AX_INDXC, AX_DATAC |
| 238 | * space = 1: AX_INDXP, AX_DATAP |
| 239 | */ |
| 240 | void alink_ax_indx(u32 space /*c or p? */ , u32 axindc, u32 mask, u32 val) |
| 241 | { |
| 242 | u32 tmp; |
| 243 | |
Kerry She | feed329 | 2011-08-18 18:03:44 +0800 | [diff] [blame] | 244 | printk(BIOS_DEBUG, "SB800 - Smbus.c - alink_ax_indx - Start.\n"); |
Frank Vibrans | 63e62b0 | 2011-02-14 18:38:14 +0000 | [diff] [blame] | 245 | /* read axindc to tmp */ |
| 246 | outl(space << 29 | space << 3 | 0x30, AB_INDX); |
| 247 | outl(axindc, AB_DATA); |
| 248 | outl(0, AB_INDX); |
| 249 | outl(space << 29 | space << 3 | 0x34, AB_INDX); |
| 250 | tmp = inl(AB_DATA); |
| 251 | outl(0, AB_INDX); |
| 252 | |
| 253 | tmp &= ~mask; |
| 254 | tmp |= val; |
| 255 | |
| 256 | /* write tmp */ |
| 257 | outl(space << 29 | space << 3 | 0x30, AB_INDX); |
| 258 | outl(axindc, AB_DATA); |
| 259 | outl(0, AB_INDX); |
| 260 | outl(space << 29 | space << 3 | 0x34, AB_INDX); |
| 261 | outl(tmp, AB_DATA); |
| 262 | outl(0, AB_INDX); |
Kerry She | feed329 | 2011-08-18 18:03:44 +0800 | [diff] [blame] | 263 | printk(BIOS_DEBUG, "SB800 - Smbus.c - alink_ax_indx - End.\n"); |
Frank Vibrans | 63e62b0 | 2011-02-14 18:38:14 +0000 | [diff] [blame] | 264 | } |