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 | |
Jim Cromie | 401e72b | 2012-10-05 22:23:52 +0200 | [diff] [blame] | 26 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 27 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include <linux/module.h> |
| 29 | #include <linux/errno.h> |
| 30 | #include <linux/kernel.h> |
| 31 | #include <linux/init.h> |
| 32 | #include <linux/i2c.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | #include <linux/pci.h> |
Andres Salomon | de8255c | 2010-12-30 20:27:33 -0800 | [diff] [blame] | 34 | #include <linux/platform_device.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | #include <linux/delay.h> |
Jean Delvare | 3fb9a65 | 2006-01-18 23:17:01 +0100 | [diff] [blame] | 36 | #include <linux/mutex.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 37 | #include <linux/slab.h> |
H Hartley Sweeten | 2178218 | 2010-05-21 18:41:01 +0200 | [diff] [blame] | 38 | #include <linux/io.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | |
| 40 | #include <linux/scx200.h> |
| 41 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | MODULE_AUTHOR("Christer Weinigel <wingel@nano-system.com>"); |
| 43 | MODULE_DESCRIPTION("NatSemi SCx200 ACCESS.bus Driver"); |
Andres Salomon | de8255c | 2010-12-30 20:27:33 -0800 | [diff] [blame] | 44 | MODULE_ALIAS("platform:cs5535-smb"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | MODULE_LICENSE("GPL"); |
| 46 | |
| 47 | #define MAX_DEVICES 4 |
| 48 | static int base[MAX_DEVICES] = { 0x820, 0x840 }; |
| 49 | module_param_array(base, int, NULL, 0); |
| 50 | MODULE_PARM_DESC(base, "Base addresses for the ACCESS.bus controllers"); |
| 51 | |
Ben Gardner | f933ff5 | 2006-01-18 22:52:06 +0100 | [diff] [blame] | 52 | #define POLL_TIMEOUT (HZ/5) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | |
| 54 | enum scx200_acb_state { |
| 55 | state_idle, |
| 56 | state_address, |
| 57 | state_command, |
| 58 | state_repeat_start, |
| 59 | state_quick, |
| 60 | state_read, |
| 61 | state_write, |
| 62 | }; |
| 63 | |
| 64 | static const char *scx200_acb_state_name[] = { |
| 65 | "idle", |
| 66 | "address", |
| 67 | "command", |
| 68 | "repeat_start", |
| 69 | "quick", |
| 70 | "read", |
| 71 | "write", |
| 72 | }; |
| 73 | |
| 74 | /* Physical interface */ |
Ben Gardner | 99c3adb | 2006-01-18 22:41:50 +0100 | [diff] [blame] | 75 | struct scx200_acb_iface { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | struct scx200_acb_iface *next; |
| 77 | struct i2c_adapter adapter; |
| 78 | unsigned base; |
Jean Delvare | 3fb9a65 | 2006-01-18 23:17:01 +0100 | [diff] [blame] | 79 | struct mutex mutex; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | |
| 81 | /* State machine data */ |
| 82 | enum scx200_acb_state state; |
| 83 | int result; |
| 84 | u8 address_byte; |
| 85 | u8 command; |
| 86 | u8 *ptr; |
| 87 | char needs_reset; |
| 88 | unsigned len; |
| 89 | }; |
| 90 | |
| 91 | /* Register Definitions */ |
| 92 | #define ACBSDA (iface->base + 0) |
| 93 | #define ACBST (iface->base + 1) |
| 94 | #define ACBST_SDAST 0x40 /* SDA Status */ |
Ben Gardner | 99c3adb | 2006-01-18 22:41:50 +0100 | [diff] [blame] | 95 | #define ACBST_BER 0x20 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | #define ACBST_NEGACK 0x10 /* Negative Acknowledge */ |
| 97 | #define ACBST_STASTR 0x08 /* Stall After Start */ |
| 98 | #define ACBST_MASTER 0x02 |
| 99 | #define ACBCST (iface->base + 2) |
| 100 | #define ACBCST_BB 0x02 |
| 101 | #define ACBCTL1 (iface->base + 3) |
| 102 | #define ACBCTL1_STASTRE 0x80 |
| 103 | #define ACBCTL1_NMINTE 0x40 |
Ben Gardner | 99c3adb | 2006-01-18 22:41:50 +0100 | [diff] [blame] | 104 | #define ACBCTL1_ACK 0x10 |
| 105 | #define ACBCTL1_STOP 0x02 |
| 106 | #define ACBCTL1_START 0x01 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | #define ACBADDR (iface->base + 4) |
| 108 | #define ACBCTL2 (iface->base + 5) |
| 109 | #define ACBCTL2_ENABLE 0x01 |
| 110 | |
| 111 | /************************************************************************/ |
| 112 | |
| 113 | static void scx200_acb_machine(struct scx200_acb_iface *iface, u8 status) |
| 114 | { |
| 115 | const char *errmsg; |
| 116 | |
Ben Gardner | ef4d927 | 2006-01-18 22:43:10 +0100 | [diff] [blame] | 117 | dev_dbg(&iface->adapter.dev, "state %s, status = 0x%02x\n", |
| 118 | scx200_acb_state_name[iface->state], status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | |
| 120 | if (status & ACBST_BER) { |
| 121 | errmsg = "bus error"; |
| 122 | goto error; |
| 123 | } |
| 124 | if (!(status & ACBST_MASTER)) { |
| 125 | errmsg = "not master"; |
| 126 | goto error; |
| 127 | } |
Ben Gardner | 9b7b6d3 | 2006-01-18 22:44:04 +0100 | [diff] [blame] | 128 | if (status & ACBST_NEGACK) { |
| 129 | dev_dbg(&iface->adapter.dev, "negative ack in state %s\n", |
| 130 | scx200_acb_state_name[iface->state]); |
| 131 | |
| 132 | iface->state = state_idle; |
| 133 | iface->result = -ENXIO; |
| 134 | |
| 135 | outb(inb(ACBCTL1) | ACBCTL1_STOP, ACBCTL1); |
| 136 | outb(ACBST_STASTR | ACBST_NEGACK, ACBST); |
Jordan Crouse | 95563d3 | 2006-04-28 22:53:30 +0200 | [diff] [blame] | 137 | |
| 138 | /* Reset the status register */ |
| 139 | outb(0, ACBST); |
Ben Gardner | 9b7b6d3 | 2006-01-18 22:44:04 +0100 | [diff] [blame] | 140 | return; |
| 141 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | |
| 143 | switch (iface->state) { |
| 144 | case state_idle: |
| 145 | dev_warn(&iface->adapter.dev, "interrupt in idle state\n"); |
| 146 | break; |
| 147 | |
| 148 | case state_address: |
| 149 | /* Do a pointer write first */ |
| 150 | outb(iface->address_byte & ~1, ACBSDA); |
| 151 | |
| 152 | iface->state = state_command; |
| 153 | break; |
| 154 | |
| 155 | case state_command: |
| 156 | outb(iface->command, ACBSDA); |
| 157 | |
| 158 | if (iface->address_byte & 1) |
| 159 | iface->state = state_repeat_start; |
| 160 | else |
| 161 | iface->state = state_write; |
| 162 | break; |
| 163 | |
| 164 | case state_repeat_start: |
| 165 | outb(inb(ACBCTL1) | ACBCTL1_START, ACBCTL1); |
| 166 | /* fallthrough */ |
Ben Gardner | 99c3adb | 2006-01-18 22:41:50 +0100 | [diff] [blame] | 167 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | case state_quick: |
| 169 | if (iface->address_byte & 1) { |
Ben Gardner | 99c3adb | 2006-01-18 22:41:50 +0100 | [diff] [blame] | 170 | if (iface->len == 1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | outb(inb(ACBCTL1) | ACBCTL1_ACK, ACBCTL1); |
| 172 | else |
| 173 | outb(inb(ACBCTL1) & ~ACBCTL1_ACK, ACBCTL1); |
| 174 | outb(iface->address_byte, ACBSDA); |
| 175 | |
| 176 | iface->state = state_read; |
| 177 | } else { |
| 178 | outb(iface->address_byte, ACBSDA); |
| 179 | |
| 180 | iface->state = state_write; |
| 181 | } |
| 182 | break; |
| 183 | |
| 184 | case state_read: |
Thomas Andrews | fd627a0 | 2006-07-01 17:05:12 +0200 | [diff] [blame] | 185 | /* Set ACK if _next_ byte will be the last one */ |
| 186 | if (iface->len == 2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | outb(inb(ACBCTL1) | ACBCTL1_ACK, ACBCTL1); |
| 188 | else |
| 189 | outb(inb(ACBCTL1) & ~ACBCTL1_ACK, ACBCTL1); |
| 190 | |
Thomas Andrews | fd627a0 | 2006-07-01 17:05:12 +0200 | [diff] [blame] | 191 | if (iface->len == 1) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | iface->result = 0; |
| 193 | iface->state = state_idle; |
| 194 | outb(inb(ACBCTL1) | ACBCTL1_STOP, ACBCTL1); |
| 195 | } |
| 196 | |
Thomas Andrews | fd627a0 | 2006-07-01 17:05:12 +0200 | [diff] [blame] | 197 | *iface->ptr++ = inb(ACBSDA); |
| 198 | --iface->len; |
| 199 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | break; |
| 201 | |
| 202 | case state_write: |
| 203 | if (iface->len == 0) { |
| 204 | iface->result = 0; |
| 205 | iface->state = state_idle; |
| 206 | outb(inb(ACBCTL1) | ACBCTL1_STOP, ACBCTL1); |
| 207 | break; |
| 208 | } |
Ben Gardner | 99c3adb | 2006-01-18 22:41:50 +0100 | [diff] [blame] | 209 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | outb(*iface->ptr++, ACBSDA); |
| 211 | --iface->len; |
Ben Gardner | 99c3adb | 2006-01-18 22:41:50 +0100 | [diff] [blame] | 212 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | break; |
| 214 | } |
| 215 | |
| 216 | return; |
| 217 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | error: |
Willy Tarreau | fce96f3 | 2009-09-18 22:45:47 +0200 | [diff] [blame] | 219 | dev_err(&iface->adapter.dev, |
| 220 | "%s in state %s (addr=0x%02x, len=%d, status=0x%02x)\n", errmsg, |
| 221 | scx200_acb_state_name[iface->state], iface->address_byte, |
| 222 | iface->len, status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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: |
Ben Gardner | 99c3adb | 2006-01-18 22:41:50 +0100 | [diff] [blame] | 314 | len = data->block[0]; |
Jean Delvare | c3efaca | 2006-07-01 17:06:43 +0200 | [diff] [blame] | 315 | if (len == 0 || len > I2C_SMBUS_BLOCK_MAX) |
| 316 | return -EINVAL; |
Ben Gardner | 99c3adb | 2006-01-18 22:41:50 +0100 | [diff] [blame] | 317 | buffer = &data->block[1]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | break; |
Ben Gardner | 99c3adb | 2006-01-18 22:41:50 +0100 | [diff] [blame] | 319 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | default: |
Ben Gardner | 99c3adb | 2006-01-18 22:41:50 +0100 | [diff] [blame] | 321 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | } |
| 323 | |
Ben Gardner | ef4d927 | 2006-01-18 22:43:10 +0100 | [diff] [blame] | 324 | dev_dbg(&adapter->dev, |
| 325 | "size=%d, address=0x%x, command=0x%x, len=%d, read=%d\n", |
| 326 | size, address, command, len, rw); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | |
| 328 | if (!len && rw == I2C_SMBUS_READ) { |
Ben Gardner | ef4d927 | 2006-01-18 22:43:10 +0100 | [diff] [blame] | 329 | dev_dbg(&adapter->dev, "zero length read\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | return -EINVAL; |
| 331 | } |
| 332 | |
Jean Delvare | 3fb9a65 | 2006-01-18 23:17:01 +0100 | [diff] [blame] | 333 | mutex_lock(&iface->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | |
Ben Gardner | 9b7b6d3 | 2006-01-18 22:44:04 +0100 | [diff] [blame] | 335 | iface->address_byte = (address << 1) | rw; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | iface->command = command; |
| 337 | iface->ptr = buffer; |
| 338 | iface->len = len; |
| 339 | iface->result = -EINVAL; |
| 340 | iface->needs_reset = 0; |
| 341 | |
| 342 | outb(inb(ACBCTL1) | ACBCTL1_START, ACBCTL1); |
| 343 | |
| 344 | if (size == I2C_SMBUS_QUICK || size == I2C_SMBUS_BYTE) |
| 345 | iface->state = state_quick; |
| 346 | else |
| 347 | iface->state = state_address; |
| 348 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | while (iface->state != state_idle) |
| 350 | scx200_acb_poll(iface); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | |
| 352 | if (iface->needs_reset) |
| 353 | scx200_acb_reset(iface); |
| 354 | |
| 355 | rc = iface->result; |
| 356 | |
Jean Delvare | 3fb9a65 | 2006-01-18 23:17:01 +0100 | [diff] [blame] | 357 | mutex_unlock(&iface->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | |
| 359 | if (rc == 0 && size == I2C_SMBUS_WORD_DATA && rw == I2C_SMBUS_READ) |
Ben Gardner | 99c3adb | 2006-01-18 22:41:50 +0100 | [diff] [blame] | 360 | data->word = le16_to_cpu(cur_word); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | |
| 362 | #ifdef DEBUG |
Ben Gardner | ef4d927 | 2006-01-18 22:43:10 +0100 | [diff] [blame] | 363 | dev_dbg(&adapter->dev, "transfer done, result: %d", rc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | if (buffer) { |
| 365 | int i; |
| 366 | printk(" data:"); |
| 367 | for (i = 0; i < len; ++i) |
| 368 | printk(" %02x", buffer[i]); |
| 369 | } |
| 370 | printk("\n"); |
| 371 | #endif |
| 372 | |
| 373 | return rc; |
| 374 | } |
| 375 | |
| 376 | static u32 scx200_acb_func(struct i2c_adapter *adapter) |
| 377 | { |
| 378 | return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE | |
| 379 | I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA | |
Jean Delvare | c3efaca | 2006-07-01 17:06:43 +0200 | [diff] [blame] | 380 | I2C_FUNC_SMBUS_I2C_BLOCK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | /* For now, we only handle combined mode (smbus) */ |
Jean Delvare | 8f9082c | 2006-09-03 22:39:46 +0200 | [diff] [blame] | 384 | static const struct i2c_algorithm scx200_acb_algorithm = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | .smbus_xfer = scx200_acb_smbus_xfer, |
| 386 | .functionality = scx200_acb_func, |
| 387 | }; |
| 388 | |
| 389 | static struct scx200_acb_iface *scx200_acb_list; |
Matthias Kaehlcke | 9d9c01c | 2007-07-12 14:12:28 +0200 | [diff] [blame] | 390 | static DEFINE_MUTEX(scx200_acb_list_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | |
Andres Salomon | de8255c | 2010-12-30 20:27:33 -0800 | [diff] [blame] | 392 | static __devinit int scx200_acb_probe(struct scx200_acb_iface *iface) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | { |
| 394 | u8 val; |
| 395 | |
| 396 | /* Disable the ACCESS.bus device and Configure the SCL |
Ben Gardner | 99c3adb | 2006-01-18 22:41:50 +0100 | [diff] [blame] | 397 | frequency: 16 clock cycles */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | outb(0x70, ACBCTL2); |
| 399 | |
| 400 | if (inb(ACBCTL2) != 0x70) { |
Jim Cromie | 401e72b | 2012-10-05 22:23:52 +0200 | [diff] [blame] | 401 | pr_debug("ACBCTL2 readback failed\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | return -ENXIO; |
| 403 | } |
| 404 | |
| 405 | outb(inb(ACBCTL1) | ACBCTL1_NMINTE, ACBCTL1); |
| 406 | |
| 407 | val = inb(ACBCTL1); |
| 408 | if (val) { |
Jim Cromie | 401e72b | 2012-10-05 22:23:52 +0200 | [diff] [blame] | 409 | pr_debug("disabled, but ACBCTL1=0x%02x\n", 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) { |
Jim Cromie | 401e72b | 2012-10-05 22:23:52 +0200 | [diff] [blame] | 419 | pr_debug("enabled, but NMINTE won't be set, ACBCTL1=0x%02x\n", |
| 420 | val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | return -ENXIO; |
| 422 | } |
| 423 | |
| 424 | return 0; |
| 425 | } |
| 426 | |
Andres Salomon | de8255c | 2010-12-30 20:27:33 -0800 | [diff] [blame] | 427 | static __devinit 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) { |
Jim Cromie | 401e72b | 2012-10-05 22:23:52 +0200 | [diff] [blame] | 435 | pr_err("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; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | adapter->algo = &scx200_acb_algorithm; |
Jean Delvare | 3401b2f | 2008-07-14 22:38:29 +0200 | [diff] [blame] | 444 | adapter->class = I2C_CLASS_HWMON | I2C_CLASS_SPD; |
Jean Delvare | 12a917f | 2007-02-13 22:09:03 +0100 | [diff] [blame] | 445 | adapter->dev.parent = dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | |
Jean Delvare | 3fb9a65 | 2006-01-18 23:17:01 +0100 | [diff] [blame] | 447 | mutex_init(&iface->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | |
Jordan Crouse | 80cd3a8 | 2006-06-12 21:44:28 +0200 | [diff] [blame] | 449 | return iface; |
| 450 | } |
| 451 | |
Andres Salomon | de8255c | 2010-12-30 20:27:33 -0800 | [diff] [blame] | 452 | static int __devinit scx200_acb_create(struct scx200_acb_iface *iface) |
Jordan Crouse | 80cd3a8 | 2006-06-12 21:44:28 +0200 | [diff] [blame] | 453 | { |
| 454 | struct i2c_adapter *adapter; |
| 455 | int rc; |
| 456 | |
| 457 | adapter = &iface->adapter; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | |
| 459 | rc = scx200_acb_probe(iface); |
| 460 | if (rc) { |
Jim Cromie | 401e72b | 2012-10-05 22:23:52 +0200 | [diff] [blame] | 461 | pr_warn("probe failed\n"); |
Jordan Crouse | 80cd3a8 | 2006-06-12 21:44:28 +0200 | [diff] [blame] | 462 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | scx200_acb_reset(iface); |
| 466 | |
| 467 | if (i2c_add_adapter(adapter) < 0) { |
Jim Cromie | 401e72b | 2012-10-05 22:23:52 +0200 | [diff] [blame] | 468 | pr_err("failed to register\n"); |
Jordan Crouse | 80cd3a8 | 2006-06-12 21:44:28 +0200 | [diff] [blame] | 469 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | } |
| 471 | |
Andres Salomon | de8255c | 2010-12-30 20:27:33 -0800 | [diff] [blame] | 472 | if (!adapter->dev.parent) { |
| 473 | /* If there's no dev, we're tracking (ISA) ifaces manually */ |
| 474 | mutex_lock(&scx200_acb_list_mutex); |
| 475 | iface->next = scx200_acb_list; |
| 476 | scx200_acb_list = iface; |
| 477 | mutex_unlock(&scx200_acb_list_mutex); |
| 478 | } |
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 | |
Andres Salomon | de8255c | 2010-12-30 20:27:33 -0800 | [diff] [blame] | 483 | static struct scx200_acb_iface * __devinit scx200_create_dev(const char *text, |
| 484 | unsigned long base, int index, struct device *dev) |
Jordan Crouse | 80cd3a8 | 2006-06-12 21:44:28 +0200 | [diff] [blame] | 485 | { |
| 486 | struct scx200_acb_iface *iface; |
| 487 | int rc; |
| 488 | |
Andres Salomon | de8255c | 2010-12-30 20:27:33 -0800 | [diff] [blame] | 489 | iface = scx200_create_iface(text, dev, index); |
Jordan Crouse | 80cd3a8 | 2006-06-12 21:44:28 +0200 | [diff] [blame] | 490 | |
| 491 | if (iface == NULL) |
Andres Salomon | de8255c | 2010-12-30 20:27:33 -0800 | [diff] [blame] | 492 | return NULL; |
Jordan Crouse | 80cd3a8 | 2006-06-12 21:44:28 +0200 | [diff] [blame] | 493 | |
Adrian Bunk | dec1a99 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 494 | if (!request_region(base, 8, iface->adapter.name)) { |
Jim Cromie | 401e72b | 2012-10-05 22:23:52 +0200 | [diff] [blame] | 495 | pr_err("can't allocate io 0x%lx-0x%lx\n", base, base + 8 - 1); |
Jordan Crouse | 80cd3a8 | 2006-06-12 21:44:28 +0200 | [diff] [blame] | 496 | goto errout_free; |
Ben Gardner | 16ffc5c | 2006-01-18 22:48:26 +0100 | [diff] [blame] | 497 | } |
| 498 | |
Jordan Crouse | 80cd3a8 | 2006-06-12 21:44:28 +0200 | [diff] [blame] | 499 | iface->base = base; |
| 500 | rc = scx200_acb_create(iface); |
Ben Gardner | 16ffc5c | 2006-01-18 22:48:26 +0100 | [diff] [blame] | 501 | |
Jordan Crouse | 80cd3a8 | 2006-06-12 21:44:28 +0200 | [diff] [blame] | 502 | if (rc == 0) |
Andres Salomon | de8255c | 2010-12-30 20:27:33 -0800 | [diff] [blame] | 503 | return iface; |
Jordan Crouse | 80cd3a8 | 2006-06-12 21:44:28 +0200 | [diff] [blame] | 504 | |
| 505 | release_region(base, 8); |
| 506 | errout_free: |
| 507 | kfree(iface); |
Andres Salomon | de8255c | 2010-12-30 20:27:33 -0800 | [diff] [blame] | 508 | return NULL; |
Jordan Crouse | 80cd3a8 | 2006-06-12 21:44:28 +0200 | [diff] [blame] | 509 | } |
| 510 | |
Andres Salomon | de8255c | 2010-12-30 20:27:33 -0800 | [diff] [blame] | 511 | static int __devinit scx200_probe(struct platform_device *pdev) |
| 512 | { |
| 513 | struct scx200_acb_iface *iface; |
| 514 | struct resource *res; |
Jordan Crouse | 80cd3a8 | 2006-06-12 21:44:28 +0200 | [diff] [blame] | 515 | |
Andres Salomon | de8255c | 2010-12-30 20:27:33 -0800 | [diff] [blame] | 516 | res = platform_get_resource(pdev, IORESOURCE_IO, 0); |
| 517 | if (!res) { |
| 518 | dev_err(&pdev->dev, "can't fetch device resource info\n"); |
| 519 | return -ENODEV; |
| 520 | } |
| 521 | |
| 522 | iface = scx200_create_dev("CS5535", res->start, 0, &pdev->dev); |
| 523 | if (!iface) |
| 524 | return -EIO; |
| 525 | |
| 526 | dev_info(&pdev->dev, "SCx200 device '%s' registered\n", |
| 527 | iface->adapter.name); |
| 528 | platform_set_drvdata(pdev, iface); |
| 529 | |
| 530 | return 0; |
| 531 | } |
| 532 | |
| 533 | static void __devexit scx200_cleanup_iface(struct scx200_acb_iface *iface) |
| 534 | { |
| 535 | i2c_del_adapter(&iface->adapter); |
| 536 | release_region(iface->base, 8); |
| 537 | kfree(iface); |
| 538 | } |
| 539 | |
| 540 | static int __devexit scx200_remove(struct platform_device *pdev) |
| 541 | { |
| 542 | struct scx200_acb_iface *iface; |
| 543 | |
| 544 | iface = platform_get_drvdata(pdev); |
| 545 | platform_set_drvdata(pdev, NULL); |
| 546 | scx200_cleanup_iface(iface); |
| 547 | |
| 548 | return 0; |
| 549 | } |
| 550 | |
Harvey Yang | 6fcf84a | 2011-10-30 13:47:25 +0100 | [diff] [blame] | 551 | static struct platform_driver scx200_pci_driver = { |
Andres Salomon | de8255c | 2010-12-30 20:27:33 -0800 | [diff] [blame] | 552 | .driver = { |
| 553 | .name = "cs5535-smb", |
| 554 | .owner = THIS_MODULE, |
| 555 | }, |
| 556 | .probe = scx200_probe, |
| 557 | .remove = __devexit_p(scx200_remove), |
| 558 | }; |
| 559 | |
Axel Lin | 3527bd5 | 2012-01-12 20:32:04 +0100 | [diff] [blame] | 560 | static DEFINE_PCI_DEVICE_TABLE(scx200_isa) = { |
Andres Salomon | de8255c | 2010-12-30 20:27:33 -0800 | [diff] [blame] | 561 | { PCI_DEVICE(PCI_VENDOR_ID_NS, PCI_DEVICE_ID_NS_SCx200_BRIDGE) }, |
| 562 | { PCI_DEVICE(PCI_VENDOR_ID_NS, PCI_DEVICE_ID_NS_SC1100_BRIDGE) }, |
Dzianis Kahanovich | 87acf5a | 2010-10-27 20:33:05 -0600 | [diff] [blame] | 563 | { 0, } |
Jordan Crouse | 80cd3a8 | 2006-06-12 21:44:28 +0200 | [diff] [blame] | 564 | }; |
| 565 | |
Andres Salomon | de8255c | 2010-12-30 20:27:33 -0800 | [diff] [blame] | 566 | static __init void scx200_scan_isa(void) |
Jordan Crouse | 80cd3a8 | 2006-06-12 21:44:28 +0200 | [diff] [blame] | 567 | { |
Andres Salomon | de8255c | 2010-12-30 20:27:33 -0800 | [diff] [blame] | 568 | int i; |
Jordan Crouse | 80cd3a8 | 2006-06-12 21:44:28 +0200 | [diff] [blame] | 569 | |
Andres Salomon | de8255c | 2010-12-30 20:27:33 -0800 | [diff] [blame] | 570 | if (!pci_dev_present(scx200_isa)) |
| 571 | return; |
Jordan Crouse | 80cd3a8 | 2006-06-12 21:44:28 +0200 | [diff] [blame] | 572 | |
Andres Salomon | de8255c | 2010-12-30 20:27:33 -0800 | [diff] [blame] | 573 | for (i = 0; i < MAX_DEVICES; ++i) { |
| 574 | if (base[i] == 0) |
Jordan Crouse | 80cd3a8 | 2006-06-12 21:44:28 +0200 | [diff] [blame] | 575 | continue; |
| 576 | |
Andres Salomon | de8255c | 2010-12-30 20:27:33 -0800 | [diff] [blame] | 577 | /* XXX: should we care about failures? */ |
| 578 | scx200_create_dev("SCx200", base[i], i, NULL); |
Jordan Crouse | 80cd3a8 | 2006-06-12 21:44:28 +0200 | [diff] [blame] | 579 | } |
Ben Gardner | 16ffc5c | 2006-01-18 22:48:26 +0100 | [diff] [blame] | 580 | } |
| 581 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 582 | static int __init scx200_acb_init(void) |
| 583 | { |
Jim Cromie | 401e72b | 2012-10-05 22:23:52 +0200 | [diff] [blame] | 584 | pr_debug("NatSemi SCx200 ACCESS.bus Driver\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | |
Andres Salomon | de8255c | 2010-12-30 20:27:33 -0800 | [diff] [blame] | 586 | /* First scan for ISA-based devices */ |
| 587 | scx200_scan_isa(); /* XXX: should we care about errors? */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 588 | |
Jean Delvare | 6f9c296 | 2006-04-26 22:50:32 +0200 | [diff] [blame] | 589 | /* If at least one bus was created, init must succeed */ |
| 590 | if (scx200_acb_list) |
| 591 | return 0; |
Andres Salomon | de8255c | 2010-12-30 20:27:33 -0800 | [diff] [blame] | 592 | |
| 593 | /* No ISA devices; register the platform driver for PCI-based devices */ |
Harvey Yang | 6fcf84a | 2011-10-30 13:47:25 +0100 | [diff] [blame] | 594 | return platform_driver_register(&scx200_pci_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 595 | } |
| 596 | |
| 597 | static void __exit scx200_acb_cleanup(void) |
| 598 | { |
| 599 | struct scx200_acb_iface *iface; |
Ben Gardner | 99c3adb | 2006-01-18 22:41:50 +0100 | [diff] [blame] | 600 | |
Harvey Yang | 6fcf84a | 2011-10-30 13:47:25 +0100 | [diff] [blame] | 601 | platform_driver_unregister(&scx200_pci_driver); |
Andres Salomon | de8255c | 2010-12-30 20:27:33 -0800 | [diff] [blame] | 602 | |
Matthias Kaehlcke | 9d9c01c | 2007-07-12 14:12:28 +0200 | [diff] [blame] | 603 | mutex_lock(&scx200_acb_list_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 604 | while ((iface = scx200_acb_list) != NULL) { |
| 605 | scx200_acb_list = iface->next; |
Matthias Kaehlcke | 9d9c01c | 2007-07-12 14:12:28 +0200 | [diff] [blame] | 606 | mutex_unlock(&scx200_acb_list_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | |
Andres Salomon | de8255c | 2010-12-30 20:27:33 -0800 | [diff] [blame] | 608 | scx200_cleanup_iface(iface); |
Jordan Crouse | 80cd3a8 | 2006-06-12 21:44:28 +0200 | [diff] [blame] | 609 | |
Matthias Kaehlcke | 9d9c01c | 2007-07-12 14:12:28 +0200 | [diff] [blame] | 610 | mutex_lock(&scx200_acb_list_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 611 | } |
Matthias Kaehlcke | 9d9c01c | 2007-07-12 14:12:28 +0200 | [diff] [blame] | 612 | mutex_unlock(&scx200_acb_list_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 613 | } |
| 614 | |
| 615 | module_init(scx200_acb_init); |
| 616 | module_exit(scx200_acb_cleanup); |