2 Copyright (c) 2001,2002 Christer Weinigel <wingel@nano-system.com>
4 National Semiconductor SCx200 ACCESS.bus support
5 Also supports the AMD CS5535 and AMD CS5536
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.
22 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
24 #include <linux/module.h>
25 #include <linux/errno.h>
26 #include <linux/kernel.h>
27 #include <linux/init.h>
28 #include <linux/i2c.h>
29 #include <linux/pci.h>
30 #include <linux/platform_device.h>
31 #include <linux/delay.h>
32 #include <linux/mutex.h>
33 #include <linux/slab.h>
36 #include <linux/scx200.h>
38 MODULE_AUTHOR("Christer Weinigel <wingel@nano-system.com>");
39 MODULE_DESCRIPTION("NatSemi SCx200 ACCESS.bus Driver");
40 MODULE_ALIAS("platform:cs5535-smb");
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 #define POLL_TIMEOUT (HZ/5)
50 enum scx200_acb_state {
60 static const char *scx200_acb_state_name[] = {
70 /* Physical interface */
71 struct scx200_acb_iface {
72 struct scx200_acb_iface *next;
73 struct i2c_adapter adapter;
77 /* State machine data */
78 enum scx200_acb_state state;
87 /* Register Definitions */
88 #define ACBSDA (iface->base + 0)
89 #define ACBST (iface->base + 1)
90 #define ACBST_SDAST 0x40 /* SDA Status */
91 #define ACBST_BER 0x20
92 #define ACBST_NEGACK 0x10 /* Negative Acknowledge */
93 #define ACBST_STASTR 0x08 /* Stall After Start */
94 #define ACBST_MASTER 0x02
95 #define ACBCST (iface->base + 2)
96 #define ACBCST_BB 0x02
97 #define ACBCTL1 (iface->base + 3)
98 #define ACBCTL1_STASTRE 0x80
99 #define ACBCTL1_NMINTE 0x40
100 #define ACBCTL1_ACK 0x10
101 #define ACBCTL1_STOP 0x02
102 #define ACBCTL1_START 0x01
103 #define ACBADDR (iface->base + 4)
104 #define ACBCTL2 (iface->base + 5)
105 #define ACBCTL2_ENABLE 0x01
107 /************************************************************************/
109 static void scx200_acb_machine(struct scx200_acb_iface *iface, u8 status)
113 dev_dbg(&iface->adapter.dev, "state %s, status = 0x%02x\n",
114 scx200_acb_state_name[iface->state], status);
116 if (status & ACBST_BER) {
117 errmsg = "bus error";
120 if (!(status & ACBST_MASTER)) {
121 errmsg = "not master";
124 if (status & ACBST_NEGACK) {
125 dev_dbg(&iface->adapter.dev, "negative ack in state %s\n",
126 scx200_acb_state_name[iface->state]);
128 iface->state = state_idle;
129 iface->result = -ENXIO;
131 outb(inb(ACBCTL1) | ACBCTL1_STOP, ACBCTL1);
132 outb(ACBST_STASTR | ACBST_NEGACK, ACBST);
134 /* Reset the status register */
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 _next_ byte will be the last one */
183 outb(inb(ACBCTL1) | ACBCTL1_ACK, ACBCTL1);
185 outb(inb(ACBCTL1) & ~ACBCTL1_ACK, ACBCTL1);
187 if (iface->len == 1) {
189 iface->state = state_idle;
190 outb(inb(ACBCTL1) | ACBCTL1_STOP, ACBCTL1);
193 *iface->ptr++ = inb(ACBSDA);
199 if (iface->len == 0) {
201 iface->state = state_idle;
202 outb(inb(ACBCTL1) | ACBCTL1_STOP, ACBCTL1);
206 outb(*iface->ptr++, ACBSDA);
215 dev_err(&iface->adapter.dev,
216 "%s in state %s (addr=0x%02x, len=%d, status=0x%02x)\n", errmsg,
217 scx200_acb_state_name[iface->state], iface->address_byte,
220 iface->state = state_idle;
221 iface->result = -EIO;
222 iface->needs_reset = 1;
225 static void scx200_acb_poll(struct scx200_acb_iface *iface)
228 unsigned long timeout;
230 timeout = jiffies + POLL_TIMEOUT;
234 /* Reset the status register to avoid the hang */
237 if ((status & (ACBST_SDAST|ACBST_BER|ACBST_NEGACK)) != 0) {
238 scx200_acb_machine(iface, status);
241 if (time_after(jiffies, timeout))
247 dev_err(&iface->adapter.dev, "timeout in state %s\n",
248 scx200_acb_state_name[iface->state]);
250 iface->state = state_idle;
251 iface->result = -EIO;
252 iface->needs_reset = 1;
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:
295 buffer = rw ? &data->byte : &command;
298 case I2C_SMBUS_BYTE_DATA:
300 buffer = &data->byte;
303 case I2C_SMBUS_WORD_DATA:
305 cur_word = cpu_to_le16(data->word);
306 buffer = (u8 *)&cur_word;
309 case I2C_SMBUS_I2C_BLOCK_DATA:
310 len = data->block[0];
311 if (len == 0 || len > I2C_SMBUS_BLOCK_MAX)
313 buffer = &data->block[1];
320 dev_dbg(&adapter->dev,
321 "size=%d, address=0x%x, command=0x%x, len=%d, read=%d\n",
322 size, address, command, len, rw);
324 if (!len && rw == I2C_SMBUS_READ) {
325 dev_dbg(&adapter->dev, "zero length read\n");
329 mutex_lock(&iface->mutex);
331 iface->address_byte = (address << 1) | rw;
332 iface->command = command;
335 iface->result = -EINVAL;
336 iface->needs_reset = 0;
338 outb(inb(ACBCTL1) | ACBCTL1_START, ACBCTL1);
340 if (size == I2C_SMBUS_QUICK || size == I2C_SMBUS_BYTE)
341 iface->state = state_quick;
343 iface->state = state_address;
345 while (iface->state != state_idle)
346 scx200_acb_poll(iface);
348 if (iface->needs_reset)
349 scx200_acb_reset(iface);
353 mutex_unlock(&iface->mutex);
355 if (rc == 0 && size == I2C_SMBUS_WORD_DATA && rw == I2C_SMBUS_READ)
356 data->word = le16_to_cpu(cur_word);
359 dev_dbg(&adapter->dev, "transfer done, result: %d", rc);
363 for (i = 0; i < len; ++i)
364 printk(" %02x", buffer[i]);
372 static u32 scx200_acb_func(struct i2c_adapter *adapter)
374 return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
375 I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
376 I2C_FUNC_SMBUS_I2C_BLOCK;
379 /* For now, we only handle combined mode (smbus) */
380 static const struct i2c_algorithm scx200_acb_algorithm = {
381 .smbus_xfer = scx200_acb_smbus_xfer,
382 .functionality = scx200_acb_func,
385 static struct scx200_acb_iface *scx200_acb_list;
386 static DEFINE_MUTEX(scx200_acb_list_mutex);
388 static int scx200_acb_probe(struct scx200_acb_iface *iface)
392 /* Disable the ACCESS.bus device and Configure the SCL
393 frequency: 16 clock cycles */
396 if (inb(ACBCTL2) != 0x70) {
397 pr_debug("ACBCTL2 readback failed\n");
401 outb(inb(ACBCTL1) | ACBCTL1_NMINTE, ACBCTL1);
405 pr_debug("disabled, but ACBCTL1=0x%02x\n", val);
409 outb(inb(ACBCTL2) | ACBCTL2_ENABLE, ACBCTL2);
411 outb(inb(ACBCTL1) | ACBCTL1_NMINTE, ACBCTL1);
414 if ((val & ACBCTL1_NMINTE) != ACBCTL1_NMINTE) {
415 pr_debug("enabled, but NMINTE won't be set, ACBCTL1=0x%02x\n",
423 static struct scx200_acb_iface *scx200_create_iface(const char *text,
424 struct device *dev, int index)
426 struct scx200_acb_iface *iface;
427 struct i2c_adapter *adapter;
429 iface = kzalloc(sizeof(*iface), GFP_KERNEL);
433 adapter = &iface->adapter;
434 i2c_set_adapdata(adapter, iface);
435 snprintf(adapter->name, sizeof(adapter->name), "%s ACB%d", text, index);
436 adapter->owner = THIS_MODULE;
437 adapter->algo = &scx200_acb_algorithm;
438 adapter->class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
439 adapter->dev.parent = dev;
441 mutex_init(&iface->mutex);
446 static int scx200_acb_create(struct scx200_acb_iface *iface)
448 struct i2c_adapter *adapter;
451 adapter = &iface->adapter;
453 rc = scx200_acb_probe(iface);
455 pr_warn("probe failed\n");
459 scx200_acb_reset(iface);
461 if (i2c_add_adapter(adapter) < 0) {
462 pr_err("failed to register\n");
466 if (!adapter->dev.parent) {
467 /* If there's no dev, we're tracking (ISA) ifaces manually */
468 mutex_lock(&scx200_acb_list_mutex);
469 iface->next = scx200_acb_list;
470 scx200_acb_list = iface;
471 mutex_unlock(&scx200_acb_list_mutex);
477 static struct scx200_acb_iface *scx200_create_dev(const char *text,
478 unsigned long base, int index, struct device *dev)
480 struct scx200_acb_iface *iface;
483 iface = scx200_create_iface(text, dev, index);
488 if (!request_region(base, 8, iface->adapter.name)) {
489 pr_err("can't allocate io 0x%lx-0x%lx\n", base, base + 8 - 1);
494 rc = scx200_acb_create(iface);
499 release_region(base, 8);
505 static int scx200_probe(struct platform_device *pdev)
507 struct scx200_acb_iface *iface;
508 struct resource *res;
510 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
512 dev_err(&pdev->dev, "can't fetch device resource info\n");
516 iface = scx200_create_dev("CS5535", res->start, 0, &pdev->dev);
520 dev_info(&pdev->dev, "SCx200 device '%s' registered\n",
521 iface->adapter.name);
522 platform_set_drvdata(pdev, iface);
527 static void scx200_cleanup_iface(struct scx200_acb_iface *iface)
529 i2c_del_adapter(&iface->adapter);
530 release_region(iface->base, 8);
534 static int scx200_remove(struct platform_device *pdev)
536 struct scx200_acb_iface *iface;
538 iface = platform_get_drvdata(pdev);
539 scx200_cleanup_iface(iface);
544 static struct platform_driver scx200_pci_driver = {
546 .name = "cs5535-smb",
547 .owner = THIS_MODULE,
549 .probe = scx200_probe,
550 .remove = scx200_remove,
553 static const struct pci_device_id scx200_isa[] = {
554 { PCI_DEVICE(PCI_VENDOR_ID_NS, PCI_DEVICE_ID_NS_SCx200_BRIDGE) },
555 { PCI_DEVICE(PCI_VENDOR_ID_NS, PCI_DEVICE_ID_NS_SC1100_BRIDGE) },
559 static __init void scx200_scan_isa(void)
563 if (!pci_dev_present(scx200_isa))
566 for (i = 0; i < MAX_DEVICES; ++i) {
570 /* XXX: should we care about failures? */
571 scx200_create_dev("SCx200", base[i], i, NULL);
575 static int __init scx200_acb_init(void)
577 pr_debug("NatSemi SCx200 ACCESS.bus Driver\n");
579 /* First scan for ISA-based devices */
580 scx200_scan_isa(); /* XXX: should we care about errors? */
582 /* If at least one bus was created, init must succeed */
586 /* No ISA devices; register the platform driver for PCI-based devices */
587 return platform_driver_register(&scx200_pci_driver);
590 static void __exit scx200_acb_cleanup(void)
592 struct scx200_acb_iface *iface;
594 platform_driver_unregister(&scx200_pci_driver);
596 mutex_lock(&scx200_acb_list_mutex);
597 while ((iface = scx200_acb_list) != NULL) {
598 scx200_acb_list = iface->next;
599 mutex_unlock(&scx200_acb_list_mutex);
601 scx200_cleanup_iface(iface);
603 mutex_lock(&scx200_acb_list_mutex);
605 mutex_unlock(&scx200_acb_list_mutex);
608 module_init(scx200_acb_init);
609 module_exit(scx200_acb_cleanup);