1 // SPDX-License-Identifier: GPL-2.0-or-later
3 Copyright (c) 2001,2002 Christer Weinigel <wingel@nano-system.com>
5 National Semiconductor SCx200 ACCESS.bus support
6 Also supports the AMD CS5535 and AMD CS5536
8 Based on i2c-keywest.c which is:
9 Copyright (c) 2001 Benjamin Herrenschmidt <benh@kernel.crashing.org>
10 Copyright (c) 2000 Philip Edelbrock <phil@stimpy.netroedge.com>
14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16 #include <linux/module.h>
17 #include <linux/errno.h>
18 #include <linux/kernel.h>
19 #include <linux/init.h>
20 #include <linux/i2c.h>
21 #include <linux/pci.h>
22 #include <linux/platform_device.h>
23 #include <linux/delay.h>
24 #include <linux/mutex.h>
25 #include <linux/slab.h>
28 #include <linux/scx200.h>
30 MODULE_AUTHOR("Christer Weinigel <wingel@nano-system.com>");
31 MODULE_DESCRIPTION("NatSemi SCx200 ACCESS.bus Driver");
32 MODULE_ALIAS("platform:cs5535-smb");
33 MODULE_LICENSE("GPL");
36 static int base
[MAX_DEVICES
] = { 0x820, 0x840 };
37 module_param_hw_array(base
, int, ioport
, NULL
, 0);
38 MODULE_PARM_DESC(base
, "Base addresses for the ACCESS.bus controllers");
40 #define POLL_TIMEOUT (HZ/5)
42 enum scx200_acb_state
{
52 static const char *scx200_acb_state_name
[] = {
62 /* Physical interface */
63 struct scx200_acb_iface
{
64 struct scx200_acb_iface
*next
;
65 struct i2c_adapter adapter
;
69 /* State machine data */
70 enum scx200_acb_state state
;
79 /* Register Definitions */
80 #define ACBSDA (iface->base + 0)
81 #define ACBST (iface->base + 1)
82 #define ACBST_SDAST 0x40 /* SDA Status */
83 #define ACBST_BER 0x20
84 #define ACBST_NEGACK 0x10 /* Negative Acknowledge */
85 #define ACBST_STASTR 0x08 /* Stall After Start */
86 #define ACBST_MASTER 0x02
87 #define ACBCST (iface->base + 2)
88 #define ACBCST_BB 0x02
89 #define ACBCTL1 (iface->base + 3)
90 #define ACBCTL1_STASTRE 0x80
91 #define ACBCTL1_NMINTE 0x40
92 #define ACBCTL1_ACK 0x10
93 #define ACBCTL1_STOP 0x02
94 #define ACBCTL1_START 0x01
95 #define ACBADDR (iface->base + 4)
96 #define ACBCTL2 (iface->base + 5)
97 #define ACBCTL2_ENABLE 0x01
99 /************************************************************************/
101 static void scx200_acb_machine(struct scx200_acb_iface
*iface
, u8 status
)
105 dev_dbg(&iface
->adapter
.dev
, "state %s, status = 0x%02x\n",
106 scx200_acb_state_name
[iface
->state
], status
);
108 if (status
& ACBST_BER
) {
109 errmsg
= "bus error";
112 if (!(status
& ACBST_MASTER
)) {
113 errmsg
= "not master";
116 if (status
& ACBST_NEGACK
) {
117 dev_dbg(&iface
->adapter
.dev
, "negative ack in state %s\n",
118 scx200_acb_state_name
[iface
->state
]);
120 iface
->state
= state_idle
;
121 iface
->result
= -ENXIO
;
123 outb(inb(ACBCTL1
) | ACBCTL1_STOP
, ACBCTL1
);
124 outb(ACBST_STASTR
| ACBST_NEGACK
, ACBST
);
126 /* Reset the status register */
131 switch (iface
->state
) {
133 dev_warn(&iface
->adapter
.dev
, "interrupt in idle state\n");
137 /* Do a pointer write first */
138 outb(iface
->address_byte
& ~1, ACBSDA
);
140 iface
->state
= state_command
;
144 outb(iface
->command
, ACBSDA
);
146 if (iface
->address_byte
& 1)
147 iface
->state
= state_repeat_start
;
149 iface
->state
= state_write
;
152 case state_repeat_start
:
153 outb(inb(ACBCTL1
) | ACBCTL1_START
, ACBCTL1
);
157 if (iface
->address_byte
& 1) {
159 outb(inb(ACBCTL1
) | ACBCTL1_ACK
, ACBCTL1
);
161 outb(inb(ACBCTL1
) & ~ACBCTL1_ACK
, ACBCTL1
);
162 outb(iface
->address_byte
, ACBSDA
);
164 iface
->state
= state_read
;
166 outb(iface
->address_byte
, ACBSDA
);
168 iface
->state
= state_write
;
173 /* Set ACK if _next_ byte will be the last one */
175 outb(inb(ACBCTL1
) | ACBCTL1_ACK
, ACBCTL1
);
177 outb(inb(ACBCTL1
) & ~ACBCTL1_ACK
, ACBCTL1
);
179 if (iface
->len
== 1) {
181 iface
->state
= state_idle
;
182 outb(inb(ACBCTL1
) | ACBCTL1_STOP
, ACBCTL1
);
185 *iface
->ptr
++ = inb(ACBSDA
);
191 if (iface
->len
== 0) {
193 iface
->state
= state_idle
;
194 outb(inb(ACBCTL1
) | ACBCTL1_STOP
, ACBCTL1
);
198 outb(*iface
->ptr
++, ACBSDA
);
207 dev_err(&iface
->adapter
.dev
,
208 "%s in state %s (addr=0x%02x, len=%d, status=0x%02x)\n", errmsg
,
209 scx200_acb_state_name
[iface
->state
], iface
->address_byte
,
212 iface
->state
= state_idle
;
213 iface
->result
= -EIO
;
214 iface
->needs_reset
= 1;
217 static void scx200_acb_poll(struct scx200_acb_iface
*iface
)
220 unsigned long timeout
;
222 timeout
= jiffies
+ POLL_TIMEOUT
;
226 /* Reset the status register to avoid the hang */
229 if ((status
& (ACBST_SDAST
|ACBST_BER
|ACBST_NEGACK
)) != 0) {
230 scx200_acb_machine(iface
, status
);
233 if (time_after(jiffies
, timeout
))
239 dev_err(&iface
->adapter
.dev
, "timeout in state %s\n",
240 scx200_acb_state_name
[iface
->state
]);
242 iface
->state
= state_idle
;
243 iface
->result
= -EIO
;
244 iface
->needs_reset
= 1;
247 static void scx200_acb_reset(struct scx200_acb_iface
*iface
)
249 /* Disable the ACCESS.bus device and Configure the SCL
250 frequency: 16 clock cycles */
254 /* Disable slave address */
256 /* Enable the ACCESS.bus device */
257 outb(inb(ACBCTL2
) | ACBCTL2_ENABLE
, ACBCTL2
);
258 /* Free STALL after START */
259 outb(inb(ACBCTL1
) & ~(ACBCTL1_STASTRE
| ACBCTL1_NMINTE
), ACBCTL1
);
261 outb(inb(ACBCTL1
) | ACBCTL1_STOP
, ACBCTL1
);
262 /* Clear BER, NEGACK and STASTR bits */
263 outb(ACBST_BER
| ACBST_NEGACK
| ACBST_STASTR
, ACBST
);
265 outb(inb(ACBCST
) | ACBCST_BB
, ACBCST
);
268 static s32
scx200_acb_smbus_xfer(struct i2c_adapter
*adapter
,
269 u16 address
, unsigned short flags
,
270 char rw
, u8 command
, int size
,
271 union i2c_smbus_data
*data
)
273 struct scx200_acb_iface
*iface
= i2c_get_adapdata(adapter
);
280 case I2C_SMBUS_QUICK
:
287 buffer
= rw
? &data
->byte
: &command
;
290 case I2C_SMBUS_BYTE_DATA
:
292 buffer
= &data
->byte
;
295 case I2C_SMBUS_WORD_DATA
:
297 cur_word
= cpu_to_le16(data
->word
);
298 buffer
= (u8
*)&cur_word
;
301 case I2C_SMBUS_I2C_BLOCK_DATA
:
302 len
= data
->block
[0];
303 if (len
== 0 || len
> I2C_SMBUS_BLOCK_MAX
)
305 buffer
= &data
->block
[1];
312 dev_dbg(&adapter
->dev
,
313 "size=%d, address=0x%x, command=0x%x, len=%d, read=%d\n",
314 size
, address
, command
, len
, rw
);
316 if (!len
&& rw
== I2C_SMBUS_READ
) {
317 dev_dbg(&adapter
->dev
, "zero length read\n");
321 mutex_lock(&iface
->mutex
);
323 iface
->address_byte
= (address
<< 1) | rw
;
324 iface
->command
= command
;
327 iface
->result
= -EINVAL
;
328 iface
->needs_reset
= 0;
330 outb(inb(ACBCTL1
) | ACBCTL1_START
, ACBCTL1
);
332 if (size
== I2C_SMBUS_QUICK
|| size
== I2C_SMBUS_BYTE
)
333 iface
->state
= state_quick
;
335 iface
->state
= state_address
;
337 while (iface
->state
!= state_idle
)
338 scx200_acb_poll(iface
);
340 if (iface
->needs_reset
)
341 scx200_acb_reset(iface
);
345 mutex_unlock(&iface
->mutex
);
347 if (rc
== 0 && size
== I2C_SMBUS_WORD_DATA
&& rw
== I2C_SMBUS_READ
)
348 data
->word
= le16_to_cpu(cur_word
);
351 dev_dbg(&adapter
->dev
, "transfer done, result: %d", rc
);
355 for (i
= 0; i
< len
; ++i
)
356 printk(" %02x", buffer
[i
]);
364 static u32
scx200_acb_func(struct i2c_adapter
*adapter
)
366 return I2C_FUNC_SMBUS_QUICK
| I2C_FUNC_SMBUS_BYTE
|
367 I2C_FUNC_SMBUS_BYTE_DATA
| I2C_FUNC_SMBUS_WORD_DATA
|
368 I2C_FUNC_SMBUS_I2C_BLOCK
;
371 /* For now, we only handle combined mode (smbus) */
372 static const struct i2c_algorithm scx200_acb_algorithm
= {
373 .smbus_xfer
= scx200_acb_smbus_xfer
,
374 .functionality
= scx200_acb_func
,
377 static struct scx200_acb_iface
*scx200_acb_list
;
378 static DEFINE_MUTEX(scx200_acb_list_mutex
);
380 static int scx200_acb_probe(struct scx200_acb_iface
*iface
)
384 /* Disable the ACCESS.bus device and Configure the SCL
385 frequency: 16 clock cycles */
388 if (inb(ACBCTL2
) != 0x70) {
389 pr_debug("ACBCTL2 readback failed\n");
393 outb(inb(ACBCTL1
) | ACBCTL1_NMINTE
, ACBCTL1
);
397 pr_debug("disabled, but ACBCTL1=0x%02x\n", val
);
401 outb(inb(ACBCTL2
) | ACBCTL2_ENABLE
, ACBCTL2
);
403 outb(inb(ACBCTL1
) | ACBCTL1_NMINTE
, ACBCTL1
);
406 if ((val
& ACBCTL1_NMINTE
) != ACBCTL1_NMINTE
) {
407 pr_debug("enabled, but NMINTE won't be set, ACBCTL1=0x%02x\n",
415 static struct scx200_acb_iface
*scx200_create_iface(const char *text
,
416 struct device
*dev
, int index
)
418 struct scx200_acb_iface
*iface
;
419 struct i2c_adapter
*adapter
;
421 iface
= kzalloc(sizeof(*iface
), GFP_KERNEL
);
425 adapter
= &iface
->adapter
;
426 i2c_set_adapdata(adapter
, iface
);
427 snprintf(adapter
->name
, sizeof(adapter
->name
), "%s ACB%d", text
, index
);
428 adapter
->owner
= THIS_MODULE
;
429 adapter
->algo
= &scx200_acb_algorithm
;
430 adapter
->class = I2C_CLASS_HWMON
| I2C_CLASS_SPD
;
431 adapter
->dev
.parent
= dev
;
433 mutex_init(&iface
->mutex
);
438 static int scx200_acb_create(struct scx200_acb_iface
*iface
)
440 struct i2c_adapter
*adapter
;
443 adapter
= &iface
->adapter
;
445 rc
= scx200_acb_probe(iface
);
447 pr_warn("probe failed\n");
451 scx200_acb_reset(iface
);
453 if (i2c_add_adapter(adapter
) < 0) {
454 pr_err("failed to register\n");
458 if (!adapter
->dev
.parent
) {
459 /* If there's no dev, we're tracking (ISA) ifaces manually */
460 mutex_lock(&scx200_acb_list_mutex
);
461 iface
->next
= scx200_acb_list
;
462 scx200_acb_list
= iface
;
463 mutex_unlock(&scx200_acb_list_mutex
);
469 static struct scx200_acb_iface
*scx200_create_dev(const char *text
,
470 unsigned long base
, int index
, struct device
*dev
)
472 struct scx200_acb_iface
*iface
;
475 iface
= scx200_create_iface(text
, dev
, index
);
480 if (!request_region(base
, 8, iface
->adapter
.name
)) {
481 pr_err("can't allocate io 0x%lx-0x%lx\n", base
, base
+ 8 - 1);
486 rc
= scx200_acb_create(iface
);
491 release_region(base
, 8);
497 static int scx200_probe(struct platform_device
*pdev
)
499 struct scx200_acb_iface
*iface
;
500 struct resource
*res
;
502 res
= platform_get_resource(pdev
, IORESOURCE_IO
, 0);
504 dev_err(&pdev
->dev
, "can't fetch device resource info\n");
508 iface
= scx200_create_dev("CS5535", res
->start
, 0, &pdev
->dev
);
512 dev_info(&pdev
->dev
, "SCx200 device '%s' registered\n",
513 iface
->adapter
.name
);
514 platform_set_drvdata(pdev
, iface
);
519 static void scx200_cleanup_iface(struct scx200_acb_iface
*iface
)
521 i2c_del_adapter(&iface
->adapter
);
522 release_region(iface
->base
, 8);
526 static int scx200_remove(struct platform_device
*pdev
)
528 struct scx200_acb_iface
*iface
;
530 iface
= platform_get_drvdata(pdev
);
531 scx200_cleanup_iface(iface
);
536 static struct platform_driver scx200_pci_driver
= {
538 .name
= "cs5535-smb",
540 .probe
= scx200_probe
,
541 .remove
= scx200_remove
,
544 static const struct pci_device_id scx200_isa
[] = {
545 { PCI_DEVICE(PCI_VENDOR_ID_NS
, PCI_DEVICE_ID_NS_SCx200_BRIDGE
) },
546 { PCI_DEVICE(PCI_VENDOR_ID_NS
, PCI_DEVICE_ID_NS_SC1100_BRIDGE
) },
550 static __init
void scx200_scan_isa(void)
554 if (!pci_dev_present(scx200_isa
))
557 for (i
= 0; i
< MAX_DEVICES
; ++i
) {
561 /* XXX: should we care about failures? */
562 scx200_create_dev("SCx200", base
[i
], i
, NULL
);
566 static int __init
scx200_acb_init(void)
568 pr_debug("NatSemi SCx200 ACCESS.bus Driver\n");
570 /* First scan for ISA-based devices */
571 scx200_scan_isa(); /* XXX: should we care about errors? */
573 /* If at least one bus was created, init must succeed */
577 /* No ISA devices; register the platform driver for PCI-based devices */
578 return platform_driver_register(&scx200_pci_driver
);
581 static void __exit
scx200_acb_cleanup(void)
583 struct scx200_acb_iface
*iface
;
585 platform_driver_unregister(&scx200_pci_driver
);
587 mutex_lock(&scx200_acb_list_mutex
);
588 while ((iface
= scx200_acb_list
) != NULL
) {
589 scx200_acb_list
= iface
->next
;
590 mutex_unlock(&scx200_acb_list_mutex
);
592 scx200_cleanup_iface(iface
);
594 mutex_lock(&scx200_acb_list_mutex
);
596 mutex_unlock(&scx200_acb_list_mutex
);
599 module_init(scx200_acb_init
);
600 module_exit(scx200_acb_cleanup
);