Uwe Hermann | 97f56a4 | 2008-10-21 15:07:53 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
| 4 | * Copyright (C) 2008 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. |
Uwe Hermann | 97f56a4 | 2008-10-21 15:07:53 +0000 | [diff] [blame] | 14 | */ |
| 15 | |
stepan | 836ae29 | 2010-12-08 05:42:47 +0000 | [diff] [blame] | 16 | #include "smbus.h" |
Michael Xie | 7586cef | 2008-09-22 13:11:39 +0000 | [diff] [blame] | 17 | |
| 18 | static inline void smbus_delay(void) |
| 19 | { |
| 20 | outb(0x80, 0x80); |
| 21 | } |
| 22 | |
| 23 | static int smbus_wait_until_ready(u32 smbus_io_base) |
| 24 | { |
Joe Bao | 164463c | 2008-12-01 19:37:21 +0000 | [diff] [blame] | 25 | u32 loops; |
Michael Xie | 7586cef | 2008-09-22 13:11:39 +0000 | [diff] [blame] | 26 | loops = SMBUS_TIMEOUT; |
| 27 | do { |
| 28 | u8 val; |
| 29 | val = inb(smbus_io_base + SMBHSTSTAT); |
| 30 | val &= 0x1f; |
| 31 | if (val == 0) { /* ready now */ |
| 32 | return 0; |
| 33 | } |
| 34 | outb(val, smbus_io_base + SMBHSTSTAT); |
| 35 | } while (--loops); |
| 36 | return -2; /* time out */ |
| 37 | } |
| 38 | |
| 39 | static int smbus_wait_until_done(u32 smbus_io_base) |
| 40 | { |
Joe Bao | 164463c | 2008-12-01 19:37:21 +0000 | [diff] [blame] | 41 | u32 loops; |
Michael Xie | 7586cef | 2008-09-22 13:11:39 +0000 | [diff] [blame] | 42 | loops = SMBUS_TIMEOUT; |
| 43 | do { |
| 44 | u8 val; |
| 45 | |
| 46 | val = inb(smbus_io_base + SMBHSTSTAT); |
| 47 | val &= 0x1f; /* mask off reserved bits */ |
| 48 | if (val & 0x1c) { |
| 49 | return -5; /* error */ |
| 50 | } |
| 51 | if (val == 0x02) { |
| 52 | outb(val, smbus_io_base + SMBHSTSTAT); /* clear status */ |
| 53 | return 0; |
| 54 | } |
| 55 | } while (--loops); |
| 56 | return -3; /* timeout */ |
| 57 | } |
| 58 | |
Stefan Reinauer | 5e33e82 | 2010-07-07 21:59:06 +0000 | [diff] [blame] | 59 | int do_smbus_recv_byte(u32 smbus_io_base, u32 device) |
Michael Xie | 7586cef | 2008-09-22 13:11:39 +0000 | [diff] [blame] | 60 | { |
| 61 | u8 byte; |
| 62 | |
| 63 | if (smbus_wait_until_ready(smbus_io_base) < 0) { |
| 64 | return -2; /* not ready */ |
| 65 | } |
| 66 | |
| 67 | /* set the device I'm talking too */ |
| 68 | outb(((device & 0x7f) << 1) | 1, smbus_io_base + SMBHSTADDR); |
| 69 | |
| 70 | byte = inb(smbus_io_base + SMBHSTCTRL); |
| 71 | byte &= 0xe3; /* Clear [4:2] */ |
| 72 | byte |= (1 << 2) | (1 << 6); /* Byte data read/write command, start the command */ |
| 73 | outb(byte, smbus_io_base + SMBHSTCTRL); |
| 74 | |
| 75 | /* poll for transaction completion */ |
| 76 | if (smbus_wait_until_done(smbus_io_base) < 0) { |
| 77 | return -3; /* timeout or error */ |
| 78 | } |
| 79 | |
| 80 | /* read results of transaction */ |
| 81 | byte = inb(smbus_io_base + SMBHSTCMD); |
| 82 | |
| 83 | return byte; |
| 84 | } |
| 85 | |
Stefan Reinauer | 5e33e82 | 2010-07-07 21:59:06 +0000 | [diff] [blame] | 86 | int do_smbus_send_byte(u32 smbus_io_base, u32 device, u8 val) |
Michael Xie | 7586cef | 2008-09-22 13:11:39 +0000 | [diff] [blame] | 87 | { |
| 88 | u8 byte; |
| 89 | |
| 90 | if (smbus_wait_until_ready(smbus_io_base) < 0) { |
| 91 | return -2; /* not ready */ |
| 92 | } |
| 93 | |
| 94 | /* set the command... */ |
| 95 | outb(val, smbus_io_base + SMBHSTCMD); |
| 96 | |
| 97 | /* set the device I'm talking too */ |
| 98 | outb(((device & 0x7f) << 1) | 0, smbus_io_base + SMBHSTADDR); |
| 99 | |
| 100 | byte = inb(smbus_io_base + SMBHSTCTRL); |
| 101 | byte &= 0xe3; /* Clear [4:2] */ |
| 102 | byte |= (1 << 2) | (1 << 6); /* Byte data read/write command, start the command */ |
| 103 | outb(byte, smbus_io_base + SMBHSTCTRL); |
| 104 | |
| 105 | /* poll for transaction completion */ |
| 106 | if (smbus_wait_until_done(smbus_io_base) < 0) { |
| 107 | return -3; /* timeout or error */ |
| 108 | } |
| 109 | |
| 110 | return 0; |
| 111 | } |
| 112 | |
Uwe Hermann | ff492b1 | 2010-09-24 23:37:25 +0000 | [diff] [blame] | 113 | int do_smbus_read_byte(u32 smbus_io_base, u32 device, u32 address) |
Michael Xie | 7586cef | 2008-09-22 13:11:39 +0000 | [diff] [blame] | 114 | { |
| 115 | u8 byte; |
| 116 | |
| 117 | if (smbus_wait_until_ready(smbus_io_base) < 0) { |
| 118 | return -2; /* not ready */ |
| 119 | } |
| 120 | |
| 121 | /* set the command/address... */ |
| 122 | outb(address & 0xff, smbus_io_base + SMBHSTCMD); |
| 123 | |
| 124 | /* set the device I'm talking too */ |
| 125 | outb(((device & 0x7f) << 1) | 1, smbus_io_base + SMBHSTADDR); |
| 126 | |
| 127 | byte = inb(smbus_io_base + SMBHSTCTRL); |
| 128 | byte &= 0xe3; /* Clear [4:2] */ |
| 129 | byte |= (1 << 3) | (1 << 6); /* Byte data read/write command, start the command */ |
| 130 | outb(byte, smbus_io_base + SMBHSTCTRL); |
| 131 | |
| 132 | /* poll for transaction completion */ |
| 133 | if (smbus_wait_until_done(smbus_io_base) < 0) { |
| 134 | return -3; /* timeout or error */ |
| 135 | } |
| 136 | |
| 137 | /* read results of transaction */ |
| 138 | byte = inb(smbus_io_base + SMBHSTDAT0); |
| 139 | |
| 140 | return byte; |
| 141 | } |
| 142 | |
Uwe Hermann | ff492b1 | 2010-09-24 23:37:25 +0000 | [diff] [blame] | 143 | int do_smbus_write_byte(u32 smbus_io_base, u32 device, u32 address, u8 val) |
Michael Xie | 7586cef | 2008-09-22 13:11:39 +0000 | [diff] [blame] | 144 | { |
| 145 | u8 byte; |
| 146 | |
| 147 | if (smbus_wait_until_ready(smbus_io_base) < 0) { |
| 148 | return -2; /* not ready */ |
| 149 | } |
| 150 | |
| 151 | /* set the command/address... */ |
| 152 | outb(address & 0xff, smbus_io_base + SMBHSTCMD); |
| 153 | |
| 154 | /* set the device I'm talking too */ |
| 155 | outb(((device & 0x7f) << 1) | 0, smbus_io_base + SMBHSTADDR); |
| 156 | |
| 157 | /* output value */ |
| 158 | outb(val, smbus_io_base + SMBHSTDAT0); |
| 159 | |
| 160 | byte = inb(smbus_io_base + SMBHSTCTRL); |
| 161 | byte &= 0xe3; /* Clear [4:2] */ |
| 162 | byte |= (1 << 3) | (1 << 6); /* Byte data read/write command, start the command */ |
| 163 | outb(byte, smbus_io_base + SMBHSTCTRL); |
| 164 | |
| 165 | /* poll for transaction completion */ |
| 166 | if (smbus_wait_until_done(smbus_io_base) < 0) { |
| 167 | return -3; /* timeout or error */ |
| 168 | } |
| 169 | |
| 170 | return 0; |
| 171 | } |
| 172 | |
Uwe Hermann | ff492b1 | 2010-09-24 23:37:25 +0000 | [diff] [blame] | 173 | static void alink_ab_indx(u32 reg_space, u32 reg_addr, u32 mask, u32 val) |
Michael Xie | 7586cef | 2008-09-22 13:11:39 +0000 | [diff] [blame] | 174 | { |
Joe Bao | 164463c | 2008-12-01 19:37:21 +0000 | [diff] [blame] | 175 | u32 tmp; |
| 176 | |
Michael Xie | 7586cef | 2008-09-22 13:11:39 +0000 | [diff] [blame] | 177 | outl((reg_space & 0x3) << 30 | reg_addr, AB_INDX); |
| 178 | tmp = inl(AB_DATA); |
| 179 | |
| 180 | tmp &= ~mask; |
| 181 | tmp |= val; |
| 182 | |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 183 | /* printk(BIOS_DEBUG, "about write %x, index=%x", tmp, (reg_space&0x3)<<30 | reg_addr); */ |
Martin Roth | dcf253c | 2014-12-16 20:51:31 -0700 | [diff] [blame] | 184 | outl((reg_space & 0x3) << 30 | reg_addr, AB_INDX); /* probably we don't have to do it again. */ |
Michael Xie | 7586cef | 2008-09-22 13:11:39 +0000 | [diff] [blame] | 185 | outl(tmp, AB_DATA); |
| 186 | } |
| 187 | |
| 188 | /* space = 0: AX_INDXC, AX_DATAC |
| 189 | * space = 1: AX_INDXP, AX_DATAP |
| 190 | */ |
Joe Bao | 164463c | 2008-12-01 19:37:21 +0000 | [diff] [blame] | 191 | static void alink_ax_indx(u32 space /*c or p? */ , u32 axindc, |
| 192 | u32 mask, u32 val) |
Michael Xie | 7586cef | 2008-09-22 13:11:39 +0000 | [diff] [blame] | 193 | { |
Joe Bao | 164463c | 2008-12-01 19:37:21 +0000 | [diff] [blame] | 194 | u32 tmp; |
Michael Xie | 7586cef | 2008-09-22 13:11:39 +0000 | [diff] [blame] | 195 | |
| 196 | /* read axindc to tmp */ |
| 197 | outl(space << 30 | space << 3 | 0x30, AB_INDX); |
| 198 | outl(axindc, AB_DATA); |
| 199 | outl(space << 30 | space << 3 | 0x34, AB_INDX); |
| 200 | tmp = inl(AB_DATA); |
| 201 | |
| 202 | tmp &= ~mask; |
| 203 | tmp |= val; |
| 204 | |
| 205 | /* write tmp */ |
| 206 | outl(space << 30 | space << 3 | 0x30, AB_INDX); |
| 207 | outl(axindc, AB_DATA); |
| 208 | outl(space << 30 | space << 3 | 0x34, AB_INDX); |
| 209 | outl(tmp, AB_DATA); |
| 210 | } |