blob: 140426db28df19bac1a3eb4b1782798049dcb065 [file] [log] [blame]
Thomas Gleixnerc942fdd2019-05-27 08:55:06 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/* ------------------------------------------------------------------------- */
3/* i2c-elektor.c i2c-hw access for PCF8584 style isa bus adaptes */
4/* ------------------------------------------------------------------------- */
5/* Copyright (C) 1995-97 Simon G. Vogl
6 1998-99 Hans Berglund
7
Thomas Gleixnerc942fdd2019-05-27 08:55:06 +02008 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009/* ------------------------------------------------------------------------- */
10
Jan Engelhardt96de0e22007-10-19 23:21:04 +020011/* With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi> and even
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 Frodo Looijaard <frodol@dds.nl> */
13
Lucas De Marchi25985ed2011-03-30 22:57:33 -030014/* Partially rewriten by Oleg I. Vdovikin for mmapped support of
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 for Alpha Processor Inc. UP-2000(+) boards */
16
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/kernel.h>
18#include <linux/ioport.h>
19#include <linux/module.h>
20#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/init.h>
22#include <linux/interrupt.h>
23#include <linux/pci.h>
24#include <linux/wait.h>
25
Jean Delvare4a5d3032007-05-01 23:26:30 +020026#include <linux/isa.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/i2c.h>
28#include <linux/i2c-algo-pcf.h>
H Hartley Sweeten21782182010-05-21 18:41:01 +020029#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <asm/irq.h>
32
33#include "../algos/i2c-algo-pcf.h"
34
35#define DEFAULT_BASE 0x330
36
37static int base;
Stig Telfer3634ff62005-10-08 00:21:48 +020038static u8 __iomem *base_iomem;
39
Linus Torvalds1da177e2005-04-16 15:20:36 -070040static int irq;
41static int clock = 0x1c;
42static int own = 0x55;
43static int mmapped;
44
Stig Telferfe3d6a92005-10-08 00:23:27 +020045/* vdovikin: removed static struct i2c_pcf_isa gpi; code -
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 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 */
49
50static wait_queue_head_t pcf_wait;
51static int pcf_pending;
52static spinlock_t lock;
53
Stig Telferfe3d6a92005-10-08 00:23:27 +020054static struct i2c_adapter pcf_isa_ops;
55
Linus Torvalds1da177e2005-04-16 15:20:36 -070056/* ----- local functions ---------------------------------------------- */
57
58static void pcf_isa_setbyte(void *data, int ctl, int val)
59{
Stig Telfer3634ff62005-10-08 00:21:48 +020060 u8 __iomem *address = ctl ? (base_iomem + 1) : base_iomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62 /* enable irq if any specified for serial operation */
63 if (ctl && irq && (val & I2C_PCF_ESO)) {
64 val |= I2C_PCF_ENI;
65 }
66
Stig Telferfe3d6a92005-10-08 00:23:27 +020067 pr_debug("%s: Write %p 0x%02X\n", pcf_isa_ops.name, address, val);
Stig Telfer3634ff62005-10-08 00:21:48 +020068 iowrite8(val, address);
69#ifdef __alpha__
70 /* API UP2000 needs some hardware fudging to make the write stick */
71 iowrite8(val, address);
72#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070073}
74
75static int pcf_isa_getbyte(void *data, int ctl)
76{
Stig Telfer3634ff62005-10-08 00:21:48 +020077 u8 __iomem *address = ctl ? (base_iomem + 1) : base_iomem;
78 int val = ioread8(address);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Stig Telferfe3d6a92005-10-08 00:23:27 +020080 pr_debug("%s: Read %p 0x%02X\n", pcf_isa_ops.name, address, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 return (val);
82}
83
84static int pcf_isa_getown(void *data)
85{
86 return (own);
87}
88
89
90static int pcf_isa_getclock(void *data)
91{
92 return (clock);
93}
94
David Miller08e53382008-10-22 20:21:29 +020095static void pcf_isa_waitforpin(void *data)
96{
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 DEFINE_WAIT(wait);
98 int timeout = 2;
99 unsigned long flags;
100
101 if (irq > 0) {
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) {
109 pcf_pending = 0;
110 }
111 spin_unlock_irqrestore(&lock, flags);
112 }
113 finish_wait(&pcf_wait, &wait);
114 } else {
115 pcf_pending = 0;
116 spin_unlock_irqrestore(&lock, flags);
117 }
118 } else {
119 udelay(100);
120 }
121}
122
123
David Howells7d12e782006-10-05 14:55:46 +0100124static irqreturn_t pcf_isa_handler(int this_irq, void *dev_id) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 spin_lock(&lock);
126 pcf_pending = 1;
127 spin_unlock(&lock);
128 wake_up_interruptible(&pcf_wait);
129 return IRQ_HANDLED;
130}
131
132
133static int pcf_isa_init(void)
134{
135 spin_lock_init(&lock);
136 if (!mmapped) {
Stig Telferfe3d6a92005-10-08 00:23:27 +0200137 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 return -ENODEV;
141 }
Stig Telfer3634ff62005-10-08 00:21:48 +0200142 base_iomem = ioport_map(base, 2);
143 if (!base_iomem) {
Stig Telferfe3d6a92005-10-08 00:23:27 +0200144 printk(KERN_ERR "%s: remap of I/O region %#x failed\n",
145 pcf_isa_ops.name, base);
Stig Telfer3634ff62005-10-08 00:21:48 +0200146 release_region(base, 2);
147 return -ENODEV;
148 }
149 } else {
Stig Telferfe3d6a92005-10-08 00:23:27 +0200150 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);
Stig Telfer3634ff62005-10-08 00:21:48 +0200153 return -ENODEV;
154 }
155 base_iomem = ioremap(base, 2);
156 if (base_iomem == NULL) {
Stig Telferfe3d6a92005-10-08 00:23:27 +0200157 printk(KERN_ERR "%s: remap of memory region %#x "
158 "failed\n", pcf_isa_ops.name, base);
Stig Telfer3634ff62005-10-08 00:21:48 +0200159 release_mem_region(base, 2);
160 return -ENODEV;
161 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 }
Stig Telferfe3d6a92005-10-08 00:23:27 +0200163 pr_debug("%s: registers %#x remapped to %p\n", pcf_isa_ops.name, base,
Stig Telfer3634ff62005-10-08 00:21:48 +0200164 base_iomem);
165
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 if (irq > 0) {
Stig Telferfe3d6a92005-10-08 00:23:27 +0200167 if (request_irq(irq, pcf_isa_handler, 0, pcf_isa_ops.name,
168 NULL) < 0) {
169 printk(KERN_ERR "%s: Request irq%d failed\n",
170 pcf_isa_ops.name, irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 irq = 0;
172 } else
173 enable_irq(irq);
174 }
175 return 0;
176}
177
178/* ------------------------------------------------------------------------
179 * Encapsulate the above functions in the correct operations structure.
180 * This is only done when more than one hardware adapter is supported.
181 */
182static 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,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188};
189
190static struct i2c_adapter pcf_isa_ops = {
191 .owner = THIS_MODULE,
Jean Delvare3401b2f2008-07-14 22:38:29 +0200192 .class = I2C_CLASS_HWMON | I2C_CLASS_SPD,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 .algo_data = &pcf_isa_data,
Stig Telferfe3d6a92005-10-08 00:23:27 +0200194 .name = "i2c-elektor",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195};
196
Bill Pemberton0b255e92012-11-27 15:59:38 -0500197static int elektor_match(struct device *dev, unsigned int id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198{
199#ifdef __alpha__
Stig Telferfe3d6a92005-10-08 00:23:27 +0200200 /* check to see we have memory mapped PCF8584 connected to the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 Cypress cy82c693 PCI-ISA bridge as on UP2000 board */
202 if (base == 0) {
203 struct pci_dev *cy693_dev;
Stig Telferfe3d6a92005-10-08 00:23:27 +0200204
205 cy693_dev = pci_get_device(PCI_VENDOR_ID_CONTAQ,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 PCI_DEVICE_ID_CONTAQ_82C693, NULL);
207 if (cy693_dev) {
Stig Telfer3634ff62005-10-08 00:21:48 +0200208 unsigned char config;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 /* yeap, we've found cypress, let's check config */
210 if (!pci_read_config_byte(cy693_dev, 0x47, &config)) {
Stig Telferfe3d6a92005-10-08 00:23:27 +0200211
Jean Delvare4a5d3032007-05-01 23:26:30 +0200212 dev_dbg(dev, "found cy82c693, config "
213 "register 0x47 = 0x%02x\n", config);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
215 /* UP2000 board has this register set to 0xe1,
Stig Telferfe3d6a92005-10-08 00:23:27 +0200216 but the most significant bit as seems can be
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 reset during the proper initialisation
Stig Telferfe3d6a92005-10-08 00:23:27 +0200218 sequence if guys from API decides to do that
219 (so, we can even enable Tsunami Pchip
220 window for the upper 1 Gb) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
222 /* so just check for ROMCS at 0xe0000,
Stig Telferfe3d6a92005-10-08 00:23:27 +0200223 ROMCS enabled for writes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 and external XD Bus buffer in use. */
225 if ((config & 0x7f) == 0x61) {
226 /* seems to be UP2000 like board */
227 base = 0xe0000;
Stig Telfer3634ff62005-10-08 00:21:48 +0200228 mmapped = 1;
Stig Telferfe3d6a92005-10-08 00:23:27 +0200229 /* UP2000 drives ISA with
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 8.25 MHz (PCI/4) clock
231 (this can be read from cypress) */
232 clock = I2C_PCF_CLK | I2C_PCF_TRNS90;
Jean Delvare4a5d3032007-05-01 23:26:30 +0200233 dev_info(dev, "found API UP2000 like "
234 "board, will probe PCF8584 "
235 "later\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 }
237 }
238 pci_dev_put(cy693_dev);
239 }
240 }
241#endif
242
243 /* sanity checks for mmapped I/O */
244 if (mmapped && base < 0xc8000) {
Jean Delvare4a5d3032007-05-01 23:26:30 +0200245 dev_err(dev, "incorrect base address (%#x) specified "
246 "for mmapped I/O\n", base);
247 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 }
249
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 if (base == 0) {
251 base = DEFAULT_BASE;
252 }
Jean Delvare4a5d3032007-05-01 23:26:30 +0200253 return 1;
254}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
Bill Pemberton0b255e92012-11-27 15:59:38 -0500256static int elektor_probe(struct device *dev, unsigned int id)
Jean Delvare4a5d3032007-05-01 23:26:30 +0200257{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 init_waitqueue_head(&pcf_wait);
259 if (pcf_isa_init())
260 return -ENODEV;
Jean Delvare4a5d3032007-05-01 23:26:30 +0200261 pcf_isa_ops.dev.parent = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 if (i2c_pcf_add_bus(&pcf_isa_ops) < 0)
263 goto fail;
Stig Telferfe3d6a92005-10-08 00:23:27 +0200264
Jean Delvare4a5d3032007-05-01 23:26:30 +0200265 dev_info(dev, "found device at %#x\n", base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
267 return 0;
268
269 fail:
270 if (irq > 0) {
271 disable_irq(irq);
272 free_irq(irq, NULL);
273 }
274
Stig Telfer3634ff62005-10-08 00:21:48 +0200275 if (!mmapped) {
276 ioport_unmap(base_iomem);
Stig Telferfe3d6a92005-10-08 00:23:27 +0200277 release_region(base, 2);
Stig Telfer3634ff62005-10-08 00:21:48 +0200278 } else {
279 iounmap(base_iomem);
280 release_mem_region(base, 2);
281 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 return -ENODEV;
283}
284
Bill Pemberton0b255e92012-11-27 15:59:38 -0500285static int elektor_remove(struct device *dev, unsigned int id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286{
Jean Delvare32697112006-12-10 21:21:33 +0100287 i2c_del_adapter(&pcf_isa_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
289 if (irq > 0) {
290 disable_irq(irq);
291 free_irq(irq, NULL);
292 }
293
Stig Telfer3634ff62005-10-08 00:21:48 +0200294 if (!mmapped) {
295 ioport_unmap(base_iomem);
Stig Telferfe3d6a92005-10-08 00:23:27 +0200296 release_region(base, 2);
Stig Telfer3634ff62005-10-08 00:21:48 +0200297 } else {
298 iounmap(base_iomem);
299 release_mem_region(base, 2);
300 }
Jean Delvare4a5d3032007-05-01 23:26:30 +0200301
302 return 0;
303}
304
305static struct isa_driver i2c_elektor_driver = {
306 .match = elektor_match,
307 .probe = elektor_probe,
Bill Pemberton0b255e92012-11-27 15:59:38 -0500308 .remove = elektor_remove,
Jean Delvare4a5d3032007-05-01 23:26:30 +0200309 .driver = {
310 .owner = THIS_MODULE,
311 .name = "i2c-elektor",
312 },
313};
314
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315MODULE_AUTHOR("Hans Berglund <hb@spacetec.no>");
316MODULE_DESCRIPTION("I2C-Bus adapter routines for PCF8584 ISA bus adapter");
317MODULE_LICENSE("GPL");
318
David Howellsc78babc2017-04-04 16:54:23 +0100319module_param_hw(base, int, ioport_or_iomem, 0);
320module_param_hw(irq, int, irq, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321module_param(clock, int, 0);
322module_param(own, int, 0);
David Howellsc78babc2017-04-04 16:54:23 +0100323module_param_hw(mmapped, int, other, 0);
William Breathitt Gray9e55c072016-05-31 11:37:13 -0400324module_isa_driver(i2c_elektor_driver, 1);