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 };
48 module_param_array(base, int, NULL, 0);
49 MODULE_PARM_DESC(base, "Base addresses for the ACCESS.bus controllers");
52 #define DBG(x...) printk(KERN_DEBUG NAME ": " x)
57 /* The hardware supports interrupt driven mode too, but I haven't
60 #define POLL_TIMEOUT (HZ)
62 enum scx200_acb_state {
72 static const char *scx200_acb_state_name[] = {
82 /* Physical interface */
83 struct scx200_acb_iface
85 struct scx200_acb_iface *next;
86 struct i2c_adapter adapter;
90 /* State machine data */
91 enum scx200_acb_state state;
100 /* Register Definitions */
101 #define ACBSDA (iface->base + 0)
102 #define ACBST (iface->base + 1)
103 #define ACBST_SDAST 0x40 /* SDA Status */
104 #define ACBST_BER 0x20
105 #define ACBST_NEGACK 0x10 /* Negative Acknowledge */
106 #define ACBST_STASTR 0x08 /* Stall After Start */
107 #define ACBST_MASTER 0x02
108 #define ACBCST (iface->base + 2)
109 #define ACBCST_BB 0x02
110 #define ACBCTL1 (iface->base + 3)
111 #define ACBCTL1_STASTRE 0x80
112 #define ACBCTL1_NMINTE 0x40
113 #define ACBCTL1_ACK 0x10
114 #define ACBCTL1_STOP 0x02
115 #define ACBCTL1_START 0x01
116 #define ACBADDR (iface->base + 4)
117 #define ACBCTL2 (iface->base + 5)
118 #define ACBCTL2_ENABLE 0x01
120 /************************************************************************/
122 static void scx200_acb_machine(struct scx200_acb_iface *iface, u8 status)
126 DBG("state %s, status = 0x%02x\n",
127 scx200_acb_state_name[iface->state], status);
129 if (status & ACBST_BER) {
130 errmsg = "bus error";
133 if (!(status & ACBST_MASTER)) {
134 errmsg = "not master";
137 if (status & ACBST_NEGACK)
140 switch (iface->state) {
142 dev_warn(&iface->adapter.dev, "interrupt in idle state\n");
146 /* Do a pointer write first */
147 outb(iface->address_byte & ~1, ACBSDA);
149 iface->state = state_command;
153 outb(iface->command, ACBSDA);
155 if (iface->address_byte & 1)
156 iface->state = state_repeat_start;
158 iface->state = state_write;
161 case state_repeat_start:
162 outb(inb(ACBCTL1) | ACBCTL1_START, ACBCTL1);
166 if (iface->address_byte & 1) {
168 outb(inb(ACBCTL1) | ACBCTL1_ACK, ACBCTL1);
170 outb(inb(ACBCTL1) & ~ACBCTL1_ACK, ACBCTL1);
171 outb(iface->address_byte, ACBSDA);
173 iface->state = state_read;
175 outb(iface->address_byte, ACBSDA);
177 iface->state = state_write;
182 /* Set ACK if receiving the last byte */
184 outb(inb(ACBCTL1) | ACBCTL1_ACK, ACBCTL1);
186 outb(inb(ACBCTL1) & ~ACBCTL1_ACK, ACBCTL1);
188 *iface->ptr++ = inb(ACBSDA);
191 if (iface->len == 0) {
193 iface->state = state_idle;
194 outb(inb(ACBCTL1) | ACBCTL1_STOP, ACBCTL1);
200 if (iface->len == 0) {
202 iface->state = state_idle;
203 outb(inb(ACBCTL1) | ACBCTL1_STOP, ACBCTL1);
207 outb(*iface->ptr++, ACBSDA);
216 DBG("negative acknowledge in state %s\n",
217 scx200_acb_state_name[iface->state]);
219 iface->state = state_idle;
220 iface->result = -ENXIO;
222 outb(inb(ACBCTL1) | ACBCTL1_STOP, ACBCTL1);
223 outb(ACBST_STASTR | ACBST_NEGACK, ACBST);
227 dev_err(&iface->adapter.dev, "%s in state %s\n", errmsg,
228 scx200_acb_state_name[iface->state]);
230 iface->state = state_idle;
231 iface->result = -EIO;
232 iface->needs_reset = 1;
235 static void scx200_acb_timeout(struct scx200_acb_iface *iface)
237 dev_err(&iface->adapter.dev, "timeout in state %s\n",
238 scx200_acb_state_name[iface->state]);
240 iface->state = state_idle;
241 iface->result = -EIO;
242 iface->needs_reset = 1;
246 static void scx200_acb_poll(struct scx200_acb_iface *iface)
249 unsigned long timeout;
251 timeout = jiffies + POLL_TIMEOUT;
252 while (time_before(jiffies, timeout)) {
254 if ((status & (ACBST_SDAST|ACBST_BER|ACBST_NEGACK)) != 0) {
255 scx200_acb_machine(iface, status);
261 scx200_acb_timeout(iface);
263 #endif /* POLLED_MODE */
265 static void scx200_acb_reset(struct scx200_acb_iface *iface)
267 /* Disable the ACCESS.bus device and Configure the SCL
268 frequency: 16 clock cycles */
272 /* Disable slave address */
274 /* Enable the ACCESS.bus device */
275 outb(inb(ACBCTL2) | ACBCTL2_ENABLE, ACBCTL2);
276 /* Free STALL after START */
277 outb(inb(ACBCTL1) & ~(ACBCTL1_STASTRE | ACBCTL1_NMINTE), ACBCTL1);
279 outb(inb(ACBCTL1) | ACBCTL1_STOP, ACBCTL1);
280 /* Clear BER, NEGACK and STASTR bits */
281 outb(ACBST_BER | ACBST_NEGACK | ACBST_STASTR, ACBST);
283 outb(inb(ACBCST) | ACBCST_BB, ACBCST);
286 static s32 scx200_acb_smbus_xfer(struct i2c_adapter *adapter,
287 u16 address, unsigned short flags,
288 char rw, u8 command, int size,
289 union i2c_smbus_data *data)
291 struct scx200_acb_iface *iface = i2c_get_adapdata(adapter);
298 case I2C_SMBUS_QUICK:
303 if (rw == I2C_SMBUS_READ) {
305 buffer = &data->byte;
311 case I2C_SMBUS_BYTE_DATA:
313 buffer = &data->byte;
315 case I2C_SMBUS_WORD_DATA:
317 cur_word = cpu_to_le16(data->word);
318 buffer = (u8 *)&cur_word;
320 case I2C_SMBUS_BLOCK_DATA:
321 len = data->block[0];
322 buffer = &data->block[1];
328 DBG("size=%d, address=0x%x, command=0x%x, len=%d, read=%d\n",
329 size, address, command, len, rw == I2C_SMBUS_READ);
331 if (!len && rw == I2C_SMBUS_READ) {
332 dev_warn(&adapter->dev, "zero length read\n");
336 if (len && !buffer) {
337 dev_warn(&adapter->dev, "nonzero length but no buffer\n");
343 iface->address_byte = address<<1;
344 if (rw == I2C_SMBUS_READ)
345 iface->address_byte |= 1;
346 iface->command = command;
349 iface->result = -EINVAL;
350 iface->needs_reset = 0;
352 outb(inb(ACBCTL1) | ACBCTL1_START, ACBCTL1);
354 if (size == I2C_SMBUS_QUICK || size == I2C_SMBUS_BYTE)
355 iface->state = state_quick;
357 iface->state = state_address;
360 while (iface->state != state_idle)
361 scx200_acb_poll(iface);
362 #else /* POLLED_MODE */
363 #error Interrupt driven mode not implemented
364 #endif /* POLLED_MODE */
366 if (iface->needs_reset)
367 scx200_acb_reset(iface);
373 if (rc == 0 && size == I2C_SMBUS_WORD_DATA && rw == I2C_SMBUS_READ)
374 data->word = le16_to_cpu(cur_word);
377 DBG(": transfer done, result: %d", rc);
381 for (i = 0; i < len; ++i)
382 printk(" %02x", buffer[i]);
390 static u32 scx200_acb_func(struct i2c_adapter *adapter)
392 return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
393 I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
394 I2C_FUNC_SMBUS_BLOCK_DATA;
397 /* For now, we only handle combined mode (smbus) */
398 static struct i2c_algorithm scx200_acb_algorithm = {
399 .name = "NatSemi SCx200 ACCESS.bus",
400 .id = I2C_ALGO_SMBUS,
401 .smbus_xfer = scx200_acb_smbus_xfer,
402 .functionality = scx200_acb_func,
405 static struct scx200_acb_iface *scx200_acb_list;
407 static int scx200_acb_probe(struct scx200_acb_iface *iface)
411 /* Disable the ACCESS.bus device and Configure the SCL
412 frequency: 16 clock cycles */
415 if (inb(ACBCTL2) != 0x70) {
416 DBG("ACBCTL2 readback failed\n");
420 outb(inb(ACBCTL1) | ACBCTL1_NMINTE, ACBCTL1);
424 DBG("disabled, but ACBCTL1=0x%02x\n", val);
428 outb(inb(ACBCTL2) | ACBCTL2_ENABLE, ACBCTL2);
430 outb(inb(ACBCTL1) | ACBCTL1_NMINTE, ACBCTL1);
433 if ((val & ACBCTL1_NMINTE) != ACBCTL1_NMINTE) {
434 DBG("enabled, but NMINTE won't be set, ACBCTL1=0x%02x\n", val);
441 static int __init scx200_acb_create(int base, int index)
443 struct scx200_acb_iface *iface;
444 struct i2c_adapter *adapter;
446 char description[64];
448 iface = kmalloc(sizeof(*iface), GFP_KERNEL);
450 printk(KERN_ERR NAME ": can't allocate memory\n");
455 memset(iface, 0, sizeof(*iface));
456 adapter = &iface->adapter;
457 i2c_set_adapdata(adapter, iface);
458 snprintf(adapter->name, I2C_NAME_SIZE, "SCx200 ACB%d", index);
459 adapter->owner = THIS_MODULE;
460 adapter->id = I2C_ALGO_SMBUS;
461 adapter->algo = &scx200_acb_algorithm;
462 adapter->class = I2C_CLASS_HWMON;
464 init_MUTEX(&iface->sem);
466 snprintf(description, sizeof(description), "NatSemi SCx200 ACCESS.bus [%s]", adapter->name);
467 if (request_region(base, 8, description) == 0) {
468 dev_err(&adapter->dev, "can't allocate io 0x%x-0x%x\n",
475 rc = scx200_acb_probe(iface);
477 dev_warn(&adapter->dev, "probe failed\n");
481 scx200_acb_reset(iface);
483 if (i2c_add_adapter(adapter) < 0) {
484 dev_err(&adapter->dev, "failed to register\n");
490 iface->next = scx200_acb_list;
491 scx200_acb_list = iface;
499 release_region(iface->base, 8);
505 static struct pci_device_id scx200[] = {
506 { PCI_DEVICE(PCI_VENDOR_ID_NS, PCI_DEVICE_ID_NS_SCx200_BRIDGE) },
507 { PCI_DEVICE(PCI_VENDOR_ID_NS, PCI_DEVICE_ID_NS_SC1100_BRIDGE) },
511 static int __init scx200_acb_init(void)
516 pr_debug(NAME ": NatSemi SCx200 ACCESS.bus Driver\n");
518 /* Verify that this really is a SCx200 processor */
519 if (pci_dev_present(scx200) == 0)
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"