blob: 4cb4bb009950ca98aec8aedf901629924cadff72 [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 Gardner16ffc5c2006-01-18 22:48:26 +01005 Also supports the AMD CS5535 and AMD CS5536
Ben Gardner99c3adb2006-01-18 22:41:50 +01006
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 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>
Ben Gardner99c3adb2006-01-18 22:41:50 +010010
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 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.
Ben Gardner99c3adb2006-01-18 22:41:50 +010015
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 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.
Ben Gardner99c3adb2006-01-18 22:41:50 +010020
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070024*/
25
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/module.h>
27#include <linux/errno.h>
28#include <linux/kernel.h>
29#include <linux/init.h>
30#include <linux/i2c.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/pci.h>
32#include <linux/delay.h>
Jean Delvare3fb9a652006-01-18 23:17:01 +010033#include <linux/mutex.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090034#include <linux/slab.h>
H Hartley Sweeten21782182010-05-21 18:41:01 +020035#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37#include <linux/scx200.h>
38
39#define NAME "scx200_acb"
40
41MODULE_AUTHOR("Christer Weinigel <wingel@nano-system.com>");
42MODULE_DESCRIPTION("NatSemi SCx200 ACCESS.bus Driver");
43MODULE_LICENSE("GPL");
44
45#define MAX_DEVICES 4
46static int base[MAX_DEVICES] = { 0x820, 0x840 };
47module_param_array(base, int, NULL, 0);
48MODULE_PARM_DESC(base, "Base addresses for the ACCESS.bus controllers");
49
Ben Gardnerf933ff52006-01-18 22:52:06 +010050#define POLL_TIMEOUT (HZ/5)
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52enum scx200_acb_state {
53 state_idle,
54 state_address,
55 state_command,
56 state_repeat_start,
57 state_quick,
58 state_read,
59 state_write,
60};
61
62static const char *scx200_acb_state_name[] = {
63 "idle",
64 "address",
65 "command",
66 "repeat_start",
67 "quick",
68 "read",
69 "write",
70};
71
72/* Physical interface */
Ben Gardner99c3adb2006-01-18 22:41:50 +010073struct scx200_acb_iface {
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 struct scx200_acb_iface *next;
75 struct i2c_adapter adapter;
76 unsigned base;
Jean Delvare3fb9a652006-01-18 23:17:01 +010077 struct mutex mutex;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79 /* State machine data */
80 enum scx200_acb_state state;
81 int result;
82 u8 address_byte;
83 u8 command;
84 u8 *ptr;
85 char needs_reset;
86 unsigned len;
Jordan Crouse80cd3a82006-06-12 21:44:28 +020087
88 /* PCI device info */
89 struct pci_dev *pdev;
90 int bar;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091};
92
93/* Register Definitions */
94#define ACBSDA (iface->base + 0)
95#define ACBST (iface->base + 1)
96#define ACBST_SDAST 0x40 /* SDA Status */
Ben Gardner99c3adb2006-01-18 22:41:50 +010097#define ACBST_BER 0x20
Linus Torvalds1da177e2005-04-16 15:20:36 -070098#define ACBST_NEGACK 0x10 /* Negative Acknowledge */
99#define ACBST_STASTR 0x08 /* Stall After Start */
100#define ACBST_MASTER 0x02
101#define ACBCST (iface->base + 2)
102#define ACBCST_BB 0x02
103#define ACBCTL1 (iface->base + 3)
104#define ACBCTL1_STASTRE 0x80
105#define ACBCTL1_NMINTE 0x40
Ben Gardner99c3adb2006-01-18 22:41:50 +0100106#define ACBCTL1_ACK 0x10
107#define ACBCTL1_STOP 0x02
108#define ACBCTL1_START 0x01
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109#define ACBADDR (iface->base + 4)
110#define ACBCTL2 (iface->base + 5)
111#define ACBCTL2_ENABLE 0x01
112
113/************************************************************************/
114
115static void scx200_acb_machine(struct scx200_acb_iface *iface, u8 status)
116{
117 const char *errmsg;
118
Ben Gardneref4d9272006-01-18 22:43:10 +0100119 dev_dbg(&iface->adapter.dev, "state %s, status = 0x%02x\n",
120 scx200_acb_state_name[iface->state], status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
122 if (status & ACBST_BER) {
123 errmsg = "bus error";
124 goto error;
125 }
126 if (!(status & ACBST_MASTER)) {
127 errmsg = "not master";
128 goto error;
129 }
Ben Gardner9b7b6d32006-01-18 22:44:04 +0100130 if (status & ACBST_NEGACK) {
131 dev_dbg(&iface->adapter.dev, "negative ack in state %s\n",
132 scx200_acb_state_name[iface->state]);
133
134 iface->state = state_idle;
135 iface->result = -ENXIO;
136
137 outb(inb(ACBCTL1) | ACBCTL1_STOP, ACBCTL1);
138 outb(ACBST_STASTR | ACBST_NEGACK, ACBST);
Jordan Crouse95563d32006-04-28 22:53:30 +0200139
140 /* Reset the status register */
141 outb(0, ACBST);
Ben Gardner9b7b6d32006-01-18 22:44:04 +0100142 return;
143 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
145 switch (iface->state) {
146 case state_idle:
147 dev_warn(&iface->adapter.dev, "interrupt in idle state\n");
148 break;
149
150 case state_address:
151 /* Do a pointer write first */
152 outb(iface->address_byte & ~1, ACBSDA);
153
154 iface->state = state_command;
155 break;
156
157 case state_command:
158 outb(iface->command, ACBSDA);
159
160 if (iface->address_byte & 1)
161 iface->state = state_repeat_start;
162 else
163 iface->state = state_write;
164 break;
165
166 case state_repeat_start:
167 outb(inb(ACBCTL1) | ACBCTL1_START, ACBCTL1);
168 /* fallthrough */
Ben Gardner99c3adb2006-01-18 22:41:50 +0100169
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 case state_quick:
171 if (iface->address_byte & 1) {
Ben Gardner99c3adb2006-01-18 22:41:50 +0100172 if (iface->len == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 outb(inb(ACBCTL1) | ACBCTL1_ACK, ACBCTL1);
174 else
175 outb(inb(ACBCTL1) & ~ACBCTL1_ACK, ACBCTL1);
176 outb(iface->address_byte, ACBSDA);
177
178 iface->state = state_read;
179 } else {
180 outb(iface->address_byte, ACBSDA);
181
182 iface->state = state_write;
183 }
184 break;
185
186 case state_read:
Thomas Andrewsfd627a02006-07-01 17:05:12 +0200187 /* Set ACK if _next_ byte will be the last one */
188 if (iface->len == 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 outb(inb(ACBCTL1) | ACBCTL1_ACK, ACBCTL1);
190 else
191 outb(inb(ACBCTL1) & ~ACBCTL1_ACK, ACBCTL1);
192
Thomas Andrewsfd627a02006-07-01 17:05:12 +0200193 if (iface->len == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 iface->result = 0;
195 iface->state = state_idle;
196 outb(inb(ACBCTL1) | ACBCTL1_STOP, ACBCTL1);
197 }
198
Thomas Andrewsfd627a02006-07-01 17:05:12 +0200199 *iface->ptr++ = inb(ACBSDA);
200 --iface->len;
201
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 break;
203
204 case state_write:
205 if (iface->len == 0) {
206 iface->result = 0;
207 iface->state = state_idle;
208 outb(inb(ACBCTL1) | ACBCTL1_STOP, ACBCTL1);
209 break;
210 }
Ben Gardner99c3adb2006-01-18 22:41:50 +0100211
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 outb(*iface->ptr++, ACBSDA);
213 --iface->len;
Ben Gardner99c3adb2006-01-18 22:41:50 +0100214
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 break;
216 }
217
218 return;
219
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 error:
Willy Tarreaufce96f32009-09-18 22:45:47 +0200221 dev_err(&iface->adapter.dev,
222 "%s in state %s (addr=0x%02x, len=%d, status=0x%02x)\n", errmsg,
223 scx200_acb_state_name[iface->state], iface->address_byte,
224 iface->len, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
226 iface->state = state_idle;
227 iface->result = -EIO;
228 iface->needs_reset = 1;
229}
230
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231static void scx200_acb_poll(struct scx200_acb_iface *iface)
232{
Ben Gardner9b7b6d32006-01-18 22:44:04 +0100233 u8 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 unsigned long timeout;
235
236 timeout = jiffies + POLL_TIMEOUT;
David Woodhouse3e3183b2006-08-05 12:15:19 -0700237 while (1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 status = inb(ACBST);
Jordan Crouse95563d32006-04-28 22:53:30 +0200239
240 /* Reset the status register to avoid the hang */
241 outb(0, ACBST);
242
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 if ((status & (ACBST_SDAST|ACBST_BER|ACBST_NEGACK)) != 0) {
244 scx200_acb_machine(iface, status);
245 return;
246 }
David Woodhouse3e3183b2006-08-05 12:15:19 -0700247 if (time_after(jiffies, timeout))
248 break;
249 cpu_relax();
250 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 }
252
Ben Gardner9b7b6d32006-01-18 22:44:04 +0100253 dev_err(&iface->adapter.dev, "timeout in state %s\n",
254 scx200_acb_state_name[iface->state]);
255
256 iface->state = state_idle;
257 iface->result = -EIO;
258 iface->needs_reset = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
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:
Ben Gardner9b7b6d32006-01-18 22:44:04 +0100300 len = 1;
301 buffer = rw ? &data->byte : &command;
Ben Gardner99c3adb2006-01-18 22:41:50 +0100302 break;
303
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 case I2C_SMBUS_BYTE_DATA:
Ben Gardner99c3adb2006-01-18 22:41:50 +0100305 len = 1;
306 buffer = &data->byte;
307 break;
308
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 case I2C_SMBUS_WORD_DATA:
310 len = 2;
Ben Gardner99c3adb2006-01-18 22:41:50 +0100311 cur_word = cpu_to_le16(data->word);
312 buffer = (u8 *)&cur_word;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 break;
Ben Gardner99c3adb2006-01-18 22:41:50 +0100314
Jean Delvarec3efaca2006-07-01 17:06:43 +0200315 case I2C_SMBUS_I2C_BLOCK_DATA:
Ben Gardner99c3adb2006-01-18 22:41:50 +0100316 len = data->block[0];
Jean Delvarec3efaca2006-07-01 17:06:43 +0200317 if (len == 0 || len > I2C_SMBUS_BLOCK_MAX)
318 return -EINVAL;
Ben Gardner99c3adb2006-01-18 22:41:50 +0100319 buffer = &data->block[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 break;
Ben Gardner99c3adb2006-01-18 22:41:50 +0100321
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 default:
Ben Gardner99c3adb2006-01-18 22:41:50 +0100323 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 }
325
Ben Gardneref4d9272006-01-18 22:43:10 +0100326 dev_dbg(&adapter->dev,
327 "size=%d, address=0x%x, command=0x%x, len=%d, read=%d\n",
328 size, address, command, len, rw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
330 if (!len && rw == I2C_SMBUS_READ) {
Ben Gardneref4d9272006-01-18 22:43:10 +0100331 dev_dbg(&adapter->dev, "zero length read\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 return -EINVAL;
333 }
334
Jean Delvare3fb9a652006-01-18 23:17:01 +0100335 mutex_lock(&iface->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
Ben Gardner9b7b6d32006-01-18 22:44:04 +0100337 iface->address_byte = (address << 1) | rw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 iface->command = command;
339 iface->ptr = buffer;
340 iface->len = len;
341 iface->result = -EINVAL;
342 iface->needs_reset = 0;
343
344 outb(inb(ACBCTL1) | ACBCTL1_START, ACBCTL1);
345
346 if (size == I2C_SMBUS_QUICK || size == I2C_SMBUS_BYTE)
347 iface->state = state_quick;
348 else
349 iface->state = state_address;
350
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 while (iface->state != state_idle)
352 scx200_acb_poll(iface);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
354 if (iface->needs_reset)
355 scx200_acb_reset(iface);
356
357 rc = iface->result;
358
Jean Delvare3fb9a652006-01-18 23:17:01 +0100359 mutex_unlock(&iface->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
361 if (rc == 0 && size == I2C_SMBUS_WORD_DATA && rw == I2C_SMBUS_READ)
Ben Gardner99c3adb2006-01-18 22:41:50 +0100362 data->word = le16_to_cpu(cur_word);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
364#ifdef DEBUG
Ben Gardneref4d9272006-01-18 22:43:10 +0100365 dev_dbg(&adapter->dev, "transfer done, result: %d", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 if (buffer) {
367 int i;
368 printk(" data:");
369 for (i = 0; i < len; ++i)
370 printk(" %02x", buffer[i]);
371 }
372 printk("\n");
373#endif
374
375 return rc;
376}
377
378static u32 scx200_acb_func(struct i2c_adapter *adapter)
379{
380 return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
381 I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
Jean Delvarec3efaca2006-07-01 17:06:43 +0200382 I2C_FUNC_SMBUS_I2C_BLOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383}
384
385/* For now, we only handle combined mode (smbus) */
Jean Delvare8f9082c2006-09-03 22:39:46 +0200386static const struct i2c_algorithm scx200_acb_algorithm = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 .smbus_xfer = scx200_acb_smbus_xfer,
388 .functionality = scx200_acb_func,
389};
390
391static struct scx200_acb_iface *scx200_acb_list;
Matthias Kaehlcke9d9c01c2007-07-12 14:12:28 +0200392static DEFINE_MUTEX(scx200_acb_list_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
Jean Delvare99173922006-06-12 21:46:04 +0200394static __init int scx200_acb_probe(struct scx200_acb_iface *iface)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395{
396 u8 val;
397
398 /* Disable the ACCESS.bus device and Configure the SCL
Ben Gardner99c3adb2006-01-18 22:41:50 +0100399 frequency: 16 clock cycles */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 outb(0x70, ACBCTL2);
401
402 if (inb(ACBCTL2) != 0x70) {
Ben Gardneref4d9272006-01-18 22:43:10 +0100403 pr_debug(NAME ": ACBCTL2 readback failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 return -ENXIO;
405 }
406
407 outb(inb(ACBCTL1) | ACBCTL1_NMINTE, ACBCTL1);
408
409 val = inb(ACBCTL1);
410 if (val) {
Ben Gardneref4d9272006-01-18 22:43:10 +0100411 pr_debug(NAME ": disabled, but ACBCTL1=0x%02x\n",
412 val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 return -ENXIO;
414 }
415
416 outb(inb(ACBCTL2) | ACBCTL2_ENABLE, ACBCTL2);
417
418 outb(inb(ACBCTL1) | ACBCTL1_NMINTE, ACBCTL1);
419
420 val = inb(ACBCTL1);
421 if ((val & ACBCTL1_NMINTE) != ACBCTL1_NMINTE) {
Ben Gardneref4d9272006-01-18 22:43:10 +0100422 pr_debug(NAME ": enabled, but NMINTE won't be set, "
423 "ACBCTL1=0x%02x\n", val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 return -ENXIO;
425 }
426
427 return 0;
428}
429
Jordan Crouse80cd3a82006-06-12 21:44:28 +0200430static __init struct scx200_acb_iface *scx200_create_iface(const char *text,
Jean Delvare12a917f2007-02-13 22:09:03 +0100431 struct device *dev, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432{
433 struct scx200_acb_iface *iface;
434 struct i2c_adapter *adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
Deepak Saxena5263ebb2005-10-17 23:09:43 +0200436 iface = kzalloc(sizeof(*iface), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 if (!iface) {
438 printk(KERN_ERR NAME ": can't allocate memory\n");
Jordan Crouse80cd3a82006-06-12 21:44:28 +0200439 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 }
441
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 adapter = &iface->adapter;
443 i2c_set_adapdata(adapter, iface);
David Brownell2096b952007-05-01 23:26:28 +0200444 snprintf(adapter->name, sizeof(adapter->name), "%s ACB%d", text, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 adapter->owner = THIS_MODULE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 adapter->algo = &scx200_acb_algorithm;
Jean Delvare3401b2f2008-07-14 22:38:29 +0200447 adapter->class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
Jean Delvare12a917f2007-02-13 22:09:03 +0100448 adapter->dev.parent = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
Jean Delvare3fb9a652006-01-18 23:17:01 +0100450 mutex_init(&iface->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
Jordan Crouse80cd3a82006-06-12 21:44:28 +0200452 return iface;
453}
454
455static int __init scx200_acb_create(struct scx200_acb_iface *iface)
456{
457 struct i2c_adapter *adapter;
458 int rc;
459
460 adapter = &iface->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
462 rc = scx200_acb_probe(iface);
463 if (rc) {
Ben Gardneref4d9272006-01-18 22:43:10 +0100464 printk(KERN_WARNING NAME ": probe failed\n");
Jordan Crouse80cd3a82006-06-12 21:44:28 +0200465 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 }
467
468 scx200_acb_reset(iface);
469
470 if (i2c_add_adapter(adapter) < 0) {
Ben Gardneref4d9272006-01-18 22:43:10 +0100471 printk(KERN_ERR NAME ": failed to register\n");
Jordan Crouse80cd3a82006-06-12 21:44:28 +0200472 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 }
474
Matthias Kaehlcke9d9c01c2007-07-12 14:12:28 +0200475 mutex_lock(&scx200_acb_list_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 iface->next = scx200_acb_list;
477 scx200_acb_list = iface;
Matthias Kaehlcke9d9c01c2007-07-12 14:12:28 +0200478 mutex_unlock(&scx200_acb_list_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
480 return 0;
Jordan Crouse80cd3a82006-06-12 21:44:28 +0200481}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
Jordan Crouse80cd3a82006-06-12 21:44:28 +0200483static __init int scx200_create_pci(const char *text, struct pci_dev *pdev,
484 int bar)
485{
486 struct scx200_acb_iface *iface;
487 int rc;
488
Jean Delvare12a917f2007-02-13 22:09:03 +0100489 iface = scx200_create_iface(text, &pdev->dev, 0);
Jordan Crouse80cd3a82006-06-12 21:44:28 +0200490
491 if (iface == NULL)
492 return -ENOMEM;
493
494 iface->pdev = pdev;
495 iface->bar = bar;
496
Benjamin Herrenschmidt09483912007-12-20 15:28:09 +1100497 rc = pci_enable_device_io(iface->pdev);
Jeff Garzikffb3d132006-11-18 22:19:39 -0800498 if (rc)
499 goto errout_free;
Jordan Crouse80cd3a82006-06-12 21:44:28 +0200500
501 rc = pci_request_region(iface->pdev, iface->bar, iface->adapter.name);
Jeff Garzikffb3d132006-11-18 22:19:39 -0800502 if (rc) {
Jordan Crouse80cd3a82006-06-12 21:44:28 +0200503 printk(KERN_ERR NAME ": can't allocate PCI BAR %d\n",
504 iface->bar);
505 goto errout_free;
506 }
507
508 iface->base = pci_resource_start(iface->pdev, iface->bar);
509 rc = scx200_acb_create(iface);
510
511 if (rc == 0)
512 return 0;
513
514 pci_release_region(iface->pdev, iface->bar);
515 pci_dev_put(iface->pdev);
Ben Gardner9b7b6d32006-01-18 22:44:04 +0100516 errout_free:
517 kfree(iface);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 return rc;
519}
520
Jordan Crouse80cd3a82006-06-12 21:44:28 +0200521static int __init scx200_create_isa(const char *text, unsigned long base,
522 int index)
Ben Gardner16ffc5c2006-01-18 22:48:26 +0100523{
Jordan Crouse80cd3a82006-06-12 21:44:28 +0200524 struct scx200_acb_iface *iface;
525 int rc;
Ben Gardner16ffc5c2006-01-18 22:48:26 +0100526
Jean Delvare12a917f2007-02-13 22:09:03 +0100527 iface = scx200_create_iface(text, NULL, index);
Ben Gardner16ffc5c2006-01-18 22:48:26 +0100528
Jordan Crouse80cd3a82006-06-12 21:44:28 +0200529 if (iface == NULL)
530 return -ENOMEM;
531
Adrian Bunkdec1a992008-04-22 22:16:48 +0200532 if (!request_region(base, 8, iface->adapter.name)) {
Jordan Crouse80cd3a82006-06-12 21:44:28 +0200533 printk(KERN_ERR NAME ": can't allocate io 0x%lx-0x%lx\n",
534 base, base + 8 - 1);
535 rc = -EBUSY;
536 goto errout_free;
Ben Gardner16ffc5c2006-01-18 22:48:26 +0100537 }
538
Jordan Crouse80cd3a82006-06-12 21:44:28 +0200539 iface->base = base;
540 rc = scx200_acb_create(iface);
Ben Gardner16ffc5c2006-01-18 22:48:26 +0100541
Jordan Crouse80cd3a82006-06-12 21:44:28 +0200542 if (rc == 0)
543 return 0;
544
545 release_region(base, 8);
546 errout_free:
547 kfree(iface);
548 return rc;
549}
550
551/* Driver data is an index into the scx200_data array that indicates
552 * the name and the BAR where the I/O address resource is located. ISA
553 * devices are flagged with a bar value of -1 */
554
Jean Delvaree624dbd2010-05-21 18:40:56 +0200555static const struct pci_device_id scx200_pci[] __initconst = {
Jordan Crouse80cd3a82006-06-12 21:44:28 +0200556 { PCI_DEVICE(PCI_VENDOR_ID_NS, PCI_DEVICE_ID_NS_SCx200_BRIDGE),
557 .driver_data = 0 },
558 { PCI_DEVICE(PCI_VENDOR_ID_NS, PCI_DEVICE_ID_NS_SC1100_BRIDGE),
559 .driver_data = 0 },
560 { PCI_DEVICE(PCI_VENDOR_ID_NS, PCI_DEVICE_ID_NS_CS5535_ISA),
561 .driver_data = 1 },
562 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_CS5536_ISA),
563 .driver_data = 2 }
564};
565
566static struct {
567 const char *name;
568 int bar;
569} scx200_data[] = {
570 { "SCx200", -1 },
571 { "CS5535", 0 },
572 { "CS5536", 0 }
573};
574
575static __init int scx200_scan_pci(void)
576{
577 int data, dev;
578 int rc = -ENODEV;
579 struct pci_dev *pdev;
580
581 for(dev = 0; dev < ARRAY_SIZE(scx200_pci); dev++) {
582 pdev = pci_get_device(scx200_pci[dev].vendor,
583 scx200_pci[dev].device, NULL);
584
585 if (pdev == NULL)
586 continue;
587
588 data = scx200_pci[dev].driver_data;
589
590 /* if .bar is greater or equal to zero, this is a
591 * PCI device - otherwise, we assume
592 that the ports are ISA based
593 */
594
595 if (scx200_data[data].bar >= 0)
596 rc = scx200_create_pci(scx200_data[data].name, pdev,
597 scx200_data[data].bar);
598 else {
599 int i;
600
Jean Delvare4b4686e2007-05-01 23:26:30 +0200601 pci_dev_put(pdev);
Jordan Crouse80cd3a82006-06-12 21:44:28 +0200602 for (i = 0; i < MAX_DEVICES; ++i) {
603 if (base[i] == 0)
604 continue;
605
606 rc = scx200_create_isa(scx200_data[data].name,
607 base[i],
608 i);
609 }
610 }
611
612 break;
613 }
614
615 return rc;
Ben Gardner16ffc5c2006-01-18 22:48:26 +0100616}
617
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618static int __init scx200_acb_init(void)
619{
Jordan Crouse80cd3a82006-06-12 21:44:28 +0200620 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
622 pr_debug(NAME ": NatSemi SCx200 ACCESS.bus Driver\n");
623
Jordan Crouse80cd3a82006-06-12 21:44:28 +0200624 rc = scx200_scan_pci();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
Jean Delvare6f9c2962006-04-26 22:50:32 +0200626 /* If at least one bus was created, init must succeed */
627 if (scx200_acb_list)
628 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 return rc;
630}
631
632static void __exit scx200_acb_cleanup(void)
633{
634 struct scx200_acb_iface *iface;
Ben Gardner99c3adb2006-01-18 22:41:50 +0100635
Matthias Kaehlcke9d9c01c2007-07-12 14:12:28 +0200636 mutex_lock(&scx200_acb_list_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 while ((iface = scx200_acb_list) != NULL) {
638 scx200_acb_list = iface->next;
Matthias Kaehlcke9d9c01c2007-07-12 14:12:28 +0200639 mutex_unlock(&scx200_acb_list_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
641 i2c_del_adapter(&iface->adapter);
Jordan Crouse80cd3a82006-06-12 21:44:28 +0200642
643 if (iface->pdev) {
644 pci_release_region(iface->pdev, iface->bar);
645 pci_dev_put(iface->pdev);
646 }
647 else
648 release_region(iface->base, 8);
649
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 kfree(iface);
Matthias Kaehlcke9d9c01c2007-07-12 14:12:28 +0200651 mutex_lock(&scx200_acb_list_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 }
Matthias Kaehlcke9d9c01c2007-07-12 14:12:28 +0200653 mutex_unlock(&scx200_acb_list_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654}
655
656module_init(scx200_acb_init);
657module_exit(scx200_acb_cleanup);