2 Copyright (c) 2001,2002 Christer Weinigel <wingel@nano-system.com>
4 National Semiconductor SCx200 ACCESS.bus support
6 Based on i2c-keywest.c which is:
7 Copyright (c) 2001 Benjamin Herrenschmidt <benh@kernel.crashing.org>
8 Copyright (c) 2000 Philip Edelbrock <phil@stimpy.netroedge.com>
10 This program is free software; you can redistribute it and/or
11 modify it under the terms of the GNU General Public License as
12 published by the Free Software Foundation; either version 2 of the
13 License, or (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include <linux/module.h>
26 #include <linux/errno.h>
27 #include <linux/kernel.h>
28 #include <linux/init.h>
29 #include <linux/i2c.h>
30 #include <linux/smp_lock.h>
31 #include <linux/pci.h>
32 #include <linux/delay.h>
35 #include <linux/scx200.h>
37 #define NAME "scx200_acb"
39 MODULE_AUTHOR("Christer Weinigel <wingel@nano-system.com>");
40 MODULE_DESCRIPTION("NatSemi SCx200 ACCESS.bus Driver");
41 MODULE_LICENSE("GPL");
44 static int base[MAX_DEVICES] = { 0x820, 0x840 };
45 module_param_array(base, int, NULL, 0);
46 MODULE_PARM_DESC(base, "Base addresses for the ACCESS.bus controllers");
48 /* The hardware supports interrupt driven mode too, but I haven't
51 #define POLL_TIMEOUT (HZ)
53 enum scx200_acb_state {
63 static const char *scx200_acb_state_name[] = {
73 /* Physical interface */
74 struct scx200_acb_iface {
75 struct scx200_acb_iface *next;
76 struct i2c_adapter adapter;
80 /* State machine data */
81 enum scx200_acb_state state;
90 /* Register Definitions */
91 #define ACBSDA (iface->base + 0)
92 #define ACBST (iface->base + 1)
93 #define ACBST_SDAST 0x40 /* SDA Status */
94 #define ACBST_BER 0x20
95 #define ACBST_NEGACK 0x10 /* Negative Acknowledge */
96 #define ACBST_STASTR 0x08 /* Stall After Start */
97 #define ACBST_MASTER 0x02
98 #define ACBCST (iface->base + 2)
99 #define ACBCST_BB 0x02
100 #define ACBCTL1 (iface->base + 3)
101 #define ACBCTL1_STASTRE 0x80
102 #define ACBCTL1_NMINTE 0x40
103 #define ACBCTL1_ACK 0x10
104 #define ACBCTL1_STOP 0x02
105 #define ACBCTL1_START 0x01
106 #define ACBADDR (iface->base + 4)
107 #define ACBCTL2 (iface->base + 5)
108 #define ACBCTL2_ENABLE 0x01
110 /************************************************************************/
112 static void scx200_acb_machine(struct scx200_acb_iface *iface, u8 status)
116 dev_dbg(&iface->adapter.dev, "state %s, status = 0x%02x\n",
117 scx200_acb_state_name[iface->state], status);
119 if (status & ACBST_BER) {
120 errmsg = "bus error";
123 if (!(status & ACBST_MASTER)) {
124 errmsg = "not master";
127 if (status & ACBST_NEGACK)
130 switch (iface->state) {
132 dev_warn(&iface->adapter.dev, "interrupt in idle state\n");
136 /* Do a pointer write first */
137 outb(iface->address_byte & ~1, ACBSDA);
139 iface->state = state_command;
143 outb(iface->command, ACBSDA);
145 if (iface->address_byte & 1)
146 iface->state = state_repeat_start;
148 iface->state = state_write;
151 case state_repeat_start:
152 outb(inb(ACBCTL1) | ACBCTL1_START, ACBCTL1);
156 if (iface->address_byte & 1) {
158 outb(inb(ACBCTL1) | ACBCTL1_ACK, ACBCTL1);
160 outb(inb(ACBCTL1) & ~ACBCTL1_ACK, ACBCTL1);
161 outb(iface->address_byte, ACBSDA);
163 iface->state = state_read;
165 outb(iface->address_byte, ACBSDA);
167 iface->state = state_write;
172 /* Set ACK if receiving the last byte */
174 outb(inb(ACBCTL1) | ACBCTL1_ACK, ACBCTL1);
176 outb(inb(ACBCTL1) & ~ACBCTL1_ACK, ACBCTL1);
178 *iface->ptr++ = inb(ACBSDA);
181 if (iface->len == 0) {
183 iface->state = state_idle;
184 outb(inb(ACBCTL1) | ACBCTL1_STOP, ACBCTL1);
190 if (iface->len == 0) {
192 iface->state = state_idle;
193 outb(inb(ACBCTL1) | ACBCTL1_STOP, ACBCTL1);
197 outb(*iface->ptr++, ACBSDA);
206 dev_dbg(&iface->adapter.dev, "negative ack in state %s\n",
207 scx200_acb_state_name[iface->state]);
209 iface->state = state_idle;
210 iface->result = -ENXIO;
212 outb(inb(ACBCTL1) | ACBCTL1_STOP, ACBCTL1);
213 outb(ACBST_STASTR | ACBST_NEGACK, ACBST);
217 dev_err(&iface->adapter.dev, "%s in state %s\n", errmsg,
218 scx200_acb_state_name[iface->state]);
220 iface->state = state_idle;
221 iface->result = -EIO;
222 iface->needs_reset = 1;
225 static void scx200_acb_timeout(struct scx200_acb_iface *iface)
227 dev_err(&iface->adapter.dev, "timeout in state %s\n",
228 scx200_acb_state_name[iface->state]);
230 iface->state = state_idle;
231 iface->result = -EIO;
232 iface->needs_reset = 1;
236 static void scx200_acb_poll(struct scx200_acb_iface *iface)
239 unsigned long timeout;
241 timeout = jiffies + POLL_TIMEOUT;
242 while (time_before(jiffies, timeout)) {
244 if ((status & (ACBST_SDAST|ACBST_BER|ACBST_NEGACK)) != 0) {
245 scx200_acb_machine(iface, status);
251 scx200_acb_timeout(iface);
253 #endif /* POLLED_MODE */
255 static void scx200_acb_reset(struct scx200_acb_iface *iface)
257 /* Disable the ACCESS.bus device and Configure the SCL
258 frequency: 16 clock cycles */
262 /* Disable slave address */
264 /* Enable the ACCESS.bus device */
265 outb(inb(ACBCTL2) | ACBCTL2_ENABLE, ACBCTL2);
266 /* Free STALL after START */
267 outb(inb(ACBCTL1) & ~(ACBCTL1_STASTRE | ACBCTL1_NMINTE), ACBCTL1);
269 outb(inb(ACBCTL1) | ACBCTL1_STOP, ACBCTL1);
270 /* Clear BER, NEGACK and STASTR bits */
271 outb(ACBST_BER | ACBST_NEGACK | ACBST_STASTR, ACBST);
273 outb(inb(ACBCST) | ACBCST_BB, ACBCST);
276 static s32 scx200_acb_smbus_xfer(struct i2c_adapter *adapter,
277 u16 address, unsigned short flags,
278 char rw, u8 command, int size,
279 union i2c_smbus_data *data)
281 struct scx200_acb_iface *iface = i2c_get_adapdata(adapter);
288 case I2C_SMBUS_QUICK:
294 if (rw == I2C_SMBUS_READ) {
296 buffer = &data->byte;
303 case I2C_SMBUS_BYTE_DATA:
305 buffer = &data->byte;
308 case I2C_SMBUS_WORD_DATA:
310 cur_word = cpu_to_le16(data->word);
311 buffer = (u8 *)&cur_word;
314 case I2C_SMBUS_BLOCK_DATA:
315 len = data->block[0];
316 buffer = &data->block[1];
323 dev_dbg(&adapter->dev,
324 "size=%d, address=0x%x, command=0x%x, len=%d, read=%d\n",
325 size, address, command, len, rw);
327 if (!len && rw == I2C_SMBUS_READ) {
328 dev_dbg(&adapter->dev, "zero length read\n");
334 iface->address_byte = address<<1;
335 if (rw == I2C_SMBUS_READ)
336 iface->address_byte |= 1;
337 iface->command = command;
340 iface->result = -EINVAL;
341 iface->needs_reset = 0;
343 outb(inb(ACBCTL1) | ACBCTL1_START, ACBCTL1);
345 if (size == I2C_SMBUS_QUICK || size == I2C_SMBUS_BYTE)
346 iface->state = state_quick;
348 iface->state = state_address;
351 while (iface->state != state_idle)
352 scx200_acb_poll(iface);
353 #else /* POLLED_MODE */
354 #error Interrupt driven mode not implemented
355 #endif /* POLLED_MODE */
357 if (iface->needs_reset)
358 scx200_acb_reset(iface);
364 if (rc == 0 && size == I2C_SMBUS_WORD_DATA && rw == I2C_SMBUS_READ)
365 data->word = le16_to_cpu(cur_word);
368 dev_dbg(&adapter->dev, "transfer done, result: %d", rc);
372 for (i = 0; i < len; ++i)
373 printk(" %02x", buffer[i]);
381 static u32 scx200_acb_func(struct i2c_adapter *adapter)
383 return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
384 I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
385 I2C_FUNC_SMBUS_BLOCK_DATA;
388 /* For now, we only handle combined mode (smbus) */
389 static struct i2c_algorithm scx200_acb_algorithm = {
390 .smbus_xfer = scx200_acb_smbus_xfer,
391 .functionality = scx200_acb_func,
394 static struct scx200_acb_iface *scx200_acb_list;
396 static int scx200_acb_probe(struct scx200_acb_iface *iface)
400 /* Disable the ACCESS.bus device and Configure the SCL
401 frequency: 16 clock cycles */
404 if (inb(ACBCTL2) != 0x70) {
405 pr_debug(NAME ": ACBCTL2 readback failed\n");
409 outb(inb(ACBCTL1) | ACBCTL1_NMINTE, ACBCTL1);
413 pr_debug(NAME ": disabled, but ACBCTL1=0x%02x\n",
418 outb(inb(ACBCTL2) | ACBCTL2_ENABLE, ACBCTL2);
420 outb(inb(ACBCTL1) | ACBCTL1_NMINTE, ACBCTL1);
423 if ((val & ACBCTL1_NMINTE) != ACBCTL1_NMINTE) {
424 pr_debug(NAME ": enabled, but NMINTE won't be set, "
425 "ACBCTL1=0x%02x\n", val);
432 static int __init scx200_acb_create(int base, int index)
434 struct scx200_acb_iface *iface;
435 struct i2c_adapter *adapter;
437 char description[64];
439 iface = kzalloc(sizeof(*iface), GFP_KERNEL);
441 printk(KERN_ERR NAME ": can't allocate memory\n");
446 adapter = &iface->adapter;
447 i2c_set_adapdata(adapter, iface);
448 snprintf(adapter->name, I2C_NAME_SIZE, "SCx200 ACB%d", index);
449 adapter->owner = THIS_MODULE;
450 adapter->id = I2C_HW_SMBUS_SCX200;
451 adapter->algo = &scx200_acb_algorithm;
452 adapter->class = I2C_CLASS_HWMON;
454 init_MUTEX(&iface->sem);
456 snprintf(description, sizeof(description),
457 "NatSemi SCx200 ACCESS.bus [%s]", adapter->name);
458 if (request_region(base, 8, description) == 0) {
459 printk(KERN_ERR NAME ": can't allocate io 0x%x-0x%x\n",
466 rc = scx200_acb_probe(iface);
468 printk(KERN_WARNING NAME ": probe failed\n");
472 scx200_acb_reset(iface);
474 if (i2c_add_adapter(adapter) < 0) {
475 printk(KERN_ERR NAME ": failed to register\n");
481 iface->next = scx200_acb_list;
482 scx200_acb_list = iface;
490 release_region(iface->base, 8);
496 static struct pci_device_id scx200[] = {
497 { PCI_DEVICE(PCI_VENDOR_ID_NS, PCI_DEVICE_ID_NS_SCx200_BRIDGE) },
498 { PCI_DEVICE(PCI_VENDOR_ID_NS, PCI_DEVICE_ID_NS_SC1100_BRIDGE) },
502 static int __init scx200_acb_init(void)
507 pr_debug(NAME ": NatSemi SCx200 ACCESS.bus Driver\n");
509 /* Verify that this really is a SCx200 processor */
510 if (pci_dev_present(scx200) == 0)
514 for (i = 0; i < MAX_DEVICES; ++i) {
516 rc = scx200_acb_create(base[i], i);
523 static void __exit scx200_acb_cleanup(void)
525 struct scx200_acb_iface *iface;
528 while ((iface = scx200_acb_list) != NULL) {
529 scx200_acb_list = iface->next;
532 i2c_del_adapter(&iface->adapter);
533 release_region(iface->base, 8);
540 module_init(scx200_acb_init);
541 module_exit(scx200_acb_cleanup);