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