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