blob: f45779b7690804d94452ef9670f12e2a77017e68 [file] [log] [blame]
Ben Gardner99c3adb2006-01-18 22:41:50 +01001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 Copyright (c) 2001,2002 Christer Weinigel <wingel@nano-system.com>
3
4 National Semiconductor SCx200 ACCESS.bus support
Ben Gardner99c3adb2006-01-18 22:41:50 +01005
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 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>
Ben Gardner99c3adb2006-01-18 22:41:50 +01009
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 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.
Ben Gardner99c3adb2006-01-18 22:41:50 +010014
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 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.
Ben Gardner99c3adb2006-01-18 22:41:50 +010019
Linus Torvalds1da177e2005-04-16 15:20:36 -070020 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070023*/
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#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>
33#include <asm/io.h>
34
35#include <linux/scx200.h>
36
37#define NAME "scx200_acb"
38
39MODULE_AUTHOR("Christer Weinigel <wingel@nano-system.com>");
40MODULE_DESCRIPTION("NatSemi SCx200 ACCESS.bus Driver");
41MODULE_LICENSE("GPL");
42
43#define MAX_DEVICES 4
44static int base[MAX_DEVICES] = { 0x820, 0x840 };
45module_param_array(base, int, NULL, 0);
46MODULE_PARM_DESC(base, "Base addresses for the ACCESS.bus controllers");
47
48#ifdef DEBUG
49#define DBG(x...) printk(KERN_DEBUG NAME ": " x)
50#else
51#define DBG(x...)
52#endif
53
54/* The hardware supports interrupt driven mode too, but I haven't
55 implemented that. */
56#define POLLED_MODE 1
57#define POLL_TIMEOUT (HZ)
58
59enum scx200_acb_state {
60 state_idle,
61 state_address,
62 state_command,
63 state_repeat_start,
64 state_quick,
65 state_read,
66 state_write,
67};
68
69static const char *scx200_acb_state_name[] = {
70 "idle",
71 "address",
72 "command",
73 "repeat_start",
74 "quick",
75 "read",
76 "write",
77};
78
79/* Physical interface */
Ben Gardner99c3adb2006-01-18 22:41:50 +010080struct scx200_acb_iface {
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 struct scx200_acb_iface *next;
82 struct i2c_adapter adapter;
83 unsigned base;
84 struct semaphore sem;
85
86 /* State machine data */
87 enum scx200_acb_state state;
88 int result;
89 u8 address_byte;
90 u8 command;
91 u8 *ptr;
92 char needs_reset;
93 unsigned len;
94};
95
96/* Register Definitions */
97#define ACBSDA (iface->base + 0)
98#define ACBST (iface->base + 1)
99#define ACBST_SDAST 0x40 /* SDA Status */
Ben Gardner99c3adb2006-01-18 22:41:50 +0100100#define ACBST_BER 0x20
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101#define ACBST_NEGACK 0x10 /* Negative Acknowledge */
102#define ACBST_STASTR 0x08 /* Stall After Start */
103#define ACBST_MASTER 0x02
104#define ACBCST (iface->base + 2)
105#define ACBCST_BB 0x02
106#define ACBCTL1 (iface->base + 3)
107#define ACBCTL1_STASTRE 0x80
108#define ACBCTL1_NMINTE 0x40
Ben Gardner99c3adb2006-01-18 22:41:50 +0100109#define ACBCTL1_ACK 0x10
110#define ACBCTL1_STOP 0x02
111#define ACBCTL1_START 0x01
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112#define ACBADDR (iface->base + 4)
113#define ACBCTL2 (iface->base + 5)
114#define ACBCTL2_ENABLE 0x01
115
116/************************************************************************/
117
118static void scx200_acb_machine(struct scx200_acb_iface *iface, u8 status)
119{
120 const char *errmsg;
121
Ben Gardner99c3adb2006-01-18 22:41:50 +0100122 DBG("state %s, status = 0x%02x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 scx200_acb_state_name[iface->state], status);
124
125 if (status & ACBST_BER) {
126 errmsg = "bus error";
127 goto error;
128 }
129 if (!(status & ACBST_MASTER)) {
130 errmsg = "not master";
131 goto error;
132 }
133 if (status & ACBST_NEGACK)
134 goto negack;
135
136 switch (iface->state) {
137 case state_idle:
138 dev_warn(&iface->adapter.dev, "interrupt in idle state\n");
139 break;
140
141 case state_address:
142 /* Do a pointer write first */
143 outb(iface->address_byte & ~1, ACBSDA);
144
145 iface->state = state_command;
146 break;
147
148 case state_command:
149 outb(iface->command, ACBSDA);
150
151 if (iface->address_byte & 1)
152 iface->state = state_repeat_start;
153 else
154 iface->state = state_write;
155 break;
156
157 case state_repeat_start:
158 outb(inb(ACBCTL1) | ACBCTL1_START, ACBCTL1);
159 /* fallthrough */
Ben Gardner99c3adb2006-01-18 22:41:50 +0100160
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 case state_quick:
162 if (iface->address_byte & 1) {
Ben Gardner99c3adb2006-01-18 22:41:50 +0100163 if (iface->len == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 outb(inb(ACBCTL1) | ACBCTL1_ACK, ACBCTL1);
165 else
166 outb(inb(ACBCTL1) & ~ACBCTL1_ACK, ACBCTL1);
167 outb(iface->address_byte, ACBSDA);
168
169 iface->state = state_read;
170 } else {
171 outb(iface->address_byte, ACBSDA);
172
173 iface->state = state_write;
174 }
175 break;
176
177 case state_read:
178 /* Set ACK if receiving the last byte */
179 if (iface->len == 1)
180 outb(inb(ACBCTL1) | ACBCTL1_ACK, ACBCTL1);
181 else
182 outb(inb(ACBCTL1) & ~ACBCTL1_ACK, ACBCTL1);
183
184 *iface->ptr++ = inb(ACBSDA);
185 --iface->len;
186
187 if (iface->len == 0) {
188 iface->result = 0;
189 iface->state = state_idle;
190 outb(inb(ACBCTL1) | ACBCTL1_STOP, ACBCTL1);
191 }
192
193 break;
194
195 case state_write:
196 if (iface->len == 0) {
197 iface->result = 0;
198 iface->state = state_idle;
199 outb(inb(ACBCTL1) | ACBCTL1_STOP, ACBCTL1);
200 break;
201 }
Ben Gardner99c3adb2006-01-18 22:41:50 +0100202
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 outb(*iface->ptr++, ACBSDA);
204 --iface->len;
Ben Gardner99c3adb2006-01-18 22:41:50 +0100205
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 break;
207 }
208
209 return;
210
211 negack:
Ben Gardner99c3adb2006-01-18 22:41:50 +0100212 DBG("negative acknowledge in state %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 scx200_acb_state_name[iface->state]);
214
215 iface->state = state_idle;
216 iface->result = -ENXIO;
217
218 outb(inb(ACBCTL1) | ACBCTL1_STOP, ACBCTL1);
219 outb(ACBST_STASTR | ACBST_NEGACK, ACBST);
220 return;
221
222 error:
223 dev_err(&iface->adapter.dev, "%s in state %s\n", errmsg,
224 scx200_acb_state_name[iface->state]);
225
226 iface->state = state_idle;
227 iface->result = -EIO;
228 iface->needs_reset = 1;
229}
230
Ben Gardner99c3adb2006-01-18 22:41:50 +0100231static void scx200_acb_timeout(struct scx200_acb_iface *iface)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232{
233 dev_err(&iface->adapter.dev, "timeout in state %s\n",
234 scx200_acb_state_name[iface->state]);
235
236 iface->state = state_idle;
237 iface->result = -EIO;
238 iface->needs_reset = 1;
239}
240
241#ifdef POLLED_MODE
242static void scx200_acb_poll(struct scx200_acb_iface *iface)
243{
244 u8 status = 0;
245 unsigned long timeout;
246
247 timeout = jiffies + POLL_TIMEOUT;
248 while (time_before(jiffies, timeout)) {
249 status = inb(ACBST);
250 if ((status & (ACBST_SDAST|ACBST_BER|ACBST_NEGACK)) != 0) {
251 scx200_acb_machine(iface, status);
252 return;
253 }
254 msleep(10);
255 }
256
257 scx200_acb_timeout(iface);
258}
259#endif /* POLLED_MODE */
260
261static void scx200_acb_reset(struct scx200_acb_iface *iface)
262{
263 /* Disable the ACCESS.bus device and Configure the SCL
Ben Gardner99c3adb2006-01-18 22:41:50 +0100264 frequency: 16 clock cycles */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 outb(0x70, ACBCTL2);
266 /* Polling mode */
267 outb(0, ACBCTL1);
268 /* Disable slave address */
269 outb(0, ACBADDR);
270 /* Enable the ACCESS.bus device */
271 outb(inb(ACBCTL2) | ACBCTL2_ENABLE, ACBCTL2);
272 /* Free STALL after START */
273 outb(inb(ACBCTL1) & ~(ACBCTL1_STASTRE | ACBCTL1_NMINTE), ACBCTL1);
274 /* Send a STOP */
275 outb(inb(ACBCTL1) | ACBCTL1_STOP, ACBCTL1);
276 /* Clear BER, NEGACK and STASTR bits */
277 outb(ACBST_BER | ACBST_NEGACK | ACBST_STASTR, ACBST);
278 /* Clear BB bit */
279 outb(inb(ACBCST) | ACBCST_BB, ACBCST);
280}
281
282static s32 scx200_acb_smbus_xfer(struct i2c_adapter *adapter,
Ben Gardner99c3adb2006-01-18 22:41:50 +0100283 u16 address, unsigned short flags,
284 char rw, u8 command, int size,
285 union i2c_smbus_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286{
287 struct scx200_acb_iface *iface = i2c_get_adapdata(adapter);
288 int len;
289 u8 *buffer;
290 u16 cur_word;
291 int rc;
292
293 switch (size) {
294 case I2C_SMBUS_QUICK:
Ben Gardner99c3adb2006-01-18 22:41:50 +0100295 len = 0;
296 buffer = NULL;
297 break;
298
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 case I2C_SMBUS_BYTE:
300 if (rw == I2C_SMBUS_READ) {
301 len = 1;
302 buffer = &data->byte;
303 } else {
304 len = 1;
305 buffer = &command;
306 }
Ben Gardner99c3adb2006-01-18 22:41:50 +0100307 break;
308
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 case I2C_SMBUS_BYTE_DATA:
Ben Gardner99c3adb2006-01-18 22:41:50 +0100310 len = 1;
311 buffer = &data->byte;
312 break;
313
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 case I2C_SMBUS_WORD_DATA:
315 len = 2;
Ben Gardner99c3adb2006-01-18 22:41:50 +0100316 cur_word = cpu_to_le16(data->word);
317 buffer = (u8 *)&cur_word;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 break;
Ben Gardner99c3adb2006-01-18 22:41:50 +0100319
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 case I2C_SMBUS_BLOCK_DATA:
Ben Gardner99c3adb2006-01-18 22:41:50 +0100321 len = data->block[0];
322 buffer = &data->block[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 break;
Ben Gardner99c3adb2006-01-18 22:41:50 +0100324
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 default:
Ben Gardner99c3adb2006-01-18 22:41:50 +0100326 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 }
328
329 DBG("size=%d, address=0x%x, command=0x%x, len=%d, read=%d\n",
330 size, address, command, len, rw == I2C_SMBUS_READ);
331
332 if (!len && rw == I2C_SMBUS_READ) {
333 dev_warn(&adapter->dev, "zero length read\n");
334 return -EINVAL;
335 }
336
337 if (len && !buffer) {
338 dev_warn(&adapter->dev, "nonzero length but no buffer\n");
339 return -EFAULT;
340 }
341
342 down(&iface->sem);
343
344 iface->address_byte = address<<1;
345 if (rw == I2C_SMBUS_READ)
346 iface->address_byte |= 1;
347 iface->command = command;
348 iface->ptr = buffer;
349 iface->len = len;
350 iface->result = -EINVAL;
351 iface->needs_reset = 0;
352
353 outb(inb(ACBCTL1) | ACBCTL1_START, ACBCTL1);
354
355 if (size == I2C_SMBUS_QUICK || size == I2C_SMBUS_BYTE)
356 iface->state = state_quick;
357 else
358 iface->state = state_address;
359
360#ifdef POLLED_MODE
361 while (iface->state != state_idle)
362 scx200_acb_poll(iface);
363#else /* POLLED_MODE */
364#error Interrupt driven mode not implemented
365#endif /* POLLED_MODE */
366
367 if (iface->needs_reset)
368 scx200_acb_reset(iface);
369
370 rc = iface->result;
371
372 up(&iface->sem);
373
374 if (rc == 0 && size == I2C_SMBUS_WORD_DATA && rw == I2C_SMBUS_READ)
Ben Gardner99c3adb2006-01-18 22:41:50 +0100375 data->word = le16_to_cpu(cur_word);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
377#ifdef DEBUG
378 DBG(": transfer done, result: %d", rc);
379 if (buffer) {
380 int i;
381 printk(" data:");
382 for (i = 0; i < len; ++i)
383 printk(" %02x", buffer[i]);
384 }
385 printk("\n");
386#endif
387
388 return rc;
389}
390
391static u32 scx200_acb_func(struct i2c_adapter *adapter)
392{
393 return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
394 I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
395 I2C_FUNC_SMBUS_BLOCK_DATA;
396}
397
398/* For now, we only handle combined mode (smbus) */
399static struct i2c_algorithm scx200_acb_algorithm = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 .smbus_xfer = scx200_acb_smbus_xfer,
401 .functionality = scx200_acb_func,
402};
403
404static struct scx200_acb_iface *scx200_acb_list;
405
406static int scx200_acb_probe(struct scx200_acb_iface *iface)
407{
408 u8 val;
409
410 /* Disable the ACCESS.bus device and Configure the SCL
Ben Gardner99c3adb2006-01-18 22:41:50 +0100411 frequency: 16 clock cycles */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 outb(0x70, ACBCTL2);
413
414 if (inb(ACBCTL2) != 0x70) {
415 DBG("ACBCTL2 readback failed\n");
416 return -ENXIO;
417 }
418
419 outb(inb(ACBCTL1) | ACBCTL1_NMINTE, ACBCTL1);
420
421 val = inb(ACBCTL1);
422 if (val) {
423 DBG("disabled, but ACBCTL1=0x%02x\n", val);
424 return -ENXIO;
425 }
426
427 outb(inb(ACBCTL2) | ACBCTL2_ENABLE, ACBCTL2);
428
429 outb(inb(ACBCTL1) | ACBCTL1_NMINTE, ACBCTL1);
430
431 val = inb(ACBCTL1);
432 if ((val & ACBCTL1_NMINTE) != ACBCTL1_NMINTE) {
433 DBG("enabled, but NMINTE won't be set, ACBCTL1=0x%02x\n", val);
434 return -ENXIO;
435 }
436
437 return 0;
438}
439
440static int __init scx200_acb_create(int base, int index)
441{
442 struct scx200_acb_iface *iface;
443 struct i2c_adapter *adapter;
444 int rc = 0;
445 char description[64];
446
Deepak Saxena5263ebb2005-10-17 23:09:43 +0200447 iface = kzalloc(sizeof(*iface), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 if (!iface) {
449 printk(KERN_ERR NAME ": can't allocate memory\n");
450 rc = -ENOMEM;
451 goto errout;
452 }
453
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 adapter = &iface->adapter;
455 i2c_set_adapdata(adapter, iface);
456 snprintf(adapter->name, I2C_NAME_SIZE, "SCx200 ACB%d", index);
457 adapter->owner = THIS_MODULE;
Jean Delvare1684a9842005-08-11 23:51:10 +0200458 adapter->id = I2C_HW_SMBUS_SCX200;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 adapter->algo = &scx200_acb_algorithm;
460 adapter->class = I2C_CLASS_HWMON;
461
462 init_MUTEX(&iface->sem);
463
Ben Gardner99c3adb2006-01-18 22:41:50 +0100464 snprintf(description, sizeof(description),
465 "NatSemi SCx200 ACCESS.bus [%s]", adapter->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 if (request_region(base, 8, description) == 0) {
467 dev_err(&adapter->dev, "can't allocate io 0x%x-0x%x\n",
468 base, base + 8-1);
469 rc = -EBUSY;
470 goto errout;
471 }
472 iface->base = base;
473
474 rc = scx200_acb_probe(iface);
475 if (rc) {
476 dev_warn(&adapter->dev, "probe failed\n");
477 goto errout;
478 }
479
480 scx200_acb_reset(iface);
481
482 if (i2c_add_adapter(adapter) < 0) {
483 dev_err(&adapter->dev, "failed to register\n");
484 rc = -ENODEV;
485 goto errout;
486 }
487
488 lock_kernel();
489 iface->next = scx200_acb_list;
490 scx200_acb_list = iface;
491 unlock_kernel();
492
493 return 0;
494
495 errout:
496 if (iface) {
497 if (iface->base)
498 release_region(iface->base, 8);
499 kfree(iface);
500 }
501 return rc;
502}
503
504static struct pci_device_id scx200[] = {
505 { PCI_DEVICE(PCI_VENDOR_ID_NS, PCI_DEVICE_ID_NS_SCx200_BRIDGE) },
506 { PCI_DEVICE(PCI_VENDOR_ID_NS, PCI_DEVICE_ID_NS_SC1100_BRIDGE) },
507 { },
508};
509
510static int __init scx200_acb_init(void)
511{
512 int i;
513 int rc;
514
515 pr_debug(NAME ": NatSemi SCx200 ACCESS.bus Driver\n");
516
517 /* Verify that this really is a SCx200 processor */
518 if (pci_dev_present(scx200) == 0)
519 return -ENODEV;
520
521 rc = -ENXIO;
522 for (i = 0; i < MAX_DEVICES; ++i) {
523 if (base[i] > 0)
524 rc = scx200_acb_create(base[i], i);
525 }
526 if (scx200_acb_list)
527 return 0;
528 return rc;
529}
530
531static void __exit scx200_acb_cleanup(void)
532{
533 struct scx200_acb_iface *iface;
Ben Gardner99c3adb2006-01-18 22:41:50 +0100534
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 lock_kernel();
536 while ((iface = scx200_acb_list) != NULL) {
537 scx200_acb_list = iface->next;
538 unlock_kernel();
539
540 i2c_del_adapter(&iface->adapter);
541 release_region(iface->base, 8);
542 kfree(iface);
543 lock_kernel();
544 }
545 unlock_kernel();
546}
547
548module_init(scx200_acb_init);
549module_exit(scx200_acb_cleanup);