1 /* linux/drivers/i2c/scx200_acb.c
3 Copyright (c) 2001,2002 Christer Weinigel <wingel@nano-system.com>
5 National Semiconductor SCx200 ACCESS.bus support
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>
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.
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.
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.
27 #include <linux/config.h>
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>
33 #include <linux/smp_lock.h>
34 #include <linux/pci.h>
35 #include <linux/delay.h>
38 #include <linux/scx200.h>
40 #define NAME "scx200_acb"
42 MODULE_AUTHOR("Christer Weinigel <wingel@nano-system.com>");
43 MODULE_DESCRIPTION("NatSemi SCx200 ACCESS.bus Driver");
44 MODULE_LICENSE("GPL");
47 static int base[MAX_DEVICES] = { 0x820, 0x840 };
49 module_param_array(base, int, num_base, 0);
50 MODULE_PARM_DESC(base, "Base addresses for the ACCESS.bus controllers");
53 #define DBG(x...) printk(KERN_DEBUG NAME ": " x)
58 /* The hardware supports interrupt driven mode too, but I haven't
61 #define POLL_TIMEOUT (HZ)
63 enum scx200_acb_state {
73 static const char *scx200_acb_state_name[] = {
83 /* Physical interface */
84 struct scx200_acb_iface
86 struct scx200_acb_iface *next;
87 struct i2c_adapter adapter;
91 /* State machine data */
92 enum scx200_acb_state state;
101 /* Register Definitions */
102 #define ACBSDA (iface->base + 0)
103 #define ACBST (iface->base + 1)
104 #define ACBST_SDAST 0x40 /* SDA Status */
105 #define ACBST_BER 0x20
106 #define ACBST_NEGACK 0x10 /* Negative Acknowledge */
107 #define ACBST_STASTR 0x08 /* Stall After Start */
108 #define ACBST_MASTER 0x02
109 #define ACBCST (iface->base + 2)
110 #define ACBCST_BB 0x02
111 #define ACBCTL1 (iface->base + 3)
112 #define ACBCTL1_STASTRE 0x80
113 #define ACBCTL1_NMINTE 0x40
114 #define ACBCTL1_ACK 0x10
115 #define ACBCTL1_STOP 0x02
116 #define ACBCTL1_START 0x01
117 #define ACBADDR (iface->base + 4)
118 #define ACBCTL2 (iface->base + 5)
119 #define ACBCTL2_ENABLE 0x01
121 /************************************************************************/
123 static void scx200_acb_machine(struct scx200_acb_iface *iface, u8 status)
127 DBG("state %s, status = 0x%02x\n",
128 scx200_acb_state_name[iface->state], status);
130 if (status & ACBST_BER) {
131 errmsg = "bus error";
134 if (!(status & ACBST_MASTER)) {
135 errmsg = "not master";
138 if (status & ACBST_NEGACK)
141 switch (iface->state) {
143 dev_warn(&iface->adapter.dev, "interrupt in idle state\n");
147 /* Do a pointer write first */
148 outb(iface->address_byte & ~1, ACBSDA);
150 iface->state = state_command;
154 outb(iface->command, ACBSDA);
156 if (iface->address_byte & 1)
157 iface->state = state_repeat_start;
159 iface->state = state_write;
162 case state_repeat_start:
163 outb(inb(ACBCTL1) | ACBCTL1_START, ACBCTL1);
167 if (iface->address_byte & 1) {
169 outb(inb(ACBCTL1) | ACBCTL1_ACK, ACBCTL1);
171 outb(inb(ACBCTL1) & ~ACBCTL1_ACK, ACBCTL1);
172 outb(iface->address_byte, ACBSDA);
174 iface->state = state_read;
176 outb(iface->address_byte, ACBSDA);
178 iface->state = state_write;
183 /* Set ACK if receiving the last byte */
185 outb(inb(ACBCTL1) | ACBCTL1_ACK, ACBCTL1);
187 outb(inb(ACBCTL1) & ~ACBCTL1_ACK, ACBCTL1);
189 *iface->ptr++ = inb(ACBSDA);
192 if (iface->len == 0) {
194 iface->state = state_idle;
195 outb(inb(ACBCTL1) | ACBCTL1_STOP, ACBCTL1);
201 if (iface->len == 0) {
203 iface->state = state_idle;
204 outb(inb(ACBCTL1) | ACBCTL1_STOP, ACBCTL1);
208 outb(*iface->ptr++, ACBSDA);
217 DBG("negative acknowledge in state %s\n",
218 scx200_acb_state_name[iface->state]);
220 iface->state = state_idle;
221 iface->result = -ENXIO;
223 outb(inb(ACBCTL1) | ACBCTL1_STOP, ACBCTL1);
224 outb(ACBST_STASTR | ACBST_NEGACK, ACBST);
228 dev_err(&iface->adapter.dev, "%s in state %s\n", errmsg,
229 scx200_acb_state_name[iface->state]);
231 iface->state = state_idle;
232 iface->result = -EIO;
233 iface->needs_reset = 1;
236 static void scx200_acb_timeout(struct scx200_acb_iface *iface)
238 dev_err(&iface->adapter.dev, "timeout in state %s\n",
239 scx200_acb_state_name[iface->state]);
241 iface->state = state_idle;
242 iface->result = -EIO;
243 iface->needs_reset = 1;
247 static void scx200_acb_poll(struct scx200_acb_iface *iface)
250 unsigned long timeout;
252 timeout = jiffies + POLL_TIMEOUT;
253 while (time_before(jiffies, timeout)) {
255 if ((status & (ACBST_SDAST|ACBST_BER|ACBST_NEGACK)) != 0) {
256 scx200_acb_machine(iface, status);
262 scx200_acb_timeout(iface);
264 #endif /* POLLED_MODE */
266 static void scx200_acb_reset(struct scx200_acb_iface *iface)
268 /* Disable the ACCESS.bus device and Configure the SCL
269 frequency: 16 clock cycles */
273 /* Disable slave address */
275 /* Enable the ACCESS.bus device */
276 outb(inb(ACBCTL2) | ACBCTL2_ENABLE, ACBCTL2);
277 /* Free STALL after START */
278 outb(inb(ACBCTL1) & ~(ACBCTL1_STASTRE | ACBCTL1_NMINTE), ACBCTL1);
280 outb(inb(ACBCTL1) | ACBCTL1_STOP, ACBCTL1);
281 /* Clear BER, NEGACK and STASTR bits */
282 outb(ACBST_BER | ACBST_NEGACK | ACBST_STASTR, ACBST);
284 outb(inb(ACBCST) | ACBCST_BB, ACBCST);
287 static s32 scx200_acb_smbus_xfer(struct i2c_adapter *adapter,
288 u16 address, unsigned short flags,
289 char rw, u8 command, int size,
290 union i2c_smbus_data *data)
292 struct scx200_acb_iface *iface = i2c_get_adapdata(adapter);
299 case I2C_SMBUS_QUICK:
304 if (rw == I2C_SMBUS_READ) {
306 buffer = &data->byte;
312 case I2C_SMBUS_BYTE_DATA:
314 buffer = &data->byte;
316 case I2C_SMBUS_WORD_DATA:
318 cur_word = cpu_to_le16(data->word);
319 buffer = (u8 *)&cur_word;
321 case I2C_SMBUS_BLOCK_DATA:
322 len = data->block[0];
323 buffer = &data->block[1];
329 DBG("size=%d, address=0x%x, command=0x%x, len=%d, read=%d\n",
330 size, address, command, len, rw == I2C_SMBUS_READ);
332 if (!len && rw == I2C_SMBUS_READ) {
333 dev_warn(&adapter->dev, "zero length read\n");
337 if (len && !buffer) {
338 dev_warn(&adapter->dev, "nonzero length but no buffer\n");
344 iface->address_byte = address<<1;
345 if (rw == I2C_SMBUS_READ)
346 iface->address_byte |= 1;
347 iface->command = command;
350 iface->result = -EINVAL;
351 iface->needs_reset = 0;
353 outb(inb(ACBCTL1) | ACBCTL1_START, ACBCTL1);
355 if (size == I2C_SMBUS_QUICK || size == I2C_SMBUS_BYTE)
356 iface->state = state_quick;
358 iface->state = state_address;
361 while (iface->state != state_idle)
362 scx200_acb_poll(iface);
363 #else /* POLLED_MODE */
364 #error Interrupt driven mode not implemented
365 #endif /* POLLED_MODE */
367 if (iface->needs_reset)
368 scx200_acb_reset(iface);
374 if (rc == 0 && size == I2C_SMBUS_WORD_DATA && rw == I2C_SMBUS_READ)
375 data->word = le16_to_cpu(cur_word);
378 DBG(": transfer done, result: %d", rc);
382 for (i = 0; i < len; ++i)
383 printk(" %02x", buffer[i]);
391 static u32 scx200_acb_func(struct i2c_adapter *adapter)
393 return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
394 I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
395 I2C_FUNC_SMBUS_BLOCK_DATA;
398 /* For now, we only handle combined mode (smbus) */
399 static struct i2c_algorithm scx200_acb_algorithm = {
400 .name = "NatSemi SCx200 ACCESS.bus",
401 .id = I2C_ALGO_SMBUS,
402 .smbus_xfer = scx200_acb_smbus_xfer,
403 .functionality = scx200_acb_func,
406 struct scx200_acb_iface *scx200_acb_list;
408 int scx200_acb_probe(struct scx200_acb_iface *iface)
412 /* Disable the ACCESS.bus device and Configure the SCL
413 frequency: 16 clock cycles */
416 if (inb(ACBCTL2) != 0x70) {
417 DBG("ACBCTL2 readback failed\n");
421 outb(inb(ACBCTL1) | ACBCTL1_NMINTE, ACBCTL1);
425 DBG("disabled, but ACBCTL1=0x%02x\n", val);
429 outb(inb(ACBCTL2) | ACBCTL2_ENABLE, ACBCTL2);
431 outb(inb(ACBCTL1) | ACBCTL1_NMINTE, ACBCTL1);
434 if ((val & ACBCTL1_NMINTE) != ACBCTL1_NMINTE) {
435 DBG("enabled, but NMINTE won't be set, ACBCTL1=0x%02x\n", val);
442 static int __init scx200_acb_create(int base, int index)
444 struct scx200_acb_iface *iface;
445 struct i2c_adapter *adapter;
447 char description[64];
449 iface = kmalloc(sizeof(*iface), GFP_KERNEL);
451 printk(KERN_ERR NAME ": can't allocate memory\n");
456 memset(iface, 0, sizeof(*iface));
457 adapter = &iface->adapter;
458 i2c_set_adapdata(adapter, iface);
459 snprintf(adapter->name, I2C_NAME_SIZE, "SCx200 ACB%d", index);
460 adapter->owner = THIS_MODULE;
461 adapter->id = I2C_ALGO_SMBUS;
462 adapter->algo = &scx200_acb_algorithm;
463 adapter->class = I2C_CLASS_HWMON;
465 init_MUTEX(&iface->sem);
467 snprintf(description, sizeof(description), "NatSemi SCx200 ACCESS.bus [%s]", adapter->name);
468 if (request_region(base, 8, description) == 0) {
469 dev_err(&adapter->dev, "can't allocate io 0x%x-0x%x\n",
476 rc = scx200_acb_probe(iface);
478 dev_warn(&adapter->dev, "probe failed\n");
482 scx200_acb_reset(iface);
484 if (i2c_add_adapter(adapter) < 0) {
485 dev_err(&adapter->dev, "failed to register\n");
491 iface->next = scx200_acb_list;
492 scx200_acb_list = iface;
500 release_region(iface->base, 8);
506 static int __init scx200_acb_init(void)
511 pr_debug(NAME ": NatSemi SCx200 ACCESS.bus Driver\n");
513 /* Verify that this really is a SCx200 processor */
514 if (pci_find_device(PCI_VENDOR_ID_NS,
515 PCI_DEVICE_ID_NS_SCx200_BRIDGE,
517 && pci_find_device(PCI_VENDOR_ID_NS,
518 PCI_DEVICE_ID_NS_SC1100_BRIDGE,
523 for (i = 0; i < MAX_DEVICES; ++i) {
525 rc = scx200_acb_create(base[i], i);
532 static void __exit scx200_acb_cleanup(void)
534 struct scx200_acb_iface *iface;
536 while ((iface = scx200_acb_list) != NULL) {
537 scx200_acb_list = iface->next;
540 i2c_del_adapter(&iface->adapter);
541 release_region(iface->base, 8);
548 module_init(scx200_acb_init);
549 module_exit(scx200_acb_cleanup);
553 compile-command: "make -k -C ../.. SUBDIRS=drivers/i2c modules"