1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* ------------------------------------------------------------------------- */
3 /* i2c-elektor.c i2c-hw access for PCF8584 style isa bus adaptes */
4 /* ------------------------------------------------------------------------- */
5 /* Copyright (C) 1995-97 Simon G. Vogl
9 /* ------------------------------------------------------------------------- */
11 /* With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi> and even
12 Frodo Looijaard <frodol@dds.nl> */
14 /* Partially rewriten by Oleg I. Vdovikin for mmapped support of
15 for Alpha Processor Inc. UP-2000(+) boards */
17 #include <linux/kernel.h>
18 #include <linux/ioport.h>
19 #include <linux/module.h>
20 #include <linux/delay.h>
21 #include <linux/init.h>
22 #include <linux/interrupt.h>
23 #include <linux/pci.h>
24 #include <linux/wait.h>
26 #include <linux/isa.h>
27 #include <linux/i2c.h>
28 #include <linux/i2c-algo-pcf.h>
33 #include "../algos/i2c-algo-pcf.h"
35 #define DEFAULT_BASE 0x330
38 static u8 __iomem
*base_iomem
;
41 static int clock
= 0x1c;
42 static int own
= 0x55;
45 /* vdovikin: removed static struct i2c_pcf_isa gpi; code -
46 this module in real supports only one device, due to missing arguments
47 in some functions, called from the algo-pcf module. Sometimes it's
48 need to be rewriten - but for now just remove this for simpler reading */
50 static wait_queue_head_t pcf_wait
;
51 static int pcf_pending
;
52 static spinlock_t lock
;
54 static struct i2c_adapter pcf_isa_ops
;
56 /* ----- local functions ---------------------------------------------- */
58 static void pcf_isa_setbyte(void *data
, int ctl
, int val
)
60 u8 __iomem
*address
= ctl
? (base_iomem
+ 1) : base_iomem
;
62 /* enable irq if any specified for serial operation */
63 if (ctl
&& irq
&& (val
& I2C_PCF_ESO
)) {
67 pr_debug("%s: Write %p 0x%02X\n", pcf_isa_ops
.name
, address
, val
);
68 iowrite8(val
, address
);
70 /* API UP2000 needs some hardware fudging to make the write stick */
71 iowrite8(val
, address
);
75 static int pcf_isa_getbyte(void *data
, int ctl
)
77 u8 __iomem
*address
= ctl
? (base_iomem
+ 1) : base_iomem
;
78 int val
= ioread8(address
);
80 pr_debug("%s: Read %p 0x%02X\n", pcf_isa_ops
.name
, address
, val
);
84 static int pcf_isa_getown(void *data
)
90 static int pcf_isa_getclock(void *data
)
95 static void pcf_isa_waitforpin(void *data
)
102 spin_lock_irqsave(&lock
, flags
);
103 if (pcf_pending
== 0) {
104 spin_unlock_irqrestore(&lock
, flags
);
105 prepare_to_wait(&pcf_wait
, &wait
, TASK_INTERRUPTIBLE
);
106 if (schedule_timeout(timeout
*HZ
)) {
107 spin_lock_irqsave(&lock
, flags
);
108 if (pcf_pending
== 1) {
111 spin_unlock_irqrestore(&lock
, flags
);
113 finish_wait(&pcf_wait
, &wait
);
116 spin_unlock_irqrestore(&lock
, flags
);
124 static irqreturn_t
pcf_isa_handler(int this_irq
, void *dev_id
) {
128 wake_up_interruptible(&pcf_wait
);
133 static int pcf_isa_init(void)
135 spin_lock_init(&lock
);
137 if (!request_region(base
, 2, pcf_isa_ops
.name
)) {
138 printk(KERN_ERR
"%s: requested I/O region (%#x:2) is "
139 "in use\n", pcf_isa_ops
.name
, base
);
142 base_iomem
= ioport_map(base
, 2);
144 printk(KERN_ERR
"%s: remap of I/O region %#x failed\n",
145 pcf_isa_ops
.name
, base
);
146 release_region(base
, 2);
150 if (!request_mem_region(base
, 2, pcf_isa_ops
.name
)) {
151 printk(KERN_ERR
"%s: requested memory region (%#x:2) "
152 "is in use\n", pcf_isa_ops
.name
, base
);
155 base_iomem
= ioremap(base
, 2);
156 if (base_iomem
== NULL
) {
157 printk(KERN_ERR
"%s: remap of memory region %#x "
158 "failed\n", pcf_isa_ops
.name
, base
);
159 release_mem_region(base
, 2);
163 pr_debug("%s: registers %#x remapped to %p\n", pcf_isa_ops
.name
, base
,
167 if (request_irq(irq
, pcf_isa_handler
, 0, pcf_isa_ops
.name
,
169 printk(KERN_ERR
"%s: Request irq%d failed\n",
170 pcf_isa_ops
.name
, irq
);
178 /* ------------------------------------------------------------------------
179 * Encapsulate the above functions in the correct operations structure.
180 * This is only done when more than one hardware adapter is supported.
182 static struct i2c_algo_pcf_data pcf_isa_data
= {
183 .setpcf
= pcf_isa_setbyte
,
184 .getpcf
= pcf_isa_getbyte
,
185 .getown
= pcf_isa_getown
,
186 .getclock
= pcf_isa_getclock
,
187 .waitforpin
= pcf_isa_waitforpin
,
190 static struct i2c_adapter pcf_isa_ops
= {
191 .owner
= THIS_MODULE
,
192 .class = I2C_CLASS_HWMON
| I2C_CLASS_SPD
,
193 .algo_data
= &pcf_isa_data
,
194 .name
= "i2c-elektor",
197 static int elektor_match(struct device
*dev
, unsigned int id
)
200 /* check to see we have memory mapped PCF8584 connected to the
201 Cypress cy82c693 PCI-ISA bridge as on UP2000 board */
203 struct pci_dev
*cy693_dev
;
205 cy693_dev
= pci_get_device(PCI_VENDOR_ID_CONTAQ
,
206 PCI_DEVICE_ID_CONTAQ_82C693
, NULL
);
208 unsigned char config
;
209 /* yeap, we've found cypress, let's check config */
210 if (!pci_read_config_byte(cy693_dev
, 0x47, &config
)) {
212 dev_dbg(dev
, "found cy82c693, config "
213 "register 0x47 = 0x%02x\n", config
);
215 /* UP2000 board has this register set to 0xe1,
216 but the most significant bit as seems can be
217 reset during the proper initialisation
218 sequence if guys from API decides to do that
219 (so, we can even enable Tsunami Pchip
220 window for the upper 1 Gb) */
222 /* so just check for ROMCS at 0xe0000,
223 ROMCS enabled for writes
224 and external XD Bus buffer in use. */
225 if ((config
& 0x7f) == 0x61) {
226 /* seems to be UP2000 like board */
229 /* UP2000 drives ISA with
230 8.25 MHz (PCI/4) clock
231 (this can be read from cypress) */
232 clock
= I2C_PCF_CLK
| I2C_PCF_TRNS90
;
233 dev_info(dev
, "found API UP2000 like "
234 "board, will probe PCF8584 "
238 pci_dev_put(cy693_dev
);
243 /* sanity checks for mmapped I/O */
244 if (mmapped
&& base
< 0xc8000) {
245 dev_err(dev
, "incorrect base address (%#x) specified "
246 "for mmapped I/O\n", base
);
256 static int elektor_probe(struct device
*dev
, unsigned int id
)
258 init_waitqueue_head(&pcf_wait
);
261 pcf_isa_ops
.dev
.parent
= dev
;
262 if (i2c_pcf_add_bus(&pcf_isa_ops
) < 0)
265 dev_info(dev
, "found device at %#x\n", base
);
276 ioport_unmap(base_iomem
);
277 release_region(base
, 2);
280 release_mem_region(base
, 2);
285 static int elektor_remove(struct device
*dev
, unsigned int id
)
287 i2c_del_adapter(&pcf_isa_ops
);
295 ioport_unmap(base_iomem
);
296 release_region(base
, 2);
299 release_mem_region(base
, 2);
305 static struct isa_driver i2c_elektor_driver
= {
306 .match
= elektor_match
,
307 .probe
= elektor_probe
,
308 .remove
= elektor_remove
,
310 .owner
= THIS_MODULE
,
311 .name
= "i2c-elektor",
315 MODULE_AUTHOR("Hans Berglund <hb@spacetec.no>");
316 MODULE_DESCRIPTION("I2C-Bus adapter routines for PCF8584 ISA bus adapter");
317 MODULE_LICENSE("GPL");
319 module_param_hw(base
, int, ioport_or_iomem
, 0);
320 module_param_hw(irq
, int, irq
, 0);
321 module_param(clock
, int, 0);
322 module_param(own
, int, 0);
323 module_param_hw(mmapped
, int, other
, 0);
324 module_isa_driver(i2c_elektor_driver
, 1);