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/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>
37 #include <linux/scx200.h>
39 #define NAME "scx200_acb"
41 MODULE_AUTHOR("Christer Weinigel <wingel@nano-system.com>");
42 MODULE_DESCRIPTION("NatSemi SCx200 ACCESS.bus Driver");
43 MODULE_LICENSE("GPL");
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");
51 #define DBG(x...) printk(KERN_DEBUG NAME ": " x)
56 /* The hardware supports interrupt driven mode too, but I haven't
59 #define POLL_TIMEOUT (HZ)
61 enum scx200_acb_state {
71 static const char *scx200_acb_state_name[] = {
81 /* Physical interface */
82 struct scx200_acb_iface
84 struct scx200_acb_iface *next;
85 struct i2c_adapter adapter;
89 /* State machine data */
90 enum scx200_acb_state state;
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
119 /************************************************************************/
121 static void scx200_acb_machine(struct scx200_acb_iface *iface, u8 status)
125 DBG("state %s, status = 0x%02x\n",
126 scx200_acb_state_name[iface->state], status);
128 if (status & ACBST_BER) {
129 errmsg = "bus error";
132 if (!(status & ACBST_MASTER)) {
133 errmsg = "not master";
136 if (status & ACBST_NEGACK)
139 switch (iface->state) {
141 dev_warn(&iface->adapter.dev, "interrupt in idle state\n");
145 /* Do a pointer write first */
146 outb(iface->address_byte & ~1, ACBSDA);
148 iface->state = state_command;
152 outb(iface->command, ACBSDA);
154 if (iface->address_byte & 1)
155 iface->state = state_repeat_start;
157 iface->state = state_write;
160 case state_repeat_start:
161 outb(inb(ACBCTL1) | ACBCTL1_START, ACBCTL1);
165 if (iface->address_byte & 1) {
167 outb(inb(ACBCTL1) | ACBCTL1_ACK, ACBCTL1);
169 outb(inb(ACBCTL1) & ~ACBCTL1_ACK, ACBCTL1);
170 outb(iface->address_byte, ACBSDA);
172 iface->state = state_read;
174 outb(iface->address_byte, ACBSDA);
176 iface->state = state_write;
181 /* Set ACK if receiving the last byte */
183 outb(inb(ACBCTL1) | ACBCTL1_ACK, ACBCTL1);
185 outb(inb(ACBCTL1) & ~ACBCTL1_ACK, ACBCTL1);
187 *iface->ptr++ = inb(ACBSDA);
190 if (iface->len == 0) {
192 iface->state = state_idle;
193 outb(inb(ACBCTL1) | ACBCTL1_STOP, ACBCTL1);
199 if (iface->len == 0) {
201 iface->state = state_idle;
202 outb(inb(ACBCTL1) | ACBCTL1_STOP, ACBCTL1);
206 outb(*iface->ptr++, ACBSDA);
215 DBG("negative acknowledge in state %s\n",
216 scx200_acb_state_name[iface->state]);
218 iface->state = state_idle;
219 iface->result = -ENXIO;
221 outb(inb(ACBCTL1) | ACBCTL1_STOP, ACBCTL1);
222 outb(ACBST_STASTR | ACBST_NEGACK, ACBST);
226 dev_err(&iface->adapter.dev, "%s in state %s\n", errmsg,
227 scx200_acb_state_name[iface->state]);
229 iface->state = state_idle;
230 iface->result = -EIO;
231 iface->needs_reset = 1;
234 static void scx200_acb_timeout(struct scx200_acb_iface *iface)
236 dev_err(&iface->adapter.dev, "timeout in state %s\n",
237 scx200_acb_state_name[iface->state]);
239 iface->state = state_idle;
240 iface->result = -EIO;
241 iface->needs_reset = 1;
245 static void scx200_acb_poll(struct scx200_acb_iface *iface)
248 unsigned long timeout;
250 timeout = jiffies + POLL_TIMEOUT;
251 while (time_before(jiffies, timeout)) {
253 if ((status & (ACBST_SDAST|ACBST_BER|ACBST_NEGACK)) != 0) {
254 scx200_acb_machine(iface, status);
260 scx200_acb_timeout(iface);
262 #endif /* POLLED_MODE */
264 static void scx200_acb_reset(struct scx200_acb_iface *iface)
266 /* Disable the ACCESS.bus device and Configure the SCL
267 frequency: 16 clock cycles */
271 /* Disable slave address */
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);
278 outb(inb(ACBCTL1) | ACBCTL1_STOP, ACBCTL1);
279 /* Clear BER, NEGACK and STASTR bits */
280 outb(ACBST_BER | ACBST_NEGACK | ACBST_STASTR, ACBST);
282 outb(inb(ACBCST) | ACBCST_BB, ACBCST);
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)
290 struct scx200_acb_iface *iface = i2c_get_adapdata(adapter);
297 case I2C_SMBUS_QUICK:
302 if (rw == I2C_SMBUS_READ) {
304 buffer = &data->byte;
310 case I2C_SMBUS_BYTE_DATA:
312 buffer = &data->byte;
314 case I2C_SMBUS_WORD_DATA:
316 cur_word = cpu_to_le16(data->word);
317 buffer = (u8 *)&cur_word;
319 case I2C_SMBUS_BLOCK_DATA:
320 len = data->block[0];
321 buffer = &data->block[1];
327 DBG("size=%d, address=0x%x, command=0x%x, len=%d, read=%d\n",
328 size, address, command, len, rw == I2C_SMBUS_READ);
330 if (!len && rw == I2C_SMBUS_READ) {
331 dev_warn(&adapter->dev, "zero length read\n");
335 if (len && !buffer) {
336 dev_warn(&adapter->dev, "nonzero length but no buffer\n");
342 iface->address_byte = address<<1;
343 if (rw == I2C_SMBUS_READ)
344 iface->address_byte |= 1;
345 iface->command = command;
348 iface->result = -EINVAL;
349 iface->needs_reset = 0;
351 outb(inb(ACBCTL1) | ACBCTL1_START, ACBCTL1);
353 if (size == I2C_SMBUS_QUICK || size == I2C_SMBUS_BYTE)
354 iface->state = state_quick;
356 iface->state = state_address;
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 */
365 if (iface->needs_reset)
366 scx200_acb_reset(iface);
372 if (rc == 0 && size == I2C_SMBUS_WORD_DATA && rw == I2C_SMBUS_READ)
373 data->word = le16_to_cpu(cur_word);
376 DBG(": transfer done, result: %d", rc);
380 for (i = 0; i < len; ++i)
381 printk(" %02x", buffer[i]);
389 static u32 scx200_acb_func(struct i2c_adapter *adapter)
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;
396 /* For now, we only handle combined mode (smbus) */
397 static struct i2c_algorithm scx200_acb_algorithm = {
398 .name = "NatSemi SCx200 ACCESS.bus",
399 .id = I2C_ALGO_SMBUS,
400 .smbus_xfer = scx200_acb_smbus_xfer,
401 .functionality = scx200_acb_func,
404 static struct scx200_acb_iface *scx200_acb_list;
406 static int scx200_acb_probe(struct scx200_acb_iface *iface)
410 /* Disable the ACCESS.bus device and Configure the SCL
411 frequency: 16 clock cycles */
414 if (inb(ACBCTL2) != 0x70) {
415 DBG("ACBCTL2 readback failed\n");
419 outb(inb(ACBCTL1) | ACBCTL1_NMINTE, ACBCTL1);
423 DBG("disabled, but ACBCTL1=0x%02x\n", val);
427 outb(inb(ACBCTL2) | ACBCTL2_ENABLE, ACBCTL2);
429 outb(inb(ACBCTL1) | ACBCTL1_NMINTE, ACBCTL1);
432 if ((val & ACBCTL1_NMINTE) != ACBCTL1_NMINTE) {
433 DBG("enabled, but NMINTE won't be set, ACBCTL1=0x%02x\n", val);
440 static int __init scx200_acb_create(int base, int index)
442 struct scx200_acb_iface *iface;
443 struct i2c_adapter *adapter;
445 char description[64];
447 iface = kmalloc(sizeof(*iface), GFP_KERNEL);
449 printk(KERN_ERR NAME ": can't allocate memory\n");
454 memset(iface, 0, sizeof(*iface));
455 adapter = &iface->adapter;
456 i2c_set_adapdata(adapter, iface);
457 snprintf(adapter->name, I2C_NAME_SIZE, "SCx200 ACB%d", index);
458 adapter->owner = THIS_MODULE;
459 adapter->id = I2C_ALGO_SMBUS;
460 adapter->algo = &scx200_acb_algorithm;
461 adapter->class = I2C_CLASS_HWMON;
463 init_MUTEX(&iface->sem);
465 snprintf(description, sizeof(description), "NatSemi SCx200 ACCESS.bus [%s]", adapter->name);
466 if (request_region(base, 8, description) == 0) {
467 dev_err(&adapter->dev, "can't allocate io 0x%x-0x%x\n",
474 rc = scx200_acb_probe(iface);
476 dev_warn(&adapter->dev, "probe failed\n");
480 scx200_acb_reset(iface);
482 if (i2c_add_adapter(adapter) < 0) {
483 dev_err(&adapter->dev, "failed to register\n");
489 iface->next = scx200_acb_list;
490 scx200_acb_list = iface;
498 release_region(iface->base, 8);
504 static struct pci_device_id scx200[] = {
505 { PCI_DEVICE(PCI_VENDOR_ID_NS, PCI_DEVICE_ID_NS_SCx200_BRIDGE) },
506 { PCI_DEVICE(PCI_VENDOR_ID_NS, PCI_DEVICE_ID_NS_SC1100_BRIDGE) },
510 static int __init scx200_acb_init(void)
515 pr_debug(NAME ": NatSemi SCx200 ACCESS.bus Driver\n");
517 /* Verify that this really is a SCx200 processor */
518 if (pci_dev_present(scx200) == 0)
522 for (i = 0; i < MAX_DEVICES; ++i) {
524 rc = scx200_acb_create(base[i], i);
531 static void __exit scx200_acb_cleanup(void)
533 struct scx200_acb_iface *iface;
535 while ((iface = scx200_acb_list) != NULL) {
536 scx200_acb_list = iface->next;
539 i2c_del_adapter(&iface->adapter);
540 release_region(iface->base, 8);
547 module_init(scx200_acb_init);
548 module_exit(scx200_acb_cleanup);
552 compile-command: "make -k -C ../.. SUBDIRS=drivers/i2c modules"