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