Ben Gardner | 99c3adb | 2006-01-18 22:41:50 +0100 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | Copyright (c) 2001,2002 Christer Weinigel <wingel@nano-system.com> |
| 3 | |
| 4 | National Semiconductor SCx200 ACCESS.bus support |
Ben Gardner | 16ffc5c | 2006-01-18 22:48:26 +0100 | [diff] [blame] | 5 | Also supports the AMD CS5535 and AMD CS5536 |
Ben Gardner | 99c3adb | 2006-01-18 22:41:50 +0100 | [diff] [blame] | 6 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | Based on i2c-keywest.c which is: |
| 8 | Copyright (c) 2001 Benjamin Herrenschmidt <benh@kernel.crashing.org> |
| 9 | Copyright (c) 2000 Philip Edelbrock <phil@stimpy.netroedge.com> |
Ben Gardner | 99c3adb | 2006-01-18 22:41:50 +0100 | [diff] [blame] | 10 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | This program is free software; you can redistribute it and/or |
| 12 | modify it under the terms of the GNU General Public License as |
| 13 | published by the Free Software Foundation; either version 2 of the |
| 14 | License, or (at your option) any later version. |
Ben Gardner | 99c3adb | 2006-01-18 22:41:50 +0100 | [diff] [blame] | 15 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | This program is distributed in the hope that it will be useful, |
| 17 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 19 | General Public License for more details. |
Ben Gardner | 99c3adb | 2006-01-18 22:41:50 +0100 | [diff] [blame] | 20 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | You should have received a copy of the GNU General Public License |
| 22 | along with this program; if not, write to the Free Software |
| 23 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | */ |
| 25 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include <linux/module.h> |
| 27 | #include <linux/errno.h> |
| 28 | #include <linux/kernel.h> |
| 29 | #include <linux/init.h> |
| 30 | #include <linux/i2c.h> |
| 31 | #include <linux/smp_lock.h> |
| 32 | #include <linux/pci.h> |
| 33 | #include <linux/delay.h> |
| 34 | #include <asm/io.h> |
Ben Gardner | 16ffc5c | 2006-01-18 22:48:26 +0100 | [diff] [blame] | 35 | #include <asm/msr.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | |
| 37 | #include <linux/scx200.h> |
| 38 | |
| 39 | #define NAME "scx200_acb" |
| 40 | |
| 41 | MODULE_AUTHOR("Christer Weinigel <wingel@nano-system.com>"); |
| 42 | MODULE_DESCRIPTION("NatSemi SCx200 ACCESS.bus Driver"); |
| 43 | MODULE_LICENSE("GPL"); |
| 44 | |
| 45 | #define MAX_DEVICES 4 |
| 46 | static int base[MAX_DEVICES] = { 0x820, 0x840 }; |
| 47 | module_param_array(base, int, NULL, 0); |
| 48 | MODULE_PARM_DESC(base, "Base addresses for the ACCESS.bus controllers"); |
| 49 | |
Ben Gardner | f933ff5 | 2006-01-18 22:52:06 +0100 | [diff] [blame] | 50 | #define POLL_TIMEOUT (HZ/5) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | |
| 52 | enum scx200_acb_state { |
| 53 | state_idle, |
| 54 | state_address, |
| 55 | state_command, |
| 56 | state_repeat_start, |
| 57 | state_quick, |
| 58 | state_read, |
| 59 | state_write, |
| 60 | }; |
| 61 | |
| 62 | static const char *scx200_acb_state_name[] = { |
| 63 | "idle", |
| 64 | "address", |
| 65 | "command", |
| 66 | "repeat_start", |
| 67 | "quick", |
| 68 | "read", |
| 69 | "write", |
| 70 | }; |
| 71 | |
| 72 | /* Physical interface */ |
Ben Gardner | 99c3adb | 2006-01-18 22:41:50 +0100 | [diff] [blame] | 73 | struct scx200_acb_iface { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | struct scx200_acb_iface *next; |
| 75 | struct i2c_adapter adapter; |
| 76 | unsigned base; |
| 77 | struct semaphore sem; |
| 78 | |
| 79 | /* State machine data */ |
| 80 | enum scx200_acb_state state; |
| 81 | int result; |
| 82 | u8 address_byte; |
| 83 | u8 command; |
| 84 | u8 *ptr; |
| 85 | char needs_reset; |
| 86 | unsigned len; |
| 87 | }; |
| 88 | |
| 89 | /* Register Definitions */ |
| 90 | #define ACBSDA (iface->base + 0) |
| 91 | #define ACBST (iface->base + 1) |
| 92 | #define ACBST_SDAST 0x40 /* SDA Status */ |
Ben Gardner | 99c3adb | 2006-01-18 22:41:50 +0100 | [diff] [blame] | 93 | #define ACBST_BER 0x20 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | #define ACBST_NEGACK 0x10 /* Negative Acknowledge */ |
| 95 | #define ACBST_STASTR 0x08 /* Stall After Start */ |
| 96 | #define ACBST_MASTER 0x02 |
| 97 | #define ACBCST (iface->base + 2) |
| 98 | #define ACBCST_BB 0x02 |
| 99 | #define ACBCTL1 (iface->base + 3) |
| 100 | #define ACBCTL1_STASTRE 0x80 |
| 101 | #define ACBCTL1_NMINTE 0x40 |
Ben Gardner | 99c3adb | 2006-01-18 22:41:50 +0100 | [diff] [blame] | 102 | #define ACBCTL1_ACK 0x10 |
| 103 | #define ACBCTL1_STOP 0x02 |
| 104 | #define ACBCTL1_START 0x01 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | #define ACBADDR (iface->base + 4) |
| 106 | #define ACBCTL2 (iface->base + 5) |
| 107 | #define ACBCTL2_ENABLE 0x01 |
| 108 | |
| 109 | /************************************************************************/ |
| 110 | |
| 111 | static void scx200_acb_machine(struct scx200_acb_iface *iface, u8 status) |
| 112 | { |
| 113 | const char *errmsg; |
| 114 | |
Ben Gardner | ef4d927 | 2006-01-18 22:43:10 +0100 | [diff] [blame] | 115 | dev_dbg(&iface->adapter.dev, "state %s, status = 0x%02x\n", |
| 116 | scx200_acb_state_name[iface->state], status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | |
| 118 | if (status & ACBST_BER) { |
| 119 | errmsg = "bus error"; |
| 120 | goto error; |
| 121 | } |
| 122 | if (!(status & ACBST_MASTER)) { |
| 123 | errmsg = "not master"; |
| 124 | goto error; |
| 125 | } |
Ben Gardner | 9b7b6d3 | 2006-01-18 22:44:04 +0100 | [diff] [blame] | 126 | if (status & ACBST_NEGACK) { |
| 127 | dev_dbg(&iface->adapter.dev, "negative ack in state %s\n", |
| 128 | scx200_acb_state_name[iface->state]); |
| 129 | |
| 130 | iface->state = state_idle; |
| 131 | iface->result = -ENXIO; |
| 132 | |
| 133 | outb(inb(ACBCTL1) | ACBCTL1_STOP, ACBCTL1); |
| 134 | outb(ACBST_STASTR | ACBST_NEGACK, ACBST); |
| 135 | return; |
| 136 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | |
| 138 | switch (iface->state) { |
| 139 | case state_idle: |
| 140 | dev_warn(&iface->adapter.dev, "interrupt in idle state\n"); |
| 141 | break; |
| 142 | |
| 143 | case state_address: |
| 144 | /* Do a pointer write first */ |
| 145 | outb(iface->address_byte & ~1, ACBSDA); |
| 146 | |
| 147 | iface->state = state_command; |
| 148 | break; |
| 149 | |
| 150 | case state_command: |
| 151 | outb(iface->command, ACBSDA); |
| 152 | |
| 153 | if (iface->address_byte & 1) |
| 154 | iface->state = state_repeat_start; |
| 155 | else |
| 156 | iface->state = state_write; |
| 157 | break; |
| 158 | |
| 159 | case state_repeat_start: |
| 160 | outb(inb(ACBCTL1) | ACBCTL1_START, ACBCTL1); |
| 161 | /* fallthrough */ |
Ben Gardner | 99c3adb | 2006-01-18 22:41:50 +0100 | [diff] [blame] | 162 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | case state_quick: |
| 164 | if (iface->address_byte & 1) { |
Ben Gardner | 99c3adb | 2006-01-18 22:41:50 +0100 | [diff] [blame] | 165 | if (iface->len == 1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | outb(inb(ACBCTL1) | ACBCTL1_ACK, ACBCTL1); |
| 167 | else |
| 168 | outb(inb(ACBCTL1) & ~ACBCTL1_ACK, ACBCTL1); |
| 169 | outb(iface->address_byte, ACBSDA); |
| 170 | |
| 171 | iface->state = state_read; |
| 172 | } else { |
| 173 | outb(iface->address_byte, ACBSDA); |
| 174 | |
| 175 | iface->state = state_write; |
| 176 | } |
| 177 | break; |
| 178 | |
| 179 | case state_read: |
| 180 | /* Set ACK if receiving the last byte */ |
| 181 | if (iface->len == 1) |
| 182 | outb(inb(ACBCTL1) | ACBCTL1_ACK, ACBCTL1); |
| 183 | else |
| 184 | outb(inb(ACBCTL1) & ~ACBCTL1_ACK, ACBCTL1); |
| 185 | |
| 186 | *iface->ptr++ = inb(ACBSDA); |
| 187 | --iface->len; |
| 188 | |
| 189 | if (iface->len == 0) { |
| 190 | iface->result = 0; |
| 191 | iface->state = state_idle; |
| 192 | outb(inb(ACBCTL1) | ACBCTL1_STOP, ACBCTL1); |
| 193 | } |
| 194 | |
| 195 | break; |
| 196 | |
| 197 | case state_write: |
| 198 | if (iface->len == 0) { |
| 199 | iface->result = 0; |
| 200 | iface->state = state_idle; |
| 201 | outb(inb(ACBCTL1) | ACBCTL1_STOP, ACBCTL1); |
| 202 | break; |
| 203 | } |
Ben Gardner | 99c3adb | 2006-01-18 22:41:50 +0100 | [diff] [blame] | 204 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | outb(*iface->ptr++, ACBSDA); |
| 206 | --iface->len; |
Ben Gardner | 99c3adb | 2006-01-18 22:41:50 +0100 | [diff] [blame] | 207 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | break; |
| 209 | } |
| 210 | |
| 211 | return; |
| 212 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | error: |
| 214 | dev_err(&iface->adapter.dev, "%s in state %s\n", errmsg, |
| 215 | scx200_acb_state_name[iface->state]); |
| 216 | |
| 217 | iface->state = state_idle; |
| 218 | iface->result = -EIO; |
| 219 | iface->needs_reset = 1; |
| 220 | } |
| 221 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | static void scx200_acb_poll(struct scx200_acb_iface *iface) |
| 223 | { |
Ben Gardner | 9b7b6d3 | 2006-01-18 22:44:04 +0100 | [diff] [blame] | 224 | u8 status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | unsigned long timeout; |
| 226 | |
| 227 | timeout = jiffies + POLL_TIMEOUT; |
| 228 | while (time_before(jiffies, timeout)) { |
| 229 | status = inb(ACBST); |
| 230 | if ((status & (ACBST_SDAST|ACBST_BER|ACBST_NEGACK)) != 0) { |
| 231 | scx200_acb_machine(iface, status); |
| 232 | return; |
| 233 | } |
Ben Gardner | f933ff5 | 2006-01-18 22:52:06 +0100 | [diff] [blame] | 234 | yield(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | } |
| 236 | |
Ben Gardner | 9b7b6d3 | 2006-01-18 22:44:04 +0100 | [diff] [blame] | 237 | dev_err(&iface->adapter.dev, "timeout in state %s\n", |
| 238 | scx200_acb_state_name[iface->state]); |
| 239 | |
| 240 | iface->state = state_idle; |
| 241 | iface->result = -EIO; |
| 242 | iface->needs_reset = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | |
| 245 | static void scx200_acb_reset(struct scx200_acb_iface *iface) |
| 246 | { |
| 247 | /* Disable the ACCESS.bus device and Configure the SCL |
Ben Gardner | 99c3adb | 2006-01-18 22:41:50 +0100 | [diff] [blame] | 248 | frequency: 16 clock cycles */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | outb(0x70, ACBCTL2); |
| 250 | /* Polling mode */ |
| 251 | outb(0, ACBCTL1); |
| 252 | /* Disable slave address */ |
| 253 | outb(0, ACBADDR); |
| 254 | /* Enable the ACCESS.bus device */ |
| 255 | outb(inb(ACBCTL2) | ACBCTL2_ENABLE, ACBCTL2); |
| 256 | /* Free STALL after START */ |
| 257 | outb(inb(ACBCTL1) & ~(ACBCTL1_STASTRE | ACBCTL1_NMINTE), ACBCTL1); |
| 258 | /* Send a STOP */ |
| 259 | outb(inb(ACBCTL1) | ACBCTL1_STOP, ACBCTL1); |
| 260 | /* Clear BER, NEGACK and STASTR bits */ |
| 261 | outb(ACBST_BER | ACBST_NEGACK | ACBST_STASTR, ACBST); |
| 262 | /* Clear BB bit */ |
| 263 | outb(inb(ACBCST) | ACBCST_BB, ACBCST); |
| 264 | } |
| 265 | |
| 266 | static s32 scx200_acb_smbus_xfer(struct i2c_adapter *adapter, |
Ben Gardner | 99c3adb | 2006-01-18 22:41:50 +0100 | [diff] [blame] | 267 | u16 address, unsigned short flags, |
| 268 | char rw, u8 command, int size, |
| 269 | union i2c_smbus_data *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | { |
| 271 | struct scx200_acb_iface *iface = i2c_get_adapdata(adapter); |
| 272 | int len; |
| 273 | u8 *buffer; |
| 274 | u16 cur_word; |
| 275 | int rc; |
| 276 | |
| 277 | switch (size) { |
| 278 | case I2C_SMBUS_QUICK: |
Ben Gardner | 99c3adb | 2006-01-18 22:41:50 +0100 | [diff] [blame] | 279 | len = 0; |
| 280 | buffer = NULL; |
| 281 | break; |
| 282 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | case I2C_SMBUS_BYTE: |
Ben Gardner | 9b7b6d3 | 2006-01-18 22:44:04 +0100 | [diff] [blame] | 284 | len = 1; |
| 285 | buffer = rw ? &data->byte : &command; |
Ben Gardner | 99c3adb | 2006-01-18 22:41:50 +0100 | [diff] [blame] | 286 | break; |
| 287 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | case I2C_SMBUS_BYTE_DATA: |
Ben Gardner | 99c3adb | 2006-01-18 22:41:50 +0100 | [diff] [blame] | 289 | len = 1; |
| 290 | buffer = &data->byte; |
| 291 | break; |
| 292 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | case I2C_SMBUS_WORD_DATA: |
| 294 | len = 2; |
Ben Gardner | 99c3adb | 2006-01-18 22:41:50 +0100 | [diff] [blame] | 295 | cur_word = cpu_to_le16(data->word); |
| 296 | buffer = (u8 *)&cur_word; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | break; |
Ben Gardner | 99c3adb | 2006-01-18 22:41:50 +0100 | [diff] [blame] | 298 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | case I2C_SMBUS_BLOCK_DATA: |
Ben Gardner | 99c3adb | 2006-01-18 22:41:50 +0100 | [diff] [blame] | 300 | len = data->block[0]; |
| 301 | buffer = &data->block[1]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | break; |
Ben Gardner | 99c3adb | 2006-01-18 22:41:50 +0100 | [diff] [blame] | 303 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | default: |
Ben Gardner | 99c3adb | 2006-01-18 22:41:50 +0100 | [diff] [blame] | 305 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | } |
| 307 | |
Ben Gardner | ef4d927 | 2006-01-18 22:43:10 +0100 | [diff] [blame] | 308 | dev_dbg(&adapter->dev, |
| 309 | "size=%d, address=0x%x, command=0x%x, len=%d, read=%d\n", |
| 310 | size, address, command, len, rw); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | |
| 312 | if (!len && rw == I2C_SMBUS_READ) { |
Ben Gardner | ef4d927 | 2006-01-18 22:43:10 +0100 | [diff] [blame] | 313 | dev_dbg(&adapter->dev, "zero length read\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | return -EINVAL; |
| 315 | } |
| 316 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | down(&iface->sem); |
| 318 | |
Ben Gardner | 9b7b6d3 | 2006-01-18 22:44:04 +0100 | [diff] [blame] | 319 | iface->address_byte = (address << 1) | rw; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | iface->command = command; |
| 321 | iface->ptr = buffer; |
| 322 | iface->len = len; |
| 323 | iface->result = -EINVAL; |
| 324 | iface->needs_reset = 0; |
| 325 | |
| 326 | outb(inb(ACBCTL1) | ACBCTL1_START, ACBCTL1); |
| 327 | |
| 328 | if (size == I2C_SMBUS_QUICK || size == I2C_SMBUS_BYTE) |
| 329 | iface->state = state_quick; |
| 330 | else |
| 331 | iface->state = state_address; |
| 332 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | while (iface->state != state_idle) |
| 334 | scx200_acb_poll(iface); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | |
| 336 | if (iface->needs_reset) |
| 337 | scx200_acb_reset(iface); |
| 338 | |
| 339 | rc = iface->result; |
| 340 | |
| 341 | up(&iface->sem); |
| 342 | |
| 343 | if (rc == 0 && size == I2C_SMBUS_WORD_DATA && rw == I2C_SMBUS_READ) |
Ben Gardner | 99c3adb | 2006-01-18 22:41:50 +0100 | [diff] [blame] | 344 | data->word = le16_to_cpu(cur_word); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | |
| 346 | #ifdef DEBUG |
Ben Gardner | ef4d927 | 2006-01-18 22:43:10 +0100 | [diff] [blame] | 347 | dev_dbg(&adapter->dev, "transfer done, result: %d", rc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | if (buffer) { |
| 349 | int i; |
| 350 | printk(" data:"); |
| 351 | for (i = 0; i < len; ++i) |
| 352 | printk(" %02x", buffer[i]); |
| 353 | } |
| 354 | printk("\n"); |
| 355 | #endif |
| 356 | |
| 357 | return rc; |
| 358 | } |
| 359 | |
| 360 | static u32 scx200_acb_func(struct i2c_adapter *adapter) |
| 361 | { |
| 362 | return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE | |
| 363 | I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA | |
| 364 | I2C_FUNC_SMBUS_BLOCK_DATA; |
| 365 | } |
| 366 | |
| 367 | /* For now, we only handle combined mode (smbus) */ |
| 368 | static struct i2c_algorithm scx200_acb_algorithm = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | .smbus_xfer = scx200_acb_smbus_xfer, |
| 370 | .functionality = scx200_acb_func, |
| 371 | }; |
| 372 | |
| 373 | static struct scx200_acb_iface *scx200_acb_list; |
Ben Gardner | 8a05940 | 2006-01-18 22:46:26 +0100 | [diff] [blame] | 374 | static DECLARE_MUTEX(scx200_acb_list_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | |
| 376 | static int scx200_acb_probe(struct scx200_acb_iface *iface) |
| 377 | { |
| 378 | u8 val; |
| 379 | |
| 380 | /* Disable the ACCESS.bus device and Configure the SCL |
Ben Gardner | 99c3adb | 2006-01-18 22:41:50 +0100 | [diff] [blame] | 381 | frequency: 16 clock cycles */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | outb(0x70, ACBCTL2); |
| 383 | |
| 384 | if (inb(ACBCTL2) != 0x70) { |
Ben Gardner | ef4d927 | 2006-01-18 22:43:10 +0100 | [diff] [blame] | 385 | pr_debug(NAME ": ACBCTL2 readback failed\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | return -ENXIO; |
| 387 | } |
| 388 | |
| 389 | outb(inb(ACBCTL1) | ACBCTL1_NMINTE, ACBCTL1); |
| 390 | |
| 391 | val = inb(ACBCTL1); |
| 392 | if (val) { |
Ben Gardner | ef4d927 | 2006-01-18 22:43:10 +0100 | [diff] [blame] | 393 | pr_debug(NAME ": disabled, but ACBCTL1=0x%02x\n", |
| 394 | val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | return -ENXIO; |
| 396 | } |
| 397 | |
| 398 | outb(inb(ACBCTL2) | ACBCTL2_ENABLE, ACBCTL2); |
| 399 | |
| 400 | outb(inb(ACBCTL1) | ACBCTL1_NMINTE, ACBCTL1); |
| 401 | |
| 402 | val = inb(ACBCTL1); |
| 403 | if ((val & ACBCTL1_NMINTE) != ACBCTL1_NMINTE) { |
Ben Gardner | ef4d927 | 2006-01-18 22:43:10 +0100 | [diff] [blame] | 404 | pr_debug(NAME ": enabled, but NMINTE won't be set, " |
| 405 | "ACBCTL1=0x%02x\n", val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | return -ENXIO; |
| 407 | } |
| 408 | |
| 409 | return 0; |
| 410 | } |
| 411 | |
Ben Gardner | 16ffc5c | 2006-01-18 22:48:26 +0100 | [diff] [blame] | 412 | static int __init scx200_acb_create(const char *text, int base, int index) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | { |
| 414 | struct scx200_acb_iface *iface; |
| 415 | struct i2c_adapter *adapter; |
Ben Gardner | 9b7b6d3 | 2006-01-18 22:44:04 +0100 | [diff] [blame] | 416 | int rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | char description[64]; |
| 418 | |
Deepak Saxena | 5263ebb | 2005-10-17 23:09:43 +0200 | [diff] [blame] | 419 | iface = kzalloc(sizeof(*iface), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | if (!iface) { |
| 421 | printk(KERN_ERR NAME ": can't allocate memory\n"); |
| 422 | rc = -ENOMEM; |
| 423 | goto errout; |
| 424 | } |
| 425 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | adapter = &iface->adapter; |
| 427 | i2c_set_adapdata(adapter, iface); |
Ben Gardner | 16ffc5c | 2006-01-18 22:48:26 +0100 | [diff] [blame] | 428 | snprintf(adapter->name, I2C_NAME_SIZE, "%s ACB%d", text, index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | adapter->owner = THIS_MODULE; |
Jean Delvare | 1684a984 | 2005-08-11 23:51:10 +0200 | [diff] [blame] | 430 | adapter->id = I2C_HW_SMBUS_SCX200; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | adapter->algo = &scx200_acb_algorithm; |
| 432 | adapter->class = I2C_CLASS_HWMON; |
| 433 | |
| 434 | init_MUTEX(&iface->sem); |
| 435 | |
Ben Gardner | 16ffc5c | 2006-01-18 22:48:26 +0100 | [diff] [blame] | 436 | snprintf(description, sizeof(description), "%s ACCESS.bus [%s]", |
| 437 | text, adapter->name); |
| 438 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | if (request_region(base, 8, description) == 0) { |
Ben Gardner | ef4d927 | 2006-01-18 22:43:10 +0100 | [diff] [blame] | 440 | printk(KERN_ERR NAME ": can't allocate io 0x%x-0x%x\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | base, base + 8-1); |
| 442 | rc = -EBUSY; |
Ben Gardner | 9b7b6d3 | 2006-01-18 22:44:04 +0100 | [diff] [blame] | 443 | goto errout_free; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | } |
| 445 | iface->base = base; |
| 446 | |
| 447 | rc = scx200_acb_probe(iface); |
| 448 | if (rc) { |
Ben Gardner | ef4d927 | 2006-01-18 22:43:10 +0100 | [diff] [blame] | 449 | printk(KERN_WARNING NAME ": probe failed\n"); |
Ben Gardner | 9b7b6d3 | 2006-01-18 22:44:04 +0100 | [diff] [blame] | 450 | goto errout_release; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | scx200_acb_reset(iface); |
| 454 | |
| 455 | if (i2c_add_adapter(adapter) < 0) { |
Ben Gardner | ef4d927 | 2006-01-18 22:43:10 +0100 | [diff] [blame] | 456 | printk(KERN_ERR NAME ": failed to register\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | rc = -ENODEV; |
Ben Gardner | 9b7b6d3 | 2006-01-18 22:44:04 +0100 | [diff] [blame] | 458 | goto errout_release; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | } |
| 460 | |
Ben Gardner | 8a05940 | 2006-01-18 22:46:26 +0100 | [diff] [blame] | 461 | down(&scx200_acb_list_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | iface->next = scx200_acb_list; |
| 463 | scx200_acb_list = iface; |
Ben Gardner | 8a05940 | 2006-01-18 22:46:26 +0100 | [diff] [blame] | 464 | up(&scx200_acb_list_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | |
| 466 | return 0; |
| 467 | |
Ben Gardner | 9b7b6d3 | 2006-01-18 22:44:04 +0100 | [diff] [blame] | 468 | errout_release: |
| 469 | release_region(iface->base, 8); |
| 470 | errout_free: |
| 471 | kfree(iface); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | errout: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | return rc; |
| 474 | } |
| 475 | |
| 476 | static struct pci_device_id scx200[] = { |
| 477 | { PCI_DEVICE(PCI_VENDOR_ID_NS, PCI_DEVICE_ID_NS_SCx200_BRIDGE) }, |
| 478 | { PCI_DEVICE(PCI_VENDOR_ID_NS, PCI_DEVICE_ID_NS_SC1100_BRIDGE) }, |
| 479 | { }, |
| 480 | }; |
| 481 | |
Ben Gardner | 16ffc5c | 2006-01-18 22:48:26 +0100 | [diff] [blame] | 482 | static struct pci_device_id divil_pci[] = { |
| 483 | { PCI_DEVICE(PCI_VENDOR_ID_NS, PCI_DEVICE_ID_NS_CS5535_ISA) }, |
| 484 | { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_CS5536_ISA) }, |
| 485 | { } /* NULL entry */ |
| 486 | }; |
| 487 | |
| 488 | #define MSR_LBAR_SMB 0x5140000B |
| 489 | |
| 490 | static int scx200_add_cs553x(void) |
| 491 | { |
| 492 | u32 low, hi; |
| 493 | u32 smb_base; |
| 494 | |
| 495 | /* Grab & reserve the SMB I/O range */ |
| 496 | rdmsr(MSR_LBAR_SMB, low, hi); |
| 497 | |
| 498 | /* Check the IO mask and whether SMB is enabled */ |
| 499 | if (hi != 0x0000F001) { |
| 500 | printk(KERN_WARNING NAME ": SMBus not enabled\n"); |
| 501 | return -ENODEV; |
| 502 | } |
| 503 | |
| 504 | /* SMBus IO size is 8 bytes */ |
| 505 | smb_base = low & 0x0000FFF8; |
| 506 | |
| 507 | return scx200_acb_create("CS5535", smb_base, 0); |
| 508 | } |
| 509 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | static int __init scx200_acb_init(void) |
| 511 | { |
| 512 | int i; |
Ben Gardner | 16ffc5c | 2006-01-18 22:48:26 +0100 | [diff] [blame] | 513 | int rc = -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | |
| 515 | pr_debug(NAME ": NatSemi SCx200 ACCESS.bus Driver\n"); |
| 516 | |
| 517 | /* Verify that this really is a SCx200 processor */ |
Ben Gardner | 16ffc5c | 2006-01-18 22:48:26 +0100 | [diff] [blame] | 518 | if (pci_dev_present(scx200)) { |
| 519 | for (i = 0; i < MAX_DEVICES; ++i) { |
| 520 | if (base[i] > 0) |
| 521 | rc = scx200_acb_create("SCx200", base[i], i); |
| 522 | } |
| 523 | } else if (pci_dev_present(divil_pci)) |
| 524 | rc = scx200_add_cs553x(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | return rc; |
| 527 | } |
| 528 | |
| 529 | static void __exit scx200_acb_cleanup(void) |
| 530 | { |
| 531 | struct scx200_acb_iface *iface; |
Ben Gardner | 99c3adb | 2006-01-18 22:41:50 +0100 | [diff] [blame] | 532 | |
Ben Gardner | 8a05940 | 2006-01-18 22:46:26 +0100 | [diff] [blame] | 533 | down(&scx200_acb_list_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 | while ((iface = scx200_acb_list) != NULL) { |
| 535 | scx200_acb_list = iface->next; |
Ben Gardner | 8a05940 | 2006-01-18 22:46:26 +0100 | [diff] [blame] | 536 | up(&scx200_acb_list_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 | |
| 538 | i2c_del_adapter(&iface->adapter); |
| 539 | release_region(iface->base, 8); |
| 540 | kfree(iface); |
Ben Gardner | 8a05940 | 2006-01-18 22:46:26 +0100 | [diff] [blame] | 541 | down(&scx200_acb_list_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 542 | } |
Ben Gardner | 8a05940 | 2006-01-18 22:46:26 +0100 | [diff] [blame] | 543 | up(&scx200_acb_list_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | } |
| 545 | |
| 546 | module_init(scx200_acb_init); |
| 547 | module_exit(scx200_acb_cleanup); |