blob: a591fe685f065dd972aa00bd46ed48d4c621d0b2 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* ------------------------------------------------------------------------- */
2/* i2c-elektor.c i2c-hw access for PCF8584 style isa bus adaptes */
3/* ------------------------------------------------------------------------- */
4/* Copyright (C) 1995-97 Simon G. Vogl
5 1998-99 Hans Berglund
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
20/* ------------------------------------------------------------------------- */
21
22/* With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi> and even
23 Frodo Looijaard <frodol@dds.nl> */
24
Stig Telferfe3d6a92005-10-08 00:23:27 +020025/* Partialy rewriten by Oleg I. Vdovikin for mmapped support of
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 for Alpha Processor Inc. UP-2000(+) boards */
27
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/kernel.h>
29#include <linux/ioport.h>
30#include <linux/module.h>
31#include <linux/delay.h>
32#include <linux/slab.h>
33#include <linux/init.h>
34#include <linux/interrupt.h>
35#include <linux/pci.h>
36#include <linux/wait.h>
37
38#include <linux/i2c.h>
39#include <linux/i2c-algo-pcf.h>
40
41#include <asm/io.h>
42#include <asm/irq.h>
43
44#include "../algos/i2c-algo-pcf.h"
45
46#define DEFAULT_BASE 0x330
47
48static int base;
Stig Telfer3634ff62005-10-08 00:21:48 +020049static u8 __iomem *base_iomem;
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051static int irq;
52static int clock = 0x1c;
53static int own = 0x55;
54static int mmapped;
55
Stig Telferfe3d6a92005-10-08 00:23:27 +020056/* vdovikin: removed static struct i2c_pcf_isa gpi; code -
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 this module in real supports only one device, due to missing arguments
58 in some functions, called from the algo-pcf module. Sometimes it's
59 need to be rewriten - but for now just remove this for simpler reading */
60
61static wait_queue_head_t pcf_wait;
62static int pcf_pending;
63static spinlock_t lock;
64
Stig Telferfe3d6a92005-10-08 00:23:27 +020065static struct i2c_adapter pcf_isa_ops;
66
Linus Torvalds1da177e2005-04-16 15:20:36 -070067/* ----- local functions ---------------------------------------------- */
68
69static void pcf_isa_setbyte(void *data, int ctl, int val)
70{
Stig Telfer3634ff62005-10-08 00:21:48 +020071 u8 __iomem *address = ctl ? (base_iomem + 1) : base_iomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
73 /* enable irq if any specified for serial operation */
74 if (ctl && irq && (val & I2C_PCF_ESO)) {
75 val |= I2C_PCF_ENI;
76 }
77
Stig Telferfe3d6a92005-10-08 00:23:27 +020078 pr_debug("%s: Write %p 0x%02X\n", pcf_isa_ops.name, address, val);
Stig Telfer3634ff62005-10-08 00:21:48 +020079 iowrite8(val, address);
80#ifdef __alpha__
81 /* API UP2000 needs some hardware fudging to make the write stick */
82 iowrite8(val, address);
83#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070084}
85
86static int pcf_isa_getbyte(void *data, int ctl)
87{
Stig Telfer3634ff62005-10-08 00:21:48 +020088 u8 __iomem *address = ctl ? (base_iomem + 1) : base_iomem;
89 int val = ioread8(address);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Stig Telferfe3d6a92005-10-08 00:23:27 +020091 pr_debug("%s: Read %p 0x%02X\n", pcf_isa_ops.name, address, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 return (val);
93}
94
95static int pcf_isa_getown(void *data)
96{
97 return (own);
98}
99
100
101static int pcf_isa_getclock(void *data)
102{
103 return (clock);
104}
105
106static void pcf_isa_waitforpin(void) {
107 DEFINE_WAIT(wait);
108 int timeout = 2;
109 unsigned long flags;
110
111 if (irq > 0) {
112 spin_lock_irqsave(&lock, flags);
113 if (pcf_pending == 0) {
114 spin_unlock_irqrestore(&lock, flags);
115 prepare_to_wait(&pcf_wait, &wait, TASK_INTERRUPTIBLE);
116 if (schedule_timeout(timeout*HZ)) {
117 spin_lock_irqsave(&lock, flags);
118 if (pcf_pending == 1) {
119 pcf_pending = 0;
120 }
121 spin_unlock_irqrestore(&lock, flags);
122 }
123 finish_wait(&pcf_wait, &wait);
124 } else {
125 pcf_pending = 0;
126 spin_unlock_irqrestore(&lock, flags);
127 }
128 } else {
129 udelay(100);
130 }
131}
132
133
David Howells7d12e782006-10-05 14:55:46 +0100134static irqreturn_t pcf_isa_handler(int this_irq, void *dev_id) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 spin_lock(&lock);
136 pcf_pending = 1;
137 spin_unlock(&lock);
138 wake_up_interruptible(&pcf_wait);
139 return IRQ_HANDLED;
140}
141
142
143static int pcf_isa_init(void)
144{
145 spin_lock_init(&lock);
146 if (!mmapped) {
Stig Telferfe3d6a92005-10-08 00:23:27 +0200147 if (!request_region(base, 2, pcf_isa_ops.name)) {
148 printk(KERN_ERR "%s: requested I/O region (%#x:2) is "
149 "in use\n", pcf_isa_ops.name, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 return -ENODEV;
151 }
Stig Telfer3634ff62005-10-08 00:21:48 +0200152 base_iomem = ioport_map(base, 2);
153 if (!base_iomem) {
Stig Telferfe3d6a92005-10-08 00:23:27 +0200154 printk(KERN_ERR "%s: remap of I/O region %#x failed\n",
155 pcf_isa_ops.name, base);
Stig Telfer3634ff62005-10-08 00:21:48 +0200156 release_region(base, 2);
157 return -ENODEV;
158 }
159 } else {
Stig Telferfe3d6a92005-10-08 00:23:27 +0200160 if (!request_mem_region(base, 2, pcf_isa_ops.name)) {
161 printk(KERN_ERR "%s: requested memory region (%#x:2) "
162 "is in use\n", pcf_isa_ops.name, base);
Stig Telfer3634ff62005-10-08 00:21:48 +0200163 return -ENODEV;
164 }
165 base_iomem = ioremap(base, 2);
166 if (base_iomem == NULL) {
Stig Telferfe3d6a92005-10-08 00:23:27 +0200167 printk(KERN_ERR "%s: remap of memory region %#x "
168 "failed\n", pcf_isa_ops.name, base);
Stig Telfer3634ff62005-10-08 00:21:48 +0200169 release_mem_region(base, 2);
170 return -ENODEV;
171 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 }
Stig Telferfe3d6a92005-10-08 00:23:27 +0200173 pr_debug("%s: registers %#x remapped to %p\n", pcf_isa_ops.name, base,
Stig Telfer3634ff62005-10-08 00:21:48 +0200174 base_iomem);
175
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 if (irq > 0) {
Stig Telferfe3d6a92005-10-08 00:23:27 +0200177 if (request_irq(irq, pcf_isa_handler, 0, pcf_isa_ops.name,
178 NULL) < 0) {
179 printk(KERN_ERR "%s: Request irq%d failed\n",
180 pcf_isa_ops.name, irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 irq = 0;
182 } else
183 enable_irq(irq);
184 }
185 return 0;
186}
187
188/* ------------------------------------------------------------------------
189 * Encapsulate the above functions in the correct operations structure.
190 * This is only done when more than one hardware adapter is supported.
191 */
192static struct i2c_algo_pcf_data pcf_isa_data = {
193 .setpcf = pcf_isa_setbyte,
194 .getpcf = pcf_isa_getbyte,
195 .getown = pcf_isa_getown,
196 .getclock = pcf_isa_getclock,
197 .waitforpin = pcf_isa_waitforpin,
198 .udelay = 10,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 .timeout = 100,
200};
201
202static struct i2c_adapter pcf_isa_ops = {
203 .owner = THIS_MODULE,
204 .class = I2C_CLASS_HWMON,
205 .id = I2C_HW_P_ELEK,
206 .algo_data = &pcf_isa_data,
Stig Telferfe3d6a92005-10-08 00:23:27 +0200207 .name = "i2c-elektor",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208};
209
Stig Telferfe3d6a92005-10-08 00:23:27 +0200210static int __init i2c_pcfisa_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211{
212#ifdef __alpha__
Stig Telferfe3d6a92005-10-08 00:23:27 +0200213 /* check to see we have memory mapped PCF8584 connected to the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 Cypress cy82c693 PCI-ISA bridge as on UP2000 board */
215 if (base == 0) {
216 struct pci_dev *cy693_dev;
Stig Telferfe3d6a92005-10-08 00:23:27 +0200217
218 cy693_dev = pci_get_device(PCI_VENDOR_ID_CONTAQ,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 PCI_DEVICE_ID_CONTAQ_82C693, NULL);
220 if (cy693_dev) {
Stig Telfer3634ff62005-10-08 00:21:48 +0200221 unsigned char config;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 /* yeap, we've found cypress, let's check config */
223 if (!pci_read_config_byte(cy693_dev, 0x47, &config)) {
Stig Telferfe3d6a92005-10-08 00:23:27 +0200224
225 pr_debug("%s: found cy82c693, config "
226 "register 0x47 = 0x%02x\n",
227 pcf_isa_ops.name, config);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
229 /* UP2000 board has this register set to 0xe1,
Stig Telferfe3d6a92005-10-08 00:23:27 +0200230 but the most significant bit as seems can be
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 reset during the proper initialisation
Stig Telferfe3d6a92005-10-08 00:23:27 +0200232 sequence if guys from API decides to do that
233 (so, we can even enable Tsunami Pchip
234 window for the upper 1 Gb) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
236 /* so just check for ROMCS at 0xe0000,
Stig Telferfe3d6a92005-10-08 00:23:27 +0200237 ROMCS enabled for writes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 and external XD Bus buffer in use. */
239 if ((config & 0x7f) == 0x61) {
240 /* seems to be UP2000 like board */
241 base = 0xe0000;
Stig Telfer3634ff62005-10-08 00:21:48 +0200242 mmapped = 1;
Stig Telferfe3d6a92005-10-08 00:23:27 +0200243 /* UP2000 drives ISA with
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 8.25 MHz (PCI/4) clock
245 (this can be read from cypress) */
246 clock = I2C_PCF_CLK | I2C_PCF_TRNS90;
Stig Telferfe3d6a92005-10-08 00:23:27 +0200247 pr_info("%s: found API UP2000 like "
248 "board, will probe PCF8584 "
249 "later\n", pcf_isa_ops.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 }
251 }
252 pci_dev_put(cy693_dev);
253 }
254 }
255#endif
256
257 /* sanity checks for mmapped I/O */
258 if (mmapped && base < 0xc8000) {
Stig Telferfe3d6a92005-10-08 00:23:27 +0200259 printk(KERN_ERR "%s: incorrect base address (%#x) specified "
260 "for mmapped I/O\n", pcf_isa_ops.name, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 return -ENODEV;
262 }
263
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 if (base == 0) {
265 base = DEFAULT_BASE;
266 }
267
268 init_waitqueue_head(&pcf_wait);
269 if (pcf_isa_init())
270 return -ENODEV;
271 if (i2c_pcf_add_bus(&pcf_isa_ops) < 0)
272 goto fail;
Stig Telferfe3d6a92005-10-08 00:23:27 +0200273
274 dev_info(&pcf_isa_ops.dev, "found device at %#x\n", base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
276 return 0;
277
278 fail:
279 if (irq > 0) {
280 disable_irq(irq);
281 free_irq(irq, NULL);
282 }
283
Stig Telfer3634ff62005-10-08 00:21:48 +0200284 if (!mmapped) {
285 ioport_unmap(base_iomem);
Stig Telferfe3d6a92005-10-08 00:23:27 +0200286 release_region(base, 2);
Stig Telfer3634ff62005-10-08 00:21:48 +0200287 } else {
288 iounmap(base_iomem);
289 release_mem_region(base, 2);
290 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 return -ENODEV;
292}
293
294static void i2c_pcfisa_exit(void)
295{
296 i2c_pcf_del_bus(&pcf_isa_ops);
297
298 if (irq > 0) {
299 disable_irq(irq);
300 free_irq(irq, NULL);
301 }
302
Stig Telfer3634ff62005-10-08 00:21:48 +0200303 if (!mmapped) {
304 ioport_unmap(base_iomem);
Stig Telferfe3d6a92005-10-08 00:23:27 +0200305 release_region(base, 2);
Stig Telfer3634ff62005-10-08 00:21:48 +0200306 } else {
307 iounmap(base_iomem);
308 release_mem_region(base, 2);
309 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310}
311
312MODULE_AUTHOR("Hans Berglund <hb@spacetec.no>");
313MODULE_DESCRIPTION("I2C-Bus adapter routines for PCF8584 ISA bus adapter");
314MODULE_LICENSE("GPL");
315
316module_param(base, int, 0);
317module_param(irq, int, 0);
318module_param(clock, int, 0);
319module_param(own, int, 0);
320module_param(mmapped, int, 0);
321
322module_init(i2c_pcfisa_init);
323module_exit(i2c_pcfisa_exit);