Merge from vendor branch LIBEVENT:
[dragonfly.git] / sys / bus / iicbus / i386 / pcf.c
1 /*-
2  * Copyright (c) 1998 Nicolas Souchu, Marc Bouget
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD: src/sys/i386/isa/pcf.c,v 1.14 2000/01/14 00:18:05 nsouch Exp $
27  * $DragonFly: src/sys/bus/iicbus/i386/pcf.c,v 1.9 2006/12/22 23:12:16 swildner Exp $
28  *
29  */
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/kernel.h>
33 #include <sys/module.h>
34 #include <sys/bus.h>
35 #include <sys/malloc.h>
36
37 #include <machine/clock.h>
38 #include <sys/rman.h>
39
40 #include <bus/isa/isareg.h>
41 #include <bus/isa/isavar.h>
42
43 #include <bus/isa/i386/isa_device.h>
44
45 #include "../iiconf.h"
46 #include "iicbus_if.h"
47
48 #define IO_PCFSIZE      2
49
50 #define TIMEOUT 9999                                    /* XXX */
51
52 /* Status bits of S1 register (read only) */
53 #define nBB     0x01            /* busy when low set/reset by STOP/START*/
54 #define LAB     0x02            /* lost arbitration bit in multi-master mode */
55 #define AAS     0x04            /* addressed as slave */
56 #define LRB     0x08            /* last received byte when not AAS */
57 #define AD0     0x08            /* general call received when AAS */
58 #define BER     0x10            /* bus error, misplaced START or STOP */
59 #define STS     0x20            /* STOP detected in slave receiver mode */
60 #define PIN     0x80            /* pending interrupt not (r/w) */
61
62 /* Control bits of S1 register (write only) */
63 #define ACK     0x01
64 #define STO     0x02
65 #define STA     0x04
66 #define ENI     0x08
67 #define ES2     0x10
68 #define ES1     0x20
69 #define ES0     0x40
70
71 #define BUFSIZE 2048
72
73 #define SLAVE_TRANSMITTER       0x1
74 #define SLAVE_RECEIVER          0x2
75
76 #define PCF_DEFAULT_ADDR        0xaa
77
78 struct pcf_softc {
79
80         int pcf_base;                   /* isa port */
81         int pcf_flags;
82         u_char pcf_addr;                /* interface I2C address */
83
84         int pcf_slave_mode;             /* receiver or transmitter */
85         int pcf_started;                /* 1 if start condition sent */
86
87         device_t iicbus;                /* the corresponding iicbus */
88
89         int rid_irq, rid_ioport;
90         struct resource *res_irq, *res_ioport;
91         void *intr_cookie;
92 };
93
94 static int pcf_probe(device_t);
95 static int pcf_attach(device_t);
96 static void pcfintr(void *arg);
97
98 static int pcf_print_child(device_t, device_t);
99
100 static int pcf_repeated_start(device_t, u_char, int);
101 static int pcf_start(device_t, u_char, int);
102 static int pcf_stop(device_t);
103 static int pcf_write(device_t, char *, int, int *, int);
104 static int pcf_read(device_t, char *, int, int *, int, int);
105 static int pcf_rst_card(device_t, u_char, u_char, u_char *);
106
107 static device_method_t pcf_methods[] = {
108         /* device interface */
109         DEVMETHOD(device_probe,         pcf_probe),
110         DEVMETHOD(device_attach,        pcf_attach),
111
112         /* bus interface */
113         DEVMETHOD(bus_print_child,      pcf_print_child),
114
115         /* iicbus interface */
116         DEVMETHOD(iicbus_callback,      iicbus_null_callback),
117         DEVMETHOD(iicbus_repeated_start, pcf_repeated_start),
118         DEVMETHOD(iicbus_start,         pcf_start),
119         DEVMETHOD(iicbus_stop,          pcf_stop),
120         DEVMETHOD(iicbus_write,         pcf_write),
121         DEVMETHOD(iicbus_read,          pcf_read),
122         DEVMETHOD(iicbus_reset,         pcf_rst_card),
123
124         { 0, 0 }
125 };
126
127 static driver_t pcf_driver = {
128         "pcf",
129         pcf_methods,
130         sizeof(struct pcf_softc),
131 };
132
133 static devclass_t pcf_devclass;
134
135 #define DEVTOSOFTC(dev) ((struct pcf_softc *)device_get_softc(dev))
136
137 static int
138 pcf_probe(device_t pcfdev)
139 {
140         struct pcf_softc *pcf = DEVTOSOFTC(pcfdev);
141         device_t parent = device_get_parent(pcfdev);
142
143         device_set_desc(pcfdev, "PCF8584 I2C bus controller");
144
145         pcf = DEVTOSOFTC(pcfdev);
146         bzero(pcf, sizeof(struct pcf_softc));
147
148         pcf->rid_irq = pcf->rid_ioport = 0;
149         pcf->res_irq = pcf->res_ioport = 0;
150
151         /* IO port is mandatory */
152         pcf->res_ioport = bus_alloc_resource(pcfdev, SYS_RES_IOPORT,
153                                              &pcf->rid_ioport, 0ul, ~0ul, 
154                                              IO_PCFSIZE, RF_ACTIVE);
155         if (pcf->res_ioport == 0) {
156                 device_printf(pcfdev, "cannot reserve I/O port range\n");
157                 goto error;
158         }
159         BUS_READ_IVAR(parent, pcfdev, ISA_IVAR_PORT, &pcf->pcf_base);
160
161         pcf->pcf_flags = device_get_flags(pcfdev);
162
163         if (!(pcf->pcf_flags & IIC_POLLED)) {
164                 pcf->res_irq = bus_alloc_resource(pcfdev, SYS_RES_IRQ, &pcf->rid_irq,
165                                                   0ul, ~0ul, 1, RF_ACTIVE);
166                 if (pcf->res_irq == 0) {
167                         device_printf(pcfdev, "can't reserve irq, polled mode.\n");
168                         pcf->pcf_flags |= IIC_POLLED;
169                 }
170         }
171
172         /* reset the chip */
173         pcf_rst_card(pcfdev, IIC_FASTEST, PCF_DEFAULT_ADDR, NULL);
174
175         return (0);
176 error:
177         if (pcf->res_ioport != 0) {
178                 bus_deactivate_resource(pcfdev, SYS_RES_IOPORT, pcf->rid_ioport,
179                                         pcf->res_ioport);
180                 bus_release_resource(pcfdev, SYS_RES_IOPORT, pcf->rid_ioport,
181                                      pcf->res_ioport);
182         }
183         return (ENXIO);
184 }
185
186 static int
187 pcf_attach(device_t pcfdev)
188 {
189         struct pcf_softc *pcf = DEVTOSOFTC(pcfdev);
190         device_t parent = device_get_parent(pcfdev);
191         int error = 0;
192
193         if (pcf->res_irq) {
194                 /* default to the tty mask for registration */  /* XXX */
195                 error = BUS_SETUP_INTR(parent, pcfdev, pcf->res_irq, 
196                                         0, pcfintr, pcfdev,
197                                         &pcf->intr_cookie, NULL);
198                 if (error)
199                         return (error);
200         }
201
202         pcf->iicbus = iicbus_alloc_bus(pcfdev);
203
204         /* probe and attach the iicbus */
205         device_probe_and_attach(pcf->iicbus);
206
207         return (0);
208 }
209
210 static int
211 pcf_print_child(device_t bus, device_t dev)
212 {
213         struct pcf_softc *pcf = (struct pcf_softc *)device_get_softc(bus);
214         int retval = 0;
215
216         retval += bus_print_child_header(bus, dev);
217         retval += kprintf(" on %s addr 0x%x\n", device_get_nameunit(bus),
218                          (int)pcf->pcf_addr);
219
220         return (retval);
221 }
222
223 /*
224  * PCF8584 datasheet : when operate at 8 MHz or more, a minimun time of
225  * 6 clocks cycles must be left between two consecutives access
226  */
227 #define pcf_nops()      DELAY(10)
228
229 #define dummy_read(pcf)         PCF_GET_S0(pcf)
230 #define dummy_write(pcf)        PCF_SET_S0(pcf, 0)
231
232 /*
233  * Specific register access to PCF8584
234  */
235 static void
236 PCF_SET_S0(struct pcf_softc *pcf, int data)
237 {
238         outb(pcf->pcf_base, data);
239         pcf_nops();
240 }
241
242 static void
243 PCF_SET_S1(struct pcf_softc *pcf, int data)
244 {
245         outb(pcf->pcf_base+1, data);
246         pcf_nops();
247 }
248
249 static char
250 PCF_GET_S0(struct pcf_softc *pcf)
251 {
252         char data;
253
254         data = inb(pcf->pcf_base);
255         pcf_nops();
256
257         return (data);
258 }
259
260 static char
261 PCF_GET_S1(struct pcf_softc *pcf)
262 {
263         char data;
264
265         data = inb(pcf->pcf_base+1);
266         pcf_nops();
267
268         return (data);
269 }
270
271 /*
272  * Polling mode for master operations wait for a new
273  * byte incomming or outgoing
274  */
275 static int
276 pcf_wait_byte(struct pcf_softc *pcf)
277 {
278         int counter = TIMEOUT;
279
280         while (counter--) {
281
282                 if ((PCF_GET_S1(pcf) & PIN) == 0)
283                         return (0);
284         }
285
286         return (IIC_ETIMEOUT);
287 }
288
289 static int
290 pcf_stop(device_t pcfdev)
291 {
292         struct pcf_softc *pcf = DEVTOSOFTC(pcfdev);
293
294         /*
295          * Send STOP condition iff the START condition was previously sent.
296          * STOP is sent only once even if a iicbus_stop() is called after
297          * an iicbus_read()... see pcf_read(): the pcf needs to send the stop
298          * before the last char is read.
299          */
300         if (pcf->pcf_started) {
301                 /* set stop condition and enable IT */
302                 PCF_SET_S1(pcf, PIN|ES0|ENI|STO|ACK);
303
304                 pcf->pcf_started = 0;
305         }
306
307         return (0);
308 }
309
310
311 static int
312 pcf_noack(struct pcf_softc *pcf, int timeout)
313 {
314         int noack;
315         int k = timeout/10;
316
317         do {
318                 noack = PCF_GET_S1(pcf) & LRB;
319                 if (!noack)
320                         break;
321                 DELAY(10);                              /* XXX wait 10 us */
322         } while (k--);
323
324         return (noack);
325 }
326
327 static int
328 pcf_repeated_start(device_t pcfdev, u_char slave, int timeout)
329 {
330         struct pcf_softc *pcf = DEVTOSOFTC(pcfdev);
331         int error = 0;
332
333         /* repeated start */
334         PCF_SET_S1(pcf, ES0|STA|STO|ACK);
335
336         /* set slave address to PCF. Last bit (LSB) must be set correctly
337          * according to transfer direction */
338         PCF_SET_S0(pcf, slave);
339
340         /* wait for address sent, polling */
341         if ((error = pcf_wait_byte(pcf)))
342                 goto error;
343
344         /* check for ack */
345         if (pcf_noack(pcf, timeout)) {
346                 error = IIC_ENOACK;
347                 goto error;
348         }
349
350         return (0);
351
352 error:
353         pcf_stop(pcfdev);
354         return (error);
355 }
356
357 static int
358 pcf_start(device_t pcfdev, u_char slave, int timeout)
359 {
360         struct pcf_softc *pcf = DEVTOSOFTC(pcfdev);
361         int error = 0;
362
363         if ((PCF_GET_S1(pcf) & nBB) == 0)
364                 return (IIC_EBUSBSY);
365
366         /* set slave address to PCF. Last bit (LSB) must be set correctly
367          * according to transfer direction */
368         PCF_SET_S0(pcf, slave);
369
370         /* START only */
371         PCF_SET_S1(pcf, PIN|ES0|STA|ACK);
372
373         pcf->pcf_started = 1;
374
375         /* wait for address sent, polling */
376         if ((error = pcf_wait_byte(pcf)))
377                 goto error;
378
379         /* check for ACK */
380         if (pcf_noack(pcf, timeout)) {
381                 error = IIC_ENOACK;
382                 goto error;
383         }
384
385         return (0);
386
387 error:
388         pcf_stop(pcfdev);
389         return (error);
390 }
391
392 static void
393 pcfintr(void *arg)
394 {
395         device_t pcfdev = (device_t)arg;
396         struct pcf_softc *pcf = DEVTOSOFTC(pcfdev);
397
398         char data, status, addr;
399         char error = 0;
400
401         status = PCF_GET_S1(pcf);
402
403         if (status & PIN) {
404                 device_printf(pcfdev, "spurious interrupt, status=0x%x\n", status & 0xff);
405
406                 goto error;
407         }       
408
409         if (status & LAB)
410                 device_printf(pcfdev, "bus arbitration lost!\n");
411
412         if (status & BER) {
413                 error = IIC_EBUSERR;
414                 iicbus_intr(pcf->iicbus, INTR_ERROR, &error);
415
416                 goto error;
417         }
418
419         do {
420                 status = PCF_GET_S1(pcf);
421
422                 switch(pcf->pcf_slave_mode) {
423
424                 case SLAVE_TRANSMITTER:
425                         if (status & LRB) {
426                                 /* ack interrupt line */
427                                 dummy_write(pcf);
428
429                                 /* no ack, don't send anymore */
430                                 pcf->pcf_slave_mode = SLAVE_RECEIVER;
431
432                                 iicbus_intr(pcf->iicbus, INTR_NOACK, NULL);
433                                 break;
434                         }
435
436                         /* get data from upper code */
437                         iicbus_intr(pcf->iicbus, INTR_TRANSMIT, &data);
438
439                         PCF_SET_S0(pcf, data);  
440                         break;  
441                 
442                 case SLAVE_RECEIVER:
443                         if (status & AAS) {
444                                 addr = PCF_GET_S0(pcf);
445
446                                 if (status & AD0)
447                                         iicbus_intr(pcf->iicbus, INTR_GENERAL, &addr);
448                                 else
449                                         iicbus_intr(pcf->iicbus, INTR_START, &addr);
450
451                                 if (addr & LSB) {
452                                         pcf->pcf_slave_mode = SLAVE_TRANSMITTER;
453
454                                         /* get the first char from upper code */
455                                         iicbus_intr(pcf->iicbus, INTR_TRANSMIT, &data);
456
457                                         /* send first data byte */
458                                         PCF_SET_S0(pcf, data);
459                                 }
460
461                                 break;
462                         }
463
464                         /* stop condition received? */
465                         if (status & STS) {
466                                 /* ack interrupt line */
467                                 dummy_read(pcf);
468
469                                 /* emulate intr stop condition */
470                                 iicbus_intr(pcf->iicbus, INTR_STOP, NULL);
471
472                         } else {
473                                 /* get data, ack interrupt line */
474                                 data = PCF_GET_S0(pcf);
475
476                                 /* deliver the character */
477                                 iicbus_intr(pcf->iicbus, INTR_RECEIVE, &data);
478                         }
479                         break;
480
481                     default:
482                         panic("%s: unknown slave mode (%d)!", __func__,
483                                 pcf->pcf_slave_mode);
484                     }
485
486         } while ((PCF_GET_S1(pcf) & PIN) == 0);
487
488         return;
489
490 error:
491         /* unknown event on bus...reset PCF */
492         PCF_SET_S1(pcf, PIN|ES0|ENI|ACK);
493
494         pcf->pcf_slave_mode = SLAVE_RECEIVER;
495
496         return;
497 }
498
499 static int
500 pcf_rst_card(device_t pcfdev, u_char speed, u_char addr, u_char *oldaddr)
501 {
502         struct pcf_softc *pcf = DEVTOSOFTC(pcfdev);
503
504         if (oldaddr)
505                 *oldaddr = pcf->pcf_addr;
506
507         /* retrieve own address from bus level */
508         if (!addr)
509                 pcf->pcf_addr = PCF_DEFAULT_ADDR;
510         else
511                 pcf->pcf_addr = addr;
512         
513         PCF_SET_S1(pcf, PIN);                           /* initialize S1 */
514
515         /* own address S'O<>0 */
516         PCF_SET_S0(pcf, pcf->pcf_addr >> 1);
517
518         /* select clock register */
519         PCF_SET_S1(pcf, PIN|ES1);
520
521         /* select bus speed : 18=90kb, 19=45kb, 1A=11kb, 1B=1.5kb */
522         switch (speed) {
523         case IIC_SLOW:
524                 PCF_SET_S0(pcf,  0x1b);
525                 break;
526
527         case IIC_FAST:
528                 PCF_SET_S0(pcf,  0x19);
529                 break;
530
531         case IIC_UNKNOWN:
532         case IIC_FASTEST:
533         default:
534                 PCF_SET_S0(pcf,  0x18);
535                 break;
536         }
537
538         /* set bus on, ack=yes, INT=yes */
539         PCF_SET_S1(pcf, PIN|ES0|ENI|ACK);
540
541         pcf->pcf_slave_mode = SLAVE_RECEIVER;
542
543         return (0);
544 }
545
546 static int
547 pcf_write(device_t pcfdev, char *buf, int len, int *sent, int timeout /* us */)
548 {
549         struct pcf_softc *pcf = DEVTOSOFTC(pcfdev);
550         int bytes, error = 0;
551
552 #ifdef PCFDEBUG
553         kprintf("pcf%d: >> writing %d bytes\n", device_get_unit(pcfdev), len);
554 #endif
555
556         bytes = 0;
557         while (len) {
558
559                 PCF_SET_S0(pcf, *buf++);
560
561                 /* wait for the byte to be send */
562                 if ((error = pcf_wait_byte(pcf)))
563                         goto error;
564
565                 /* check if ack received */
566                 if (pcf_noack(pcf, timeout)) {
567                         error = IIC_ENOACK;
568                         goto error;
569                 }
570
571                 len --;
572                 bytes ++;
573         }
574
575 error:
576         *sent = bytes;
577
578 #ifdef PCFDEBUG
579         kprintf("pcf%d: >> %d bytes written (%d)\n",
580                 device_get_unit(pcfdev), bytes, error);
581 #endif
582
583         return (error);
584 }
585
586 static int
587 pcf_read(device_t pcfdev, char *buf, int len, int *read, int last,
588                                                         int delay /* us */)
589 {
590         struct pcf_softc *pcf = DEVTOSOFTC(pcfdev);
591         int bytes, error = 0;
592
593 #ifdef PCFDEBUG
594         kprintf("pcf%d: << reading %d bytes\n", device_get_unit(pcfdev), len);
595 #endif
596
597         /* trig the bus to get the first data byte in S0 */
598         if (len) {
599                 if (len == 1 && last)
600                         /* just one byte to read */
601                         PCF_SET_S1(pcf, ES0);           /* no ack */
602
603                 dummy_read(pcf);
604         }
605
606         bytes = 0;
607         while (len) {
608
609                 /* XXX delay needed here */
610
611                 /* wait for trigged byte */
612                 if ((error = pcf_wait_byte(pcf))) {
613                         pcf_stop(pcfdev);
614                         goto error;
615                 }
616
617                 if (len == 1 && last)
618                         /* ok, last data byte already in S0, no I2C activity
619                          * on next PCF_GET_S0() */
620                         pcf_stop(pcfdev);
621
622                 else if (len == 2 && last)
623                         /* next trigged byte with no ack */
624                         PCF_SET_S1(pcf, ES0);
625
626                 /* receive byte, trig next byte */
627                 *buf++ = PCF_GET_S0(pcf);
628
629                 len --;
630                 bytes ++;
631         };
632
633 error:
634         *read = bytes;
635
636 #ifdef PCFDEBUG
637         kprintf("pcf%d: << %d bytes read (%d)\n",
638                 device_get_unit(pcfdev), bytes, error);
639 #endif
640
641         return (error);
642 }
643
644 DRIVER_MODULE(pcf, isa, pcf_driver, pcf_devclass, 0, 0);