blob: bfa243d5a8f2880231ff1aa4df48af426670247d [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070048/* The hardware supports interrupt driven mode too, but I haven't
49 implemented that. */
50#define POLLED_MODE 1
51#define POLL_TIMEOUT (HZ)
52
53enum scx200_acb_state {
54 state_idle,
55 state_address,
56 state_command,
57 state_repeat_start,
58 state_quick,
59 state_read,
60 state_write,
61};
62
63static const char *scx200_acb_state_name[] = {
64 "idle",
65 "address",
66 "command",
67 "repeat_start",
68 "quick",
69 "read",
70 "write",
71};
72
73/* Physical interface */
Ben Gardner99c3adb2006-01-18 22:41:50 +010074struct scx200_acb_iface {
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 struct scx200_acb_iface *next;
76 struct i2c_adapter adapter;
77 unsigned base;
78 struct semaphore sem;
79
80 /* State machine data */
81 enum scx200_acb_state state;
82 int result;
83 u8 address_byte;
84 u8 command;
85 u8 *ptr;
86 char needs_reset;
87 unsigned len;
88};
89
90/* Register Definitions */
91#define ACBSDA (iface->base + 0)
92#define ACBST (iface->base + 1)
93#define ACBST_SDAST 0x40 /* SDA Status */
Ben Gardner99c3adb2006-01-18 22:41:50 +010094#define ACBST_BER 0x20
Linus Torvalds1da177e2005-04-16 15:20:36 -070095#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
Ben Gardner99c3adb2006-01-18 22:41:50 +0100103#define ACBCTL1_ACK 0x10
104#define ACBCTL1_STOP 0x02
105#define ACBCTL1_START 0x01
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106#define ACBADDR (iface->base + 4)
107#define ACBCTL2 (iface->base + 5)
108#define ACBCTL2_ENABLE 0x01
109
110/************************************************************************/
111
112static void scx200_acb_machine(struct scx200_acb_iface *iface, u8 status)
113{
114 const char *errmsg;
115
Ben Gardneref4d9272006-01-18 22:43:10 +0100116 dev_dbg(&iface->adapter.dev, "state %s, status = 0x%02x\n",
117 scx200_acb_state_name[iface->state], status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
119 if (status & ACBST_BER) {
120 errmsg = "bus error";
121 goto error;
122 }
123 if (!(status & ACBST_MASTER)) {
124 errmsg = "not master";
125 goto error;
126 }
127 if (status & ACBST_NEGACK)
128 goto negack;
129
130 switch (iface->state) {
131 case state_idle:
132 dev_warn(&iface->adapter.dev, "interrupt in idle state\n");
133 break;
134
135 case state_address:
136 /* Do a pointer write first */
137 outb(iface->address_byte & ~1, ACBSDA);
138
139 iface->state = state_command;
140 break;
141
142 case state_command:
143 outb(iface->command, ACBSDA);
144
145 if (iface->address_byte & 1)
146 iface->state = state_repeat_start;
147 else
148 iface->state = state_write;
149 break;
150
151 case state_repeat_start:
152 outb(inb(ACBCTL1) | ACBCTL1_START, ACBCTL1);
153 /* fallthrough */
Ben Gardner99c3adb2006-01-18 22:41:50 +0100154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 case state_quick:
156 if (iface->address_byte & 1) {
Ben Gardner99c3adb2006-01-18 22:41:50 +0100157 if (iface->len == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 outb(inb(ACBCTL1) | ACBCTL1_ACK, ACBCTL1);
159 else
160 outb(inb(ACBCTL1) & ~ACBCTL1_ACK, ACBCTL1);
161 outb(iface->address_byte, ACBSDA);
162
163 iface->state = state_read;
164 } else {
165 outb(iface->address_byte, ACBSDA);
166
167 iface->state = state_write;
168 }
169 break;
170
171 case state_read:
172 /* Set ACK if receiving the last byte */
173 if (iface->len == 1)
174 outb(inb(ACBCTL1) | ACBCTL1_ACK, ACBCTL1);
175 else
176 outb(inb(ACBCTL1) & ~ACBCTL1_ACK, ACBCTL1);
177
178 *iface->ptr++ = inb(ACBSDA);
179 --iface->len;
180
181 if (iface->len == 0) {
182 iface->result = 0;
183 iface->state = state_idle;
184 outb(inb(ACBCTL1) | ACBCTL1_STOP, ACBCTL1);
185 }
186
187 break;
188
189 case state_write:
190 if (iface->len == 0) {
191 iface->result = 0;
192 iface->state = state_idle;
193 outb(inb(ACBCTL1) | ACBCTL1_STOP, ACBCTL1);
194 break;
195 }
Ben Gardner99c3adb2006-01-18 22:41:50 +0100196
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 outb(*iface->ptr++, ACBSDA);
198 --iface->len;
Ben Gardner99c3adb2006-01-18 22:41:50 +0100199
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 break;
201 }
202
203 return;
204
205 negack:
Ben Gardneref4d9272006-01-18 22:43:10 +0100206 dev_dbg(&iface->adapter.dev, "negative ack in state %s\n",
207 scx200_acb_state_name[iface->state]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
209 iface->state = state_idle;
210 iface->result = -ENXIO;
211
212 outb(inb(ACBCTL1) | ACBCTL1_STOP, ACBCTL1);
213 outb(ACBST_STASTR | ACBST_NEGACK, ACBST);
214 return;
215
216 error:
217 dev_err(&iface->adapter.dev, "%s in state %s\n", errmsg,
218 scx200_acb_state_name[iface->state]);
219
220 iface->state = state_idle;
221 iface->result = -EIO;
222 iface->needs_reset = 1;
223}
224
Ben Gardner99c3adb2006-01-18 22:41:50 +0100225static void scx200_acb_timeout(struct scx200_acb_iface *iface)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226{
227 dev_err(&iface->adapter.dev, "timeout in state %s\n",
228 scx200_acb_state_name[iface->state]);
229
230 iface->state = state_idle;
231 iface->result = -EIO;
232 iface->needs_reset = 1;
233}
234
235#ifdef POLLED_MODE
236static void scx200_acb_poll(struct scx200_acb_iface *iface)
237{
238 u8 status = 0;
239 unsigned long timeout;
240
241 timeout = jiffies + POLL_TIMEOUT;
242 while (time_before(jiffies, timeout)) {
243 status = inb(ACBST);
244 if ((status & (ACBST_SDAST|ACBST_BER|ACBST_NEGACK)) != 0) {
245 scx200_acb_machine(iface, status);
246 return;
247 }
248 msleep(10);
249 }
250
251 scx200_acb_timeout(iface);
252}
253#endif /* POLLED_MODE */
254
255static void scx200_acb_reset(struct scx200_acb_iface *iface)
256{
257 /* Disable the ACCESS.bus device and Configure the SCL
Ben Gardner99c3adb2006-01-18 22:41:50 +0100258 frequency: 16 clock cycles */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 outb(0x70, ACBCTL2);
260 /* Polling mode */
261 outb(0, ACBCTL1);
262 /* Disable slave address */
263 outb(0, ACBADDR);
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);
268 /* Send a STOP */
269 outb(inb(ACBCTL1) | ACBCTL1_STOP, ACBCTL1);
270 /* Clear BER, NEGACK and STASTR bits */
271 outb(ACBST_BER | ACBST_NEGACK | ACBST_STASTR, ACBST);
272 /* Clear BB bit */
273 outb(inb(ACBCST) | ACBCST_BB, ACBCST);
274}
275
276static s32 scx200_acb_smbus_xfer(struct i2c_adapter *adapter,
Ben Gardner99c3adb2006-01-18 22:41:50 +0100277 u16 address, unsigned short flags,
278 char rw, u8 command, int size,
279 union i2c_smbus_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280{
281 struct scx200_acb_iface *iface = i2c_get_adapdata(adapter);
282 int len;
283 u8 *buffer;
284 u16 cur_word;
285 int rc;
286
287 switch (size) {
288 case I2C_SMBUS_QUICK:
Ben Gardner99c3adb2006-01-18 22:41:50 +0100289 len = 0;
290 buffer = NULL;
291 break;
292
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 case I2C_SMBUS_BYTE:
294 if (rw == I2C_SMBUS_READ) {
295 len = 1;
296 buffer = &data->byte;
297 } else {
298 len = 1;
299 buffer = &command;
300 }
Ben Gardner99c3adb2006-01-18 22:41:50 +0100301 break;
302
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 case I2C_SMBUS_BYTE_DATA:
Ben Gardner99c3adb2006-01-18 22:41:50 +0100304 len = 1;
305 buffer = &data->byte;
306 break;
307
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 case I2C_SMBUS_WORD_DATA:
309 len = 2;
Ben Gardner99c3adb2006-01-18 22:41:50 +0100310 cur_word = cpu_to_le16(data->word);
311 buffer = (u8 *)&cur_word;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 break;
Ben Gardner99c3adb2006-01-18 22:41:50 +0100313
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 case I2C_SMBUS_BLOCK_DATA:
Ben Gardner99c3adb2006-01-18 22:41:50 +0100315 len = data->block[0];
316 buffer = &data->block[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 break;
Ben Gardner99c3adb2006-01-18 22:41:50 +0100318
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 default:
Ben Gardner99c3adb2006-01-18 22:41:50 +0100320 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 }
322
Ben Gardneref4d9272006-01-18 22:43:10 +0100323 dev_dbg(&adapter->dev,
324 "size=%d, address=0x%x, command=0x%x, len=%d, read=%d\n",
325 size, address, command, len, rw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
327 if (!len && rw == I2C_SMBUS_READ) {
Ben Gardneref4d9272006-01-18 22:43:10 +0100328 dev_dbg(&adapter->dev, "zero length read\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 return -EINVAL;
330 }
331
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 down(&iface->sem);
333
334 iface->address_byte = address<<1;
335 if (rw == I2C_SMBUS_READ)
336 iface->address_byte |= 1;
337 iface->command = command;
338 iface->ptr = buffer;
339 iface->len = len;
340 iface->result = -EINVAL;
341 iface->needs_reset = 0;
342
343 outb(inb(ACBCTL1) | ACBCTL1_START, ACBCTL1);
344
345 if (size == I2C_SMBUS_QUICK || size == I2C_SMBUS_BYTE)
346 iface->state = state_quick;
347 else
348 iface->state = state_address;
349
350#ifdef POLLED_MODE
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 */
356
357 if (iface->needs_reset)
358 scx200_acb_reset(iface);
359
360 rc = iface->result;
361
362 up(&iface->sem);
363
364 if (rc == 0 && size == I2C_SMBUS_WORD_DATA && rw == I2C_SMBUS_READ)
Ben Gardner99c3adb2006-01-18 22:41:50 +0100365 data->word = le16_to_cpu(cur_word);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
367#ifdef DEBUG
Ben Gardneref4d9272006-01-18 22:43:10 +0100368 dev_dbg(&adapter->dev, "transfer done, result: %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 if (buffer) {
370 int i;
371 printk(" data:");
372 for (i = 0; i < len; ++i)
373 printk(" %02x", buffer[i]);
374 }
375 printk("\n");
376#endif
377
378 return rc;
379}
380
381static u32 scx200_acb_func(struct i2c_adapter *adapter)
382{
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;
386}
387
388/* For now, we only handle combined mode (smbus) */
389static struct i2c_algorithm scx200_acb_algorithm = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 .smbus_xfer = scx200_acb_smbus_xfer,
391 .functionality = scx200_acb_func,
392};
393
394static struct scx200_acb_iface *scx200_acb_list;
395
396static int scx200_acb_probe(struct scx200_acb_iface *iface)
397{
398 u8 val;
399
400 /* Disable the ACCESS.bus device and Configure the SCL
Ben Gardner99c3adb2006-01-18 22:41:50 +0100401 frequency: 16 clock cycles */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 outb(0x70, ACBCTL2);
403
404 if (inb(ACBCTL2) != 0x70) {
Ben Gardneref4d9272006-01-18 22:43:10 +0100405 pr_debug(NAME ": ACBCTL2 readback failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 return -ENXIO;
407 }
408
409 outb(inb(ACBCTL1) | ACBCTL1_NMINTE, ACBCTL1);
410
411 val = inb(ACBCTL1);
412 if (val) {
Ben Gardneref4d9272006-01-18 22:43:10 +0100413 pr_debug(NAME ": disabled, but ACBCTL1=0x%02x\n",
414 val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 return -ENXIO;
416 }
417
418 outb(inb(ACBCTL2) | ACBCTL2_ENABLE, ACBCTL2);
419
420 outb(inb(ACBCTL1) | ACBCTL1_NMINTE, ACBCTL1);
421
422 val = inb(ACBCTL1);
423 if ((val & ACBCTL1_NMINTE) != ACBCTL1_NMINTE) {
Ben Gardneref4d9272006-01-18 22:43:10 +0100424 pr_debug(NAME ": enabled, but NMINTE won't be set, "
425 "ACBCTL1=0x%02x\n", val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 return -ENXIO;
427 }
428
429 return 0;
430}
431
432static int __init scx200_acb_create(int base, int index)
433{
434 struct scx200_acb_iface *iface;
435 struct i2c_adapter *adapter;
436 int rc = 0;
437 char description[64];
438
Deepak Saxena5263ebb2005-10-17 23:09:43 +0200439 iface = kzalloc(sizeof(*iface), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 if (!iface) {
441 printk(KERN_ERR NAME ": can't allocate memory\n");
442 rc = -ENOMEM;
443 goto errout;
444 }
445
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 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;
Jean Delvare1684a9842005-08-11 23:51:10 +0200450 adapter->id = I2C_HW_SMBUS_SCX200;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 adapter->algo = &scx200_acb_algorithm;
452 adapter->class = I2C_CLASS_HWMON;
453
454 init_MUTEX(&iface->sem);
455
Ben Gardner99c3adb2006-01-18 22:41:50 +0100456 snprintf(description, sizeof(description),
457 "NatSemi SCx200 ACCESS.bus [%s]", adapter->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 if (request_region(base, 8, description) == 0) {
Ben Gardneref4d9272006-01-18 22:43:10 +0100459 printk(KERN_ERR NAME ": can't allocate io 0x%x-0x%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 base, base + 8-1);
461 rc = -EBUSY;
462 goto errout;
463 }
464 iface->base = base;
465
466 rc = scx200_acb_probe(iface);
467 if (rc) {
Ben Gardneref4d9272006-01-18 22:43:10 +0100468 printk(KERN_WARNING NAME ": probe failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 goto errout;
470 }
471
472 scx200_acb_reset(iface);
473
474 if (i2c_add_adapter(adapter) < 0) {
Ben Gardneref4d9272006-01-18 22:43:10 +0100475 printk(KERN_ERR NAME ": failed to register\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 rc = -ENODEV;
477 goto errout;
478 }
479
480 lock_kernel();
481 iface->next = scx200_acb_list;
482 scx200_acb_list = iface;
483 unlock_kernel();
484
485 return 0;
486
487 errout:
488 if (iface) {
489 if (iface->base)
490 release_region(iface->base, 8);
491 kfree(iface);
492 }
493 return rc;
494}
495
496static 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) },
499 { },
500};
501
502static int __init scx200_acb_init(void)
503{
504 int i;
505 int rc;
506
507 pr_debug(NAME ": NatSemi SCx200 ACCESS.bus Driver\n");
508
509 /* Verify that this really is a SCx200 processor */
510 if (pci_dev_present(scx200) == 0)
511 return -ENODEV;
512
513 rc = -ENXIO;
514 for (i = 0; i < MAX_DEVICES; ++i) {
515 if (base[i] > 0)
516 rc = scx200_acb_create(base[i], i);
517 }
518 if (scx200_acb_list)
519 return 0;
520 return rc;
521}
522
523static void __exit scx200_acb_cleanup(void)
524{
525 struct scx200_acb_iface *iface;
Ben Gardner99c3adb2006-01-18 22:41:50 +0100526
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 lock_kernel();
528 while ((iface = scx200_acb_list) != NULL) {
529 scx200_acb_list = iface->next;
530 unlock_kernel();
531
532 i2c_del_adapter(&iface->adapter);
533 release_region(iface->base, 8);
534 kfree(iface);
535 lock_kernel();
536 }
537 unlock_kernel();
538}
539
540module_init(scx200_acb_init);
541module_exit(scx200_acb_cleanup);