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> |
Jean Delvare | 3fb9a65 | 2006-01-18 23:17:01 +0100 | [diff] [blame] | 34 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | #include <asm/io.h> |
| 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; |
Jean Delvare | 3fb9a65 | 2006-01-18 23:17:01 +0100 | [diff] [blame] | 77 | struct mutex mutex; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
Jordan Crouse | 80cd3a8 | 2006-06-12 21:44:28 +0200 | [diff] [blame] | 87 | |
| 88 | /* PCI device info */ |
| 89 | struct pci_dev *pdev; |
| 90 | int bar; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | }; |
| 92 | |
| 93 | /* Register Definitions */ |
| 94 | #define ACBSDA (iface->base + 0) |
| 95 | #define ACBST (iface->base + 1) |
| 96 | #define ACBST_SDAST 0x40 /* SDA Status */ |
Ben Gardner | 99c3adb | 2006-01-18 22:41:50 +0100 | [diff] [blame] | 97 | #define ACBST_BER 0x20 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | #define ACBST_NEGACK 0x10 /* Negative Acknowledge */ |
| 99 | #define ACBST_STASTR 0x08 /* Stall After Start */ |
| 100 | #define ACBST_MASTER 0x02 |
| 101 | #define ACBCST (iface->base + 2) |
| 102 | #define ACBCST_BB 0x02 |
| 103 | #define ACBCTL1 (iface->base + 3) |
| 104 | #define ACBCTL1_STASTRE 0x80 |
| 105 | #define ACBCTL1_NMINTE 0x40 |
Ben Gardner | 99c3adb | 2006-01-18 22:41:50 +0100 | [diff] [blame] | 106 | #define ACBCTL1_ACK 0x10 |
| 107 | #define ACBCTL1_STOP 0x02 |
| 108 | #define ACBCTL1_START 0x01 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | #define ACBADDR (iface->base + 4) |
| 110 | #define ACBCTL2 (iface->base + 5) |
| 111 | #define ACBCTL2_ENABLE 0x01 |
| 112 | |
| 113 | /************************************************************************/ |
| 114 | |
| 115 | static void scx200_acb_machine(struct scx200_acb_iface *iface, u8 status) |
| 116 | { |
| 117 | const char *errmsg; |
| 118 | |
Ben Gardner | ef4d927 | 2006-01-18 22:43:10 +0100 | [diff] [blame] | 119 | dev_dbg(&iface->adapter.dev, "state %s, status = 0x%02x\n", |
| 120 | scx200_acb_state_name[iface->state], status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | |
| 122 | if (status & ACBST_BER) { |
| 123 | errmsg = "bus error"; |
| 124 | goto error; |
| 125 | } |
| 126 | if (!(status & ACBST_MASTER)) { |
| 127 | errmsg = "not master"; |
| 128 | goto error; |
| 129 | } |
Ben Gardner | 9b7b6d3 | 2006-01-18 22:44:04 +0100 | [diff] [blame] | 130 | if (status & ACBST_NEGACK) { |
| 131 | dev_dbg(&iface->adapter.dev, "negative ack in state %s\n", |
| 132 | scx200_acb_state_name[iface->state]); |
| 133 | |
| 134 | iface->state = state_idle; |
| 135 | iface->result = -ENXIO; |
| 136 | |
| 137 | outb(inb(ACBCTL1) | ACBCTL1_STOP, ACBCTL1); |
| 138 | outb(ACBST_STASTR | ACBST_NEGACK, ACBST); |
Jordan Crouse | 95563d3 | 2006-04-28 22:53:30 +0200 | [diff] [blame] | 139 | |
| 140 | /* Reset the status register */ |
| 141 | outb(0, ACBST); |
Ben Gardner | 9b7b6d3 | 2006-01-18 22:44:04 +0100 | [diff] [blame] | 142 | return; |
| 143 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | |
| 145 | switch (iface->state) { |
| 146 | case state_idle: |
| 147 | dev_warn(&iface->adapter.dev, "interrupt in idle state\n"); |
| 148 | break; |
| 149 | |
| 150 | case state_address: |
| 151 | /* Do a pointer write first */ |
| 152 | outb(iface->address_byte & ~1, ACBSDA); |
| 153 | |
| 154 | iface->state = state_command; |
| 155 | break; |
| 156 | |
| 157 | case state_command: |
| 158 | outb(iface->command, ACBSDA); |
| 159 | |
| 160 | if (iface->address_byte & 1) |
| 161 | iface->state = state_repeat_start; |
| 162 | else |
| 163 | iface->state = state_write; |
| 164 | break; |
| 165 | |
| 166 | case state_repeat_start: |
| 167 | outb(inb(ACBCTL1) | ACBCTL1_START, ACBCTL1); |
| 168 | /* fallthrough */ |
Ben Gardner | 99c3adb | 2006-01-18 22:41:50 +0100 | [diff] [blame] | 169 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | case state_quick: |
| 171 | if (iface->address_byte & 1) { |
Ben Gardner | 99c3adb | 2006-01-18 22:41:50 +0100 | [diff] [blame] | 172 | if (iface->len == 1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | outb(inb(ACBCTL1) | ACBCTL1_ACK, ACBCTL1); |
| 174 | else |
| 175 | outb(inb(ACBCTL1) & ~ACBCTL1_ACK, ACBCTL1); |
| 176 | outb(iface->address_byte, ACBSDA); |
| 177 | |
| 178 | iface->state = state_read; |
| 179 | } else { |
| 180 | outb(iface->address_byte, ACBSDA); |
| 181 | |
| 182 | iface->state = state_write; |
| 183 | } |
| 184 | break; |
| 185 | |
| 186 | case state_read: |
Thomas Andrews | fd627a0 | 2006-07-01 17:05:12 +0200 | [diff] [blame] | 187 | /* Set ACK if _next_ byte will be the last one */ |
| 188 | if (iface->len == 2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | outb(inb(ACBCTL1) | ACBCTL1_ACK, ACBCTL1); |
| 190 | else |
| 191 | outb(inb(ACBCTL1) & ~ACBCTL1_ACK, ACBCTL1); |
| 192 | |
Thomas Andrews | fd627a0 | 2006-07-01 17:05:12 +0200 | [diff] [blame] | 193 | if (iface->len == 1) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | iface->result = 0; |
| 195 | iface->state = state_idle; |
| 196 | outb(inb(ACBCTL1) | ACBCTL1_STOP, ACBCTL1); |
| 197 | } |
| 198 | |
Thomas Andrews | fd627a0 | 2006-07-01 17:05:12 +0200 | [diff] [blame] | 199 | *iface->ptr++ = inb(ACBSDA); |
| 200 | --iface->len; |
| 201 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | break; |
| 203 | |
| 204 | case state_write: |
| 205 | if (iface->len == 0) { |
| 206 | iface->result = 0; |
| 207 | iface->state = state_idle; |
| 208 | outb(inb(ACBCTL1) | ACBCTL1_STOP, ACBCTL1); |
| 209 | break; |
| 210 | } |
Ben Gardner | 99c3adb | 2006-01-18 22:41:50 +0100 | [diff] [blame] | 211 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | outb(*iface->ptr++, ACBSDA); |
| 213 | --iface->len; |
Ben Gardner | 99c3adb | 2006-01-18 22:41:50 +0100 | [diff] [blame] | 214 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | break; |
| 216 | } |
| 217 | |
| 218 | return; |
| 219 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | error: |
| 221 | dev_err(&iface->adapter.dev, "%s in state %s\n", errmsg, |
| 222 | scx200_acb_state_name[iface->state]); |
| 223 | |
| 224 | iface->state = state_idle; |
| 225 | iface->result = -EIO; |
| 226 | iface->needs_reset = 1; |
| 227 | } |
| 228 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | static void scx200_acb_poll(struct scx200_acb_iface *iface) |
| 230 | { |
Ben Gardner | 9b7b6d3 | 2006-01-18 22:44:04 +0100 | [diff] [blame] | 231 | u8 status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | unsigned long timeout; |
| 233 | |
| 234 | timeout = jiffies + POLL_TIMEOUT; |
David Woodhouse | 3e3183b | 2006-08-05 12:15:19 -0700 | [diff] [blame^] | 235 | while (1) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | status = inb(ACBST); |
Jordan Crouse | 95563d3 | 2006-04-28 22:53:30 +0200 | [diff] [blame] | 237 | |
| 238 | /* Reset the status register to avoid the hang */ |
| 239 | outb(0, ACBST); |
| 240 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | if ((status & (ACBST_SDAST|ACBST_BER|ACBST_NEGACK)) != 0) { |
| 242 | scx200_acb_machine(iface, status); |
| 243 | return; |
| 244 | } |
David Woodhouse | 3e3183b | 2006-08-05 12:15:19 -0700 | [diff] [blame^] | 245 | if (time_after(jiffies, timeout)) |
| 246 | break; |
| 247 | cpu_relax(); |
| 248 | cond_resched(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | } |
| 250 | |
Ben Gardner | 9b7b6d3 | 2006-01-18 22:44:04 +0100 | [diff] [blame] | 251 | dev_err(&iface->adapter.dev, "timeout in state %s\n", |
| 252 | scx200_acb_state_name[iface->state]); |
| 253 | |
| 254 | iface->state = state_idle; |
| 255 | iface->result = -EIO; |
| 256 | iface->needs_reset = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | |
| 259 | static void scx200_acb_reset(struct scx200_acb_iface *iface) |
| 260 | { |
| 261 | /* Disable the ACCESS.bus device and Configure the SCL |
Ben Gardner | 99c3adb | 2006-01-18 22:41:50 +0100 | [diff] [blame] | 262 | frequency: 16 clock cycles */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | outb(0x70, ACBCTL2); |
| 264 | /* Polling mode */ |
| 265 | outb(0, ACBCTL1); |
| 266 | /* Disable slave address */ |
| 267 | outb(0, ACBADDR); |
| 268 | /* Enable the ACCESS.bus device */ |
| 269 | outb(inb(ACBCTL2) | ACBCTL2_ENABLE, ACBCTL2); |
| 270 | /* Free STALL after START */ |
| 271 | outb(inb(ACBCTL1) & ~(ACBCTL1_STASTRE | ACBCTL1_NMINTE), ACBCTL1); |
| 272 | /* Send a STOP */ |
| 273 | outb(inb(ACBCTL1) | ACBCTL1_STOP, ACBCTL1); |
| 274 | /* Clear BER, NEGACK and STASTR bits */ |
| 275 | outb(ACBST_BER | ACBST_NEGACK | ACBST_STASTR, ACBST); |
| 276 | /* Clear BB bit */ |
| 277 | outb(inb(ACBCST) | ACBCST_BB, ACBCST); |
| 278 | } |
| 279 | |
| 280 | static s32 scx200_acb_smbus_xfer(struct i2c_adapter *adapter, |
Ben Gardner | 99c3adb | 2006-01-18 22:41:50 +0100 | [diff] [blame] | 281 | u16 address, unsigned short flags, |
| 282 | char rw, u8 command, int size, |
| 283 | union i2c_smbus_data *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | { |
| 285 | struct scx200_acb_iface *iface = i2c_get_adapdata(adapter); |
| 286 | int len; |
| 287 | u8 *buffer; |
| 288 | u16 cur_word; |
| 289 | int rc; |
| 290 | |
| 291 | switch (size) { |
| 292 | case I2C_SMBUS_QUICK: |
Ben Gardner | 99c3adb | 2006-01-18 22:41:50 +0100 | [diff] [blame] | 293 | len = 0; |
| 294 | buffer = NULL; |
| 295 | break; |
| 296 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | case I2C_SMBUS_BYTE: |
Ben Gardner | 9b7b6d3 | 2006-01-18 22:44:04 +0100 | [diff] [blame] | 298 | len = 1; |
| 299 | buffer = rw ? &data->byte : &command; |
Ben Gardner | 99c3adb | 2006-01-18 22:41:50 +0100 | [diff] [blame] | 300 | break; |
| 301 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | case I2C_SMBUS_BYTE_DATA: |
Ben Gardner | 99c3adb | 2006-01-18 22:41:50 +0100 | [diff] [blame] | 303 | len = 1; |
| 304 | buffer = &data->byte; |
| 305 | break; |
| 306 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | case I2C_SMBUS_WORD_DATA: |
| 308 | len = 2; |
Ben Gardner | 99c3adb | 2006-01-18 22:41:50 +0100 | [diff] [blame] | 309 | cur_word = cpu_to_le16(data->word); |
| 310 | buffer = (u8 *)&cur_word; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | break; |
Ben Gardner | 99c3adb | 2006-01-18 22:41:50 +0100 | [diff] [blame] | 312 | |
Jean Delvare | c3efaca | 2006-07-01 17:06:43 +0200 | [diff] [blame] | 313 | case I2C_SMBUS_I2C_BLOCK_DATA: |
| 314 | if (rw == I2C_SMBUS_READ) |
| 315 | data->block[0] = I2C_SMBUS_BLOCK_MAX; /* For now */ |
Ben Gardner | 99c3adb | 2006-01-18 22:41:50 +0100 | [diff] [blame] | 316 | len = data->block[0]; |
Jean Delvare | c3efaca | 2006-07-01 17:06:43 +0200 | [diff] [blame] | 317 | if (len == 0 || len > I2C_SMBUS_BLOCK_MAX) |
| 318 | return -EINVAL; |
Ben Gardner | 99c3adb | 2006-01-18 22:41:50 +0100 | [diff] [blame] | 319 | buffer = &data->block[1]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | break; |
Ben Gardner | 99c3adb | 2006-01-18 22:41:50 +0100 | [diff] [blame] | 321 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | default: |
Ben Gardner | 99c3adb | 2006-01-18 22:41:50 +0100 | [diff] [blame] | 323 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | } |
| 325 | |
Ben Gardner | ef4d927 | 2006-01-18 22:43:10 +0100 | [diff] [blame] | 326 | dev_dbg(&adapter->dev, |
| 327 | "size=%d, address=0x%x, command=0x%x, len=%d, read=%d\n", |
| 328 | size, address, command, len, rw); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | |
| 330 | if (!len && rw == I2C_SMBUS_READ) { |
Ben Gardner | ef4d927 | 2006-01-18 22:43:10 +0100 | [diff] [blame] | 331 | dev_dbg(&adapter->dev, "zero length read\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | return -EINVAL; |
| 333 | } |
| 334 | |
Jean Delvare | 3fb9a65 | 2006-01-18 23:17:01 +0100 | [diff] [blame] | 335 | mutex_lock(&iface->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | |
Ben Gardner | 9b7b6d3 | 2006-01-18 22:44:04 +0100 | [diff] [blame] | 337 | iface->address_byte = (address << 1) | rw; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | iface->command = command; |
| 339 | iface->ptr = buffer; |
| 340 | iface->len = len; |
| 341 | iface->result = -EINVAL; |
| 342 | iface->needs_reset = 0; |
| 343 | |
| 344 | outb(inb(ACBCTL1) | ACBCTL1_START, ACBCTL1); |
| 345 | |
| 346 | if (size == I2C_SMBUS_QUICK || size == I2C_SMBUS_BYTE) |
| 347 | iface->state = state_quick; |
| 348 | else |
| 349 | iface->state = state_address; |
| 350 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | while (iface->state != state_idle) |
| 352 | scx200_acb_poll(iface); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | |
| 354 | if (iface->needs_reset) |
| 355 | scx200_acb_reset(iface); |
| 356 | |
| 357 | rc = iface->result; |
| 358 | |
Jean Delvare | 3fb9a65 | 2006-01-18 23:17:01 +0100 | [diff] [blame] | 359 | mutex_unlock(&iface->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | |
| 361 | if (rc == 0 && size == I2C_SMBUS_WORD_DATA && rw == I2C_SMBUS_READ) |
Ben Gardner | 99c3adb | 2006-01-18 22:41:50 +0100 | [diff] [blame] | 362 | data->word = le16_to_cpu(cur_word); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | |
| 364 | #ifdef DEBUG |
Ben Gardner | ef4d927 | 2006-01-18 22:43:10 +0100 | [diff] [blame] | 365 | dev_dbg(&adapter->dev, "transfer done, result: %d", rc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | if (buffer) { |
| 367 | int i; |
| 368 | printk(" data:"); |
| 369 | for (i = 0; i < len; ++i) |
| 370 | printk(" %02x", buffer[i]); |
| 371 | } |
| 372 | printk("\n"); |
| 373 | #endif |
| 374 | |
| 375 | return rc; |
| 376 | } |
| 377 | |
| 378 | static u32 scx200_acb_func(struct i2c_adapter *adapter) |
| 379 | { |
| 380 | return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE | |
| 381 | I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA | |
Jean Delvare | c3efaca | 2006-07-01 17:06:43 +0200 | [diff] [blame] | 382 | I2C_FUNC_SMBUS_I2C_BLOCK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | /* For now, we only handle combined mode (smbus) */ |
| 386 | static struct i2c_algorithm scx200_acb_algorithm = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | .smbus_xfer = scx200_acb_smbus_xfer, |
| 388 | .functionality = scx200_acb_func, |
| 389 | }; |
| 390 | |
| 391 | static struct scx200_acb_iface *scx200_acb_list; |
Ben Gardner | 8a05940 | 2006-01-18 22:46:26 +0100 | [diff] [blame] | 392 | static DECLARE_MUTEX(scx200_acb_list_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | |
Jean Delvare | 9917392 | 2006-06-12 21:46:04 +0200 | [diff] [blame] | 394 | static __init int scx200_acb_probe(struct scx200_acb_iface *iface) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | { |
| 396 | u8 val; |
| 397 | |
| 398 | /* Disable the ACCESS.bus device and Configure the SCL |
Ben Gardner | 99c3adb | 2006-01-18 22:41:50 +0100 | [diff] [blame] | 399 | frequency: 16 clock cycles */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | outb(0x70, ACBCTL2); |
| 401 | |
| 402 | if (inb(ACBCTL2) != 0x70) { |
Ben Gardner | ef4d927 | 2006-01-18 22:43:10 +0100 | [diff] [blame] | 403 | pr_debug(NAME ": ACBCTL2 readback failed\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | return -ENXIO; |
| 405 | } |
| 406 | |
| 407 | outb(inb(ACBCTL1) | ACBCTL1_NMINTE, ACBCTL1); |
| 408 | |
| 409 | val = inb(ACBCTL1); |
| 410 | if (val) { |
Ben Gardner | ef4d927 | 2006-01-18 22:43:10 +0100 | [diff] [blame] | 411 | pr_debug(NAME ": disabled, but ACBCTL1=0x%02x\n", |
| 412 | val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | return -ENXIO; |
| 414 | } |
| 415 | |
| 416 | outb(inb(ACBCTL2) | ACBCTL2_ENABLE, ACBCTL2); |
| 417 | |
| 418 | outb(inb(ACBCTL1) | ACBCTL1_NMINTE, ACBCTL1); |
| 419 | |
| 420 | val = inb(ACBCTL1); |
| 421 | if ((val & ACBCTL1_NMINTE) != ACBCTL1_NMINTE) { |
Ben Gardner | ef4d927 | 2006-01-18 22:43:10 +0100 | [diff] [blame] | 422 | pr_debug(NAME ": enabled, but NMINTE won't be set, " |
| 423 | "ACBCTL1=0x%02x\n", val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | return -ENXIO; |
| 425 | } |
| 426 | |
| 427 | return 0; |
| 428 | } |
| 429 | |
Jordan Crouse | 80cd3a8 | 2006-06-12 21:44:28 +0200 | [diff] [blame] | 430 | static __init struct scx200_acb_iface *scx200_create_iface(const char *text, |
| 431 | int index) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | { |
| 433 | struct scx200_acb_iface *iface; |
| 434 | struct i2c_adapter *adapter; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | |
Deepak Saxena | 5263ebb | 2005-10-17 23:09:43 +0200 | [diff] [blame] | 436 | iface = kzalloc(sizeof(*iface), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | if (!iface) { |
| 438 | printk(KERN_ERR NAME ": can't allocate memory\n"); |
Jordan Crouse | 80cd3a8 | 2006-06-12 21:44:28 +0200 | [diff] [blame] | 439 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | } |
| 441 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | adapter = &iface->adapter; |
| 443 | i2c_set_adapdata(adapter, iface); |
Ben Gardner | 16ffc5c | 2006-01-18 22:48:26 +0100 | [diff] [blame] | 444 | snprintf(adapter->name, I2C_NAME_SIZE, "%s ACB%d", text, index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | adapter->owner = THIS_MODULE; |
Jean Delvare | 1684a984 | 2005-08-11 23:51:10 +0200 | [diff] [blame] | 446 | adapter->id = I2C_HW_SMBUS_SCX200; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | adapter->algo = &scx200_acb_algorithm; |
| 448 | adapter->class = I2C_CLASS_HWMON; |
| 449 | |
Jean Delvare | 3fb9a65 | 2006-01-18 23:17:01 +0100 | [diff] [blame] | 450 | mutex_init(&iface->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | |
Jordan Crouse | 80cd3a8 | 2006-06-12 21:44:28 +0200 | [diff] [blame] | 452 | return iface; |
| 453 | } |
| 454 | |
| 455 | static int __init scx200_acb_create(struct scx200_acb_iface *iface) |
| 456 | { |
| 457 | struct i2c_adapter *adapter; |
| 458 | int rc; |
| 459 | |
| 460 | adapter = &iface->adapter; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | |
| 462 | rc = scx200_acb_probe(iface); |
| 463 | if (rc) { |
Ben Gardner | ef4d927 | 2006-01-18 22:43:10 +0100 | [diff] [blame] | 464 | printk(KERN_WARNING NAME ": probe failed\n"); |
Jordan Crouse | 80cd3a8 | 2006-06-12 21:44:28 +0200 | [diff] [blame] | 465 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | scx200_acb_reset(iface); |
| 469 | |
| 470 | if (i2c_add_adapter(adapter) < 0) { |
Ben Gardner | ef4d927 | 2006-01-18 22:43:10 +0100 | [diff] [blame] | 471 | printk(KERN_ERR NAME ": failed to register\n"); |
Jordan Crouse | 80cd3a8 | 2006-06-12 21:44:28 +0200 | [diff] [blame] | 472 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | } |
| 474 | |
Ben Gardner | 8a05940 | 2006-01-18 22:46:26 +0100 | [diff] [blame] | 475 | down(&scx200_acb_list_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | iface->next = scx200_acb_list; |
| 477 | scx200_acb_list = iface; |
Ben Gardner | 8a05940 | 2006-01-18 22:46:26 +0100 | [diff] [blame] | 478 | up(&scx200_acb_list_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | |
| 480 | return 0; |
Jordan Crouse | 80cd3a8 | 2006-06-12 21:44:28 +0200 | [diff] [blame] | 481 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | |
Jordan Crouse | 80cd3a8 | 2006-06-12 21:44:28 +0200 | [diff] [blame] | 483 | static __init int scx200_create_pci(const char *text, struct pci_dev *pdev, |
| 484 | int bar) |
| 485 | { |
| 486 | struct scx200_acb_iface *iface; |
| 487 | int rc; |
| 488 | |
| 489 | iface = scx200_create_iface(text, 0); |
| 490 | |
| 491 | if (iface == NULL) |
| 492 | return -ENOMEM; |
| 493 | |
| 494 | iface->pdev = pdev; |
| 495 | iface->bar = bar; |
| 496 | |
| 497 | pci_enable_device_bars(iface->pdev, 1 << iface->bar); |
| 498 | |
| 499 | rc = pci_request_region(iface->pdev, iface->bar, iface->adapter.name); |
| 500 | |
| 501 | if (rc != 0) { |
| 502 | printk(KERN_ERR NAME ": can't allocate PCI BAR %d\n", |
| 503 | iface->bar); |
| 504 | goto errout_free; |
| 505 | } |
| 506 | |
| 507 | iface->base = pci_resource_start(iface->pdev, iface->bar); |
| 508 | rc = scx200_acb_create(iface); |
| 509 | |
| 510 | if (rc == 0) |
| 511 | return 0; |
| 512 | |
| 513 | pci_release_region(iface->pdev, iface->bar); |
| 514 | pci_dev_put(iface->pdev); |
Ben Gardner | 9b7b6d3 | 2006-01-18 22:44:04 +0100 | [diff] [blame] | 515 | errout_free: |
| 516 | kfree(iface); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | return rc; |
| 518 | } |
| 519 | |
Jordan Crouse | 80cd3a8 | 2006-06-12 21:44:28 +0200 | [diff] [blame] | 520 | static int __init scx200_create_isa(const char *text, unsigned long base, |
| 521 | int index) |
Ben Gardner | 16ffc5c | 2006-01-18 22:48:26 +0100 | [diff] [blame] | 522 | { |
Jordan Crouse | 80cd3a8 | 2006-06-12 21:44:28 +0200 | [diff] [blame] | 523 | struct scx200_acb_iface *iface; |
| 524 | int rc; |
Ben Gardner | 16ffc5c | 2006-01-18 22:48:26 +0100 | [diff] [blame] | 525 | |
Jordan Crouse | 80cd3a8 | 2006-06-12 21:44:28 +0200 | [diff] [blame] | 526 | iface = scx200_create_iface(text, index); |
Ben Gardner | 16ffc5c | 2006-01-18 22:48:26 +0100 | [diff] [blame] | 527 | |
Jordan Crouse | 80cd3a8 | 2006-06-12 21:44:28 +0200 | [diff] [blame] | 528 | if (iface == NULL) |
| 529 | return -ENOMEM; |
| 530 | |
| 531 | if (request_region(base, 8, iface->adapter.name) == 0) { |
| 532 | printk(KERN_ERR NAME ": can't allocate io 0x%lx-0x%lx\n", |
| 533 | base, base + 8 - 1); |
| 534 | rc = -EBUSY; |
| 535 | goto errout_free; |
Ben Gardner | 16ffc5c | 2006-01-18 22:48:26 +0100 | [diff] [blame] | 536 | } |
| 537 | |
Jordan Crouse | 80cd3a8 | 2006-06-12 21:44:28 +0200 | [diff] [blame] | 538 | iface->base = base; |
| 539 | rc = scx200_acb_create(iface); |
Ben Gardner | 16ffc5c | 2006-01-18 22:48:26 +0100 | [diff] [blame] | 540 | |
Jordan Crouse | 80cd3a8 | 2006-06-12 21:44:28 +0200 | [diff] [blame] | 541 | if (rc == 0) |
| 542 | return 0; |
| 543 | |
| 544 | release_region(base, 8); |
| 545 | errout_free: |
| 546 | kfree(iface); |
| 547 | return rc; |
| 548 | } |
| 549 | |
| 550 | /* Driver data is an index into the scx200_data array that indicates |
| 551 | * the name and the BAR where the I/O address resource is located. ISA |
| 552 | * devices are flagged with a bar value of -1 */ |
| 553 | |
| 554 | static struct pci_device_id scx200_pci[] = { |
| 555 | { PCI_DEVICE(PCI_VENDOR_ID_NS, PCI_DEVICE_ID_NS_SCx200_BRIDGE), |
| 556 | .driver_data = 0 }, |
| 557 | { PCI_DEVICE(PCI_VENDOR_ID_NS, PCI_DEVICE_ID_NS_SC1100_BRIDGE), |
| 558 | .driver_data = 0 }, |
| 559 | { PCI_DEVICE(PCI_VENDOR_ID_NS, PCI_DEVICE_ID_NS_CS5535_ISA), |
| 560 | .driver_data = 1 }, |
| 561 | { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_CS5536_ISA), |
| 562 | .driver_data = 2 } |
| 563 | }; |
| 564 | |
| 565 | static struct { |
| 566 | const char *name; |
| 567 | int bar; |
| 568 | } scx200_data[] = { |
| 569 | { "SCx200", -1 }, |
| 570 | { "CS5535", 0 }, |
| 571 | { "CS5536", 0 } |
| 572 | }; |
| 573 | |
| 574 | static __init int scx200_scan_pci(void) |
| 575 | { |
| 576 | int data, dev; |
| 577 | int rc = -ENODEV; |
| 578 | struct pci_dev *pdev; |
| 579 | |
| 580 | for(dev = 0; dev < ARRAY_SIZE(scx200_pci); dev++) { |
| 581 | pdev = pci_get_device(scx200_pci[dev].vendor, |
| 582 | scx200_pci[dev].device, NULL); |
| 583 | |
| 584 | if (pdev == NULL) |
| 585 | continue; |
| 586 | |
| 587 | data = scx200_pci[dev].driver_data; |
| 588 | |
| 589 | /* if .bar is greater or equal to zero, this is a |
| 590 | * PCI device - otherwise, we assume |
| 591 | that the ports are ISA based |
| 592 | */ |
| 593 | |
| 594 | if (scx200_data[data].bar >= 0) |
| 595 | rc = scx200_create_pci(scx200_data[data].name, pdev, |
| 596 | scx200_data[data].bar); |
| 597 | else { |
| 598 | int i; |
| 599 | |
| 600 | for (i = 0; i < MAX_DEVICES; ++i) { |
| 601 | if (base[i] == 0) |
| 602 | continue; |
| 603 | |
| 604 | rc = scx200_create_isa(scx200_data[data].name, |
| 605 | base[i], |
| 606 | i); |
| 607 | } |
| 608 | } |
| 609 | |
| 610 | break; |
| 611 | } |
| 612 | |
| 613 | return rc; |
Ben Gardner | 16ffc5c | 2006-01-18 22:48:26 +0100 | [diff] [blame] | 614 | } |
| 615 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 616 | static int __init scx200_acb_init(void) |
| 617 | { |
Jordan Crouse | 80cd3a8 | 2006-06-12 21:44:28 +0200 | [diff] [blame] | 618 | int rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 619 | |
| 620 | pr_debug(NAME ": NatSemi SCx200 ACCESS.bus Driver\n"); |
| 621 | |
Jordan Crouse | 80cd3a8 | 2006-06-12 21:44:28 +0200 | [diff] [blame] | 622 | rc = scx200_scan_pci(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | |
Jean Delvare | 6f9c296 | 2006-04-26 22:50:32 +0200 | [diff] [blame] | 624 | /* If at least one bus was created, init must succeed */ |
| 625 | if (scx200_acb_list) |
| 626 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 627 | return rc; |
| 628 | } |
| 629 | |
| 630 | static void __exit scx200_acb_cleanup(void) |
| 631 | { |
| 632 | struct scx200_acb_iface *iface; |
Ben Gardner | 99c3adb | 2006-01-18 22:41:50 +0100 | [diff] [blame] | 633 | |
Ben Gardner | 8a05940 | 2006-01-18 22:46:26 +0100 | [diff] [blame] | 634 | down(&scx200_acb_list_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 635 | while ((iface = scx200_acb_list) != NULL) { |
| 636 | scx200_acb_list = iface->next; |
Ben Gardner | 8a05940 | 2006-01-18 22:46:26 +0100 | [diff] [blame] | 637 | up(&scx200_acb_list_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 638 | |
| 639 | i2c_del_adapter(&iface->adapter); |
Jordan Crouse | 80cd3a8 | 2006-06-12 21:44:28 +0200 | [diff] [blame] | 640 | |
| 641 | if (iface->pdev) { |
| 642 | pci_release_region(iface->pdev, iface->bar); |
| 643 | pci_dev_put(iface->pdev); |
| 644 | } |
| 645 | else |
| 646 | release_region(iface->base, 8); |
| 647 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 648 | kfree(iface); |
Ben Gardner | 8a05940 | 2006-01-18 22:46:26 +0100 | [diff] [blame] | 649 | down(&scx200_acb_list_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 650 | } |
Ben Gardner | 8a05940 | 2006-01-18 22:46:26 +0100 | [diff] [blame] | 651 | up(&scx200_acb_list_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 652 | } |
| 653 | |
| 654 | module_init(scx200_acb_init); |
| 655 | module_exit(scx200_acb_cleanup); |