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.
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.
26 #include <linux/module.h>
27 #include <linux/errno.h>
28 #include <linux/kernel.h>
29 #include <linux/init.h>
30 #include <linux/i2c.h>
31 #include <linux/pci.h>
32 #include <linux/platform_device.h>
33 #include <linux/delay.h>
34 #include <linux/mutex.h>
35 #include <linux/slab.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_ALIAS("platform:cs5535-smb");
45 MODULE_LICENSE("GPL");
48 static int base
[MAX_DEVICES
] = { 0x820, 0x840 };
49 module_param_array(base
, int, NULL
, 0);
50 MODULE_PARM_DESC(base
, "Base addresses for the ACCESS.bus controllers");
52 #define POLL_TIMEOUT (HZ/5)
54 enum scx200_acb_state
{
64 static const char *scx200_acb_state_name
[] = {
74 /* Physical interface */
75 struct scx200_acb_iface
{
76 struct scx200_acb_iface
*next
;
77 struct i2c_adapter adapter
;
81 /* State machine data */
82 enum scx200_acb_state state
;
91 /* Register Definitions */
92 #define ACBSDA (iface->base + 0)
93 #define ACBST (iface->base + 1)
94 #define ACBST_SDAST 0x40 /* SDA Status */
95 #define ACBST_BER 0x20
96 #define ACBST_NEGACK 0x10 /* Negative Acknowledge */
97 #define ACBST_STASTR 0x08 /* Stall After Start */
98 #define ACBST_MASTER 0x02
99 #define ACBCST (iface->base + 2)
100 #define ACBCST_BB 0x02
101 #define ACBCTL1 (iface->base + 3)
102 #define ACBCTL1_STASTRE 0x80
103 #define ACBCTL1_NMINTE 0x40
104 #define ACBCTL1_ACK 0x10
105 #define ACBCTL1_STOP 0x02
106 #define ACBCTL1_START 0x01
107 #define ACBADDR (iface->base + 4)
108 #define ACBCTL2 (iface->base + 5)
109 #define ACBCTL2_ENABLE 0x01
111 /************************************************************************/
113 static void scx200_acb_machine(struct scx200_acb_iface
*iface
, u8 status
)
117 dev_dbg(&iface
->adapter
.dev
, "state %s, status = 0x%02x\n",
118 scx200_acb_state_name
[iface
->state
], status
);
120 if (status
& ACBST_BER
) {
121 errmsg
= "bus error";
124 if (!(status
& ACBST_MASTER
)) {
125 errmsg
= "not master";
128 if (status
& ACBST_NEGACK
) {
129 dev_dbg(&iface
->adapter
.dev
, "negative ack in state %s\n",
130 scx200_acb_state_name
[iface
->state
]);
132 iface
->state
= state_idle
;
133 iface
->result
= -ENXIO
;
135 outb(inb(ACBCTL1
) | ACBCTL1_STOP
, ACBCTL1
);
136 outb(ACBST_STASTR
| ACBST_NEGACK
, ACBST
);
138 /* Reset the status register */
143 switch (iface
->state
) {
145 dev_warn(&iface
->adapter
.dev
, "interrupt in idle state\n");
149 /* Do a pointer write first */
150 outb(iface
->address_byte
& ~1, ACBSDA
);
152 iface
->state
= state_command
;
156 outb(iface
->command
, ACBSDA
);
158 if (iface
->address_byte
& 1)
159 iface
->state
= state_repeat_start
;
161 iface
->state
= state_write
;
164 case state_repeat_start
:
165 outb(inb(ACBCTL1
) | ACBCTL1_START
, ACBCTL1
);
169 if (iface
->address_byte
& 1) {
171 outb(inb(ACBCTL1
) | ACBCTL1_ACK
, ACBCTL1
);
173 outb(inb(ACBCTL1
) & ~ACBCTL1_ACK
, ACBCTL1
);
174 outb(iface
->address_byte
, ACBSDA
);
176 iface
->state
= state_read
;
178 outb(iface
->address_byte
, ACBSDA
);
180 iface
->state
= state_write
;
185 /* Set ACK if _next_ byte will be the last one */
187 outb(inb(ACBCTL1
) | ACBCTL1_ACK
, ACBCTL1
);
189 outb(inb(ACBCTL1
) & ~ACBCTL1_ACK
, ACBCTL1
);
191 if (iface
->len
== 1) {
193 iface
->state
= state_idle
;
194 outb(inb(ACBCTL1
) | ACBCTL1_STOP
, ACBCTL1
);
197 *iface
->ptr
++ = inb(ACBSDA
);
203 if (iface
->len
== 0) {
205 iface
->state
= state_idle
;
206 outb(inb(ACBCTL1
) | ACBCTL1_STOP
, ACBCTL1
);
210 outb(*iface
->ptr
++, ACBSDA
);
219 dev_err(&iface
->adapter
.dev
,
220 "%s in state %s (addr=0x%02x, len=%d, status=0x%02x)\n", errmsg
,
221 scx200_acb_state_name
[iface
->state
], iface
->address_byte
,
224 iface
->state
= state_idle
;
225 iface
->result
= -EIO
;
226 iface
->needs_reset
= 1;
229 static void scx200_acb_poll(struct scx200_acb_iface
*iface
)
232 unsigned long timeout
;
234 timeout
= jiffies
+ POLL_TIMEOUT
;
238 /* Reset the status register to avoid the hang */
241 if ((status
& (ACBST_SDAST
|ACBST_BER
|ACBST_NEGACK
)) != 0) {
242 scx200_acb_machine(iface
, status
);
245 if (time_after(jiffies
, timeout
))
251 dev_err(&iface
->adapter
.dev
, "timeout in state %s\n",
252 scx200_acb_state_name
[iface
->state
]);
254 iface
->state
= state_idle
;
255 iface
->result
= -EIO
;
256 iface
->needs_reset
= 1;
259 static void scx200_acb_reset(struct scx200_acb_iface
*iface
)
261 /* Disable the ACCESS.bus device and Configure the SCL
262 frequency: 16 clock cycles */
266 /* Disable slave address */
268 /* Enable the ACCESS.bus device */
269 outb(inb(ACBCTL2
) | ACBCTL2_ENABLE
, ACBCTL2
);
270 /* Free STALL after START */
271 outb(inb(ACBCTL1
) & ~(ACBCTL1_STASTRE
| ACBCTL1_NMINTE
), ACBCTL1
);
273 outb(inb(ACBCTL1
) | ACBCTL1_STOP
, ACBCTL1
);
274 /* Clear BER, NEGACK and STASTR bits */
275 outb(ACBST_BER
| ACBST_NEGACK
| ACBST_STASTR
, ACBST
);
277 outb(inb(ACBCST
) | ACBCST_BB
, ACBCST
);
280 static s32
scx200_acb_smbus_xfer(struct i2c_adapter
*adapter
,
281 u16 address
, unsigned short flags
,
282 char rw
, u8 command
, int size
,
283 union i2c_smbus_data
*data
)
285 struct scx200_acb_iface
*iface
= i2c_get_adapdata(adapter
);
292 case I2C_SMBUS_QUICK
:
299 buffer
= rw
? &data
->byte
: &command
;
302 case I2C_SMBUS_BYTE_DATA
:
304 buffer
= &data
->byte
;
307 case I2C_SMBUS_WORD_DATA
:
309 cur_word
= cpu_to_le16(data
->word
);
310 buffer
= (u8
*)&cur_word
;
313 case I2C_SMBUS_I2C_BLOCK_DATA
:
314 len
= data
->block
[0];
315 if (len
== 0 || len
> I2C_SMBUS_BLOCK_MAX
)
317 buffer
= &data
->block
[1];
324 dev_dbg(&adapter
->dev
,
325 "size=%d, address=0x%x, command=0x%x, len=%d, read=%d\n",
326 size
, address
, command
, len
, rw
);
328 if (!len
&& rw
== I2C_SMBUS_READ
) {
329 dev_dbg(&adapter
->dev
, "zero length read\n");
333 mutex_lock(&iface
->mutex
);
335 iface
->address_byte
= (address
<< 1) | rw
;
336 iface
->command
= command
;
339 iface
->result
= -EINVAL
;
340 iface
->needs_reset
= 0;
342 outb(inb(ACBCTL1
) | ACBCTL1_START
, ACBCTL1
);
344 if (size
== I2C_SMBUS_QUICK
|| size
== I2C_SMBUS_BYTE
)
345 iface
->state
= state_quick
;
347 iface
->state
= state_address
;
349 while (iface
->state
!= state_idle
)
350 scx200_acb_poll(iface
);
352 if (iface
->needs_reset
)
353 scx200_acb_reset(iface
);
357 mutex_unlock(&iface
->mutex
);
359 if (rc
== 0 && size
== I2C_SMBUS_WORD_DATA
&& rw
== I2C_SMBUS_READ
)
360 data
->word
= le16_to_cpu(cur_word
);
363 dev_dbg(&adapter
->dev
, "transfer done, result: %d", rc
);
367 for (i
= 0; i
< len
; ++i
)
368 printk(" %02x", buffer
[i
]);
376 static u32
scx200_acb_func(struct i2c_adapter
*adapter
)
378 return I2C_FUNC_SMBUS_QUICK
| I2C_FUNC_SMBUS_BYTE
|
379 I2C_FUNC_SMBUS_BYTE_DATA
| I2C_FUNC_SMBUS_WORD_DATA
|
380 I2C_FUNC_SMBUS_I2C_BLOCK
;
383 /* For now, we only handle combined mode (smbus) */
384 static const struct i2c_algorithm scx200_acb_algorithm
= {
385 .smbus_xfer
= scx200_acb_smbus_xfer
,
386 .functionality
= scx200_acb_func
,
389 static struct scx200_acb_iface
*scx200_acb_list
;
390 static DEFINE_MUTEX(scx200_acb_list_mutex
);
392 static __devinit
int scx200_acb_probe(struct scx200_acb_iface
*iface
)
396 /* Disable the ACCESS.bus device and Configure the SCL
397 frequency: 16 clock cycles */
400 if (inb(ACBCTL2
) != 0x70) {
401 pr_debug(NAME
": ACBCTL2 readback failed\n");
405 outb(inb(ACBCTL1
) | ACBCTL1_NMINTE
, ACBCTL1
);
409 pr_debug(NAME
": disabled, but ACBCTL1=0x%02x\n",
414 outb(inb(ACBCTL2
) | ACBCTL2_ENABLE
, ACBCTL2
);
416 outb(inb(ACBCTL1
) | ACBCTL1_NMINTE
, ACBCTL1
);
419 if ((val
& ACBCTL1_NMINTE
) != ACBCTL1_NMINTE
) {
420 pr_debug(NAME
": enabled, but NMINTE won't be set, "
421 "ACBCTL1=0x%02x\n", val
);
428 static __devinit
struct scx200_acb_iface
*scx200_create_iface(const char *text
,
429 struct device
*dev
, int index
)
431 struct scx200_acb_iface
*iface
;
432 struct i2c_adapter
*adapter
;
434 iface
= kzalloc(sizeof(*iface
), GFP_KERNEL
);
436 printk(KERN_ERR NAME
": can't allocate memory\n");
440 adapter
= &iface
->adapter
;
441 i2c_set_adapdata(adapter
, iface
);
442 snprintf(adapter
->name
, sizeof(adapter
->name
), "%s ACB%d", text
, index
);
443 adapter
->owner
= THIS_MODULE
;
444 adapter
->algo
= &scx200_acb_algorithm
;
445 adapter
->class = I2C_CLASS_HWMON
| I2C_CLASS_SPD
;
446 adapter
->dev
.parent
= dev
;
448 mutex_init(&iface
->mutex
);
453 static int __devinit
scx200_acb_create(struct scx200_acb_iface
*iface
)
455 struct i2c_adapter
*adapter
;
458 adapter
= &iface
->adapter
;
460 rc
= scx200_acb_probe(iface
);
462 printk(KERN_WARNING NAME
": probe failed\n");
466 scx200_acb_reset(iface
);
468 if (i2c_add_adapter(adapter
) < 0) {
469 printk(KERN_ERR NAME
": failed to register\n");
473 if (!adapter
->dev
.parent
) {
474 /* If there's no dev, we're tracking (ISA) ifaces manually */
475 mutex_lock(&scx200_acb_list_mutex
);
476 iface
->next
= scx200_acb_list
;
477 scx200_acb_list
= iface
;
478 mutex_unlock(&scx200_acb_list_mutex
);
484 static struct scx200_acb_iface
* __devinit
scx200_create_dev(const char *text
,
485 unsigned long base
, int index
, struct device
*dev
)
487 struct scx200_acb_iface
*iface
;
490 iface
= scx200_create_iface(text
, dev
, index
);
495 if (!request_region(base
, 8, iface
->adapter
.name
)) {
496 printk(KERN_ERR NAME
": can't allocate io 0x%lx-0x%lx\n",
502 rc
= scx200_acb_create(iface
);
507 release_region(base
, 8);
513 static int __devinit
scx200_probe(struct platform_device
*pdev
)
515 struct scx200_acb_iface
*iface
;
516 struct resource
*res
;
518 res
= platform_get_resource(pdev
, IORESOURCE_IO
, 0);
520 dev_err(&pdev
->dev
, "can't fetch device resource info\n");
524 iface
= scx200_create_dev("CS5535", res
->start
, 0, &pdev
->dev
);
528 dev_info(&pdev
->dev
, "SCx200 device '%s' registered\n",
529 iface
->adapter
.name
);
530 platform_set_drvdata(pdev
, iface
);
535 static void __devexit
scx200_cleanup_iface(struct scx200_acb_iface
*iface
)
537 i2c_del_adapter(&iface
->adapter
);
538 release_region(iface
->base
, 8);
542 static int __devexit
scx200_remove(struct platform_device
*pdev
)
544 struct scx200_acb_iface
*iface
;
546 iface
= platform_get_drvdata(pdev
);
547 platform_set_drvdata(pdev
, NULL
);
548 scx200_cleanup_iface(iface
);
553 static struct platform_driver scx200_pci_drv
= {
555 .name
= "cs5535-smb",
556 .owner
= THIS_MODULE
,
558 .probe
= scx200_probe
,
559 .remove
= __devexit_p(scx200_remove
),
562 static const struct pci_device_id scx200_isa
[] __initconst
= {
563 { PCI_DEVICE(PCI_VENDOR_ID_NS
, PCI_DEVICE_ID_NS_SCx200_BRIDGE
) },
564 { PCI_DEVICE(PCI_VENDOR_ID_NS
, PCI_DEVICE_ID_NS_SC1100_BRIDGE
) },
568 static __init
void scx200_scan_isa(void)
572 if (!pci_dev_present(scx200_isa
))
575 for (i
= 0; i
< MAX_DEVICES
; ++i
) {
579 /* XXX: should we care about failures? */
580 scx200_create_dev("SCx200", base
[i
], i
, NULL
);
584 static int __init
scx200_acb_init(void)
586 pr_debug(NAME
": NatSemi SCx200 ACCESS.bus Driver\n");
588 /* First scan for ISA-based devices */
589 scx200_scan_isa(); /* XXX: should we care about errors? */
591 /* If at least one bus was created, init must succeed */
595 /* No ISA devices; register the platform driver for PCI-based devices */
596 return platform_driver_register(&scx200_pci_drv
);
599 static void __exit
scx200_acb_cleanup(void)
601 struct scx200_acb_iface
*iface
;
603 platform_driver_unregister(&scx200_pci_drv
);
605 mutex_lock(&scx200_acb_list_mutex
);
606 while ((iface
= scx200_acb_list
) != NULL
) {
607 scx200_acb_list
= iface
->next
;
608 mutex_unlock(&scx200_acb_list_mutex
);
610 scx200_cleanup_iface(iface
);
612 mutex_lock(&scx200_acb_list_mutex
);
614 mutex_unlock(&scx200_acb_list_mutex
);
617 module_init(scx200_acb_init
);
618 module_exit(scx200_acb_cleanup
);