Deleted Added
full compact
iicbus.c (41591) iicbus.c (42442)
1/*-
2 * Copyright (c) 1998 Nicolas Souchu
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 *
1/*-
2 * Copyright (c) 1998 Nicolas Souchu
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 * $Id: iicbus.c,v 1.5 1998/11/22 22:01:42 nsouch Exp $
26 * $Id: iicbus.c,v 1.6 1998/12/07 21:58:16 archie Exp $
27 *
28 */
29
30/*
31 * Autoconfiguration and support routines for the Philips serial I2C bus
32 */
33
34#include <sys/param.h>
35#include <sys/systm.h>
36#include <sys/kernel.h>
37#include <sys/module.h>
38#include <sys/bus.h>
39
40#include <machine/clock.h>
41
42#include <dev/iicbus/iiconf.h>
43#include <dev/iicbus/iicbus.h>
44
45#include "iicbus_if.h"
46
47#define DEVTOIICBUS(dev) ((struct iicbus_device*)device_get_ivars(dev))
48
49/*
50 * structure used to attach devices to the I2C bus
51 */
52struct iicbus_device {
53 const char *iicd_name; /* device name */
54 int iicd_class; /* driver or slave device class */
55 const char *iicd_desc; /* device descriptor */
56 u_char iicd_addr; /* address of the device */
57 int iicd_waitack; /* wait for ack timeout or delay */
58 int iicd_alive; /* 1 if device found */
59};
60
61/*
62 * Common I2C addresses
63 */
64#define I2C_GENERAL_CALL 0x0
65#define PCF_MASTER_ADDRESS 0xaa
66#define FIRST_SLAVE_ADDR 0x2
67
68#define LAST_SLAVE_ADDR 255
69
70#define IICBUS_UNKNOWN_CLASS 0
71#define IICBUS_DEVICE_CLASS 1
72#define IICBUS_DRIVER_CLASS 2
73
74/*
75 * list of known devices
27 *
28 */
29
30/*
31 * Autoconfiguration and support routines for the Philips serial I2C bus
32 */
33
34#include <sys/param.h>
35#include <sys/systm.h>
36#include <sys/kernel.h>
37#include <sys/module.h>
38#include <sys/bus.h>
39
40#include <machine/clock.h>
41
42#include <dev/iicbus/iiconf.h>
43#include <dev/iicbus/iicbus.h>
44
45#include "iicbus_if.h"
46
47#define DEVTOIICBUS(dev) ((struct iicbus_device*)device_get_ivars(dev))
48
49/*
50 * structure used to attach devices to the I2C bus
51 */
52struct iicbus_device {
53 const char *iicd_name; /* device name */
54 int iicd_class; /* driver or slave device class */
55 const char *iicd_desc; /* device descriptor */
56 u_char iicd_addr; /* address of the device */
57 int iicd_waitack; /* wait for ack timeout or delay */
58 int iicd_alive; /* 1 if device found */
59};
60
61/*
62 * Common I2C addresses
63 */
64#define I2C_GENERAL_CALL 0x0
65#define PCF_MASTER_ADDRESS 0xaa
66#define FIRST_SLAVE_ADDR 0x2
67
68#define LAST_SLAVE_ADDR 255
69
70#define IICBUS_UNKNOWN_CLASS 0
71#define IICBUS_DEVICE_CLASS 1
72#define IICBUS_DRIVER_CLASS 2
73
74/*
75 * list of known devices
76 *
77 * XXX only one smb driver should exist for each I2C interface
76 */
77struct iicbus_device iicbus_children[] = {
78 { "iicsmb", IICBUS_DRIVER_CLASS, "I2C to SMB bridge" },
78 */
79struct iicbus_device iicbus_children[] = {
80 { "iicsmb", IICBUS_DRIVER_CLASS, "I2C to SMB bridge" },
79 { "iic", IICBUS_DEVICE_CLASS, "PCF8574 I2C to 8 bits parallel i/o", 64},
80 { "iic", IICBUS_DEVICE_CLASS, "PCF8584 as slave", PCF_MASTER_ADDRESS },
81 { "ic", IICBUS_DEVICE_CLASS, "network interface", PCF_MASTER_ADDRESS },
81 { "iic", IICBUS_DRIVER_CLASS, "I2C general purpose I/O" },
82#if 0
82#if 0
83 { "iic", IICBUS_DRIVER_CLASS, "General Call", I2C_GENERAL_CALL },
83 { "ic", IICBUS_DEVICE_CLASS, "network interface", PCF_MASTER_ADDRESS },
84#endif
85 { NULL, 0 }
86};
87
88static devclass_t iicbus_devclass;
89
90/*
91 * Device methods
92 */
93static int iicbus_probe(device_t);
94static int iicbus_attach(device_t);
95static void iicbus_print_child(device_t, device_t);
96static int iicbus_read_ivar(device_t , device_t, int, u_long *);
97static int iicbus_write_ivar(device_t , device_t, int, u_long);
98
99static device_method_t iicbus_methods[] = {
100 /* device interface */
101 DEVMETHOD(device_probe, iicbus_probe),
102 DEVMETHOD(device_attach, iicbus_attach),
103 DEVMETHOD(device_detach, bus_generic_detach),
104 DEVMETHOD(device_shutdown, bus_generic_shutdown),
105
106 /* bus interface */
107 DEVMETHOD(bus_print_child, iicbus_print_child),
108 DEVMETHOD(bus_read_ivar, iicbus_read_ivar),
109 DEVMETHOD(bus_write_ivar, iicbus_write_ivar),
110
111 { 0, 0 }
112};
113
114static driver_t iicbus_driver = {
115 "iicbus",
116 iicbus_methods,
117 DRIVER_TYPE_MISC,
118 sizeof(struct iicbus_softc),
119};
120
121static int
122iicbus_probe(device_t dev)
123{
84#endif
85 { NULL, 0 }
86};
87
88static devclass_t iicbus_devclass;
89
90/*
91 * Device methods
92 */
93static int iicbus_probe(device_t);
94static int iicbus_attach(device_t);
95static void iicbus_print_child(device_t, device_t);
96static int iicbus_read_ivar(device_t , device_t, int, u_long *);
97static int iicbus_write_ivar(device_t , device_t, int, u_long);
98
99static device_method_t iicbus_methods[] = {
100 /* device interface */
101 DEVMETHOD(device_probe, iicbus_probe),
102 DEVMETHOD(device_attach, iicbus_attach),
103 DEVMETHOD(device_detach, bus_generic_detach),
104 DEVMETHOD(device_shutdown, bus_generic_shutdown),
105
106 /* bus interface */
107 DEVMETHOD(bus_print_child, iicbus_print_child),
108 DEVMETHOD(bus_read_ivar, iicbus_read_ivar),
109 DEVMETHOD(bus_write_ivar, iicbus_write_ivar),
110
111 { 0, 0 }
112};
113
114static driver_t iicbus_driver = {
115 "iicbus",
116 iicbus_methods,
117 DRIVER_TYPE_MISC,
118 sizeof(struct iicbus_softc),
119};
120
121static int
122iicbus_probe(device_t dev)
123{
124 /* always present if probed */
124 device_set_desc(dev, "Philips I2C bus");
125
125 return (0);
126}
127
126 return (0);
127}
128
129#if 0
128static int
129iic_probe_device(device_t dev, u_char addr)
130{
131 int count;
132 char byte;
133
134 if ((addr & 1) == 0) {
135 /* is device writable? */
136 if (!iicbus_start(dev, (u_char)addr, 0)) {
137 iicbus_stop(dev);
138 return (1);
139 }
140 } else {
141 /* is device readable? */
142 if (!iicbus_block_read(dev, (u_char)addr, &byte, 1, &count))
143 return (1);
144 }
145
146 return (0);
147}
130static int
131iic_probe_device(device_t dev, u_char addr)
132{
133 int count;
134 char byte;
135
136 if ((addr & 1) == 0) {
137 /* is device writable? */
138 if (!iicbus_start(dev, (u_char)addr, 0)) {
139 iicbus_stop(dev);
140 return (1);
141 }
142 } else {
143 /* is device readable? */
144 if (!iicbus_block_read(dev, (u_char)addr, &byte, 1, &count))
145 return (1);
146 }
147
148 return (0);
149}
150#endif
148
149/*
150 * We add all the devices which we know about.
151 * The generic attach routine will attach them if they are alive.
152 */
153static int
154iicbus_attach(device_t dev)
155{
156 struct iicbus_device *iicdev;
157 device_t child;
151
152/*
153 * We add all the devices which we know about.
154 * The generic attach routine will attach them if they are alive.
155 */
156static int
157iicbus_attach(device_t dev)
158{
159 struct iicbus_device *iicdev;
160 device_t child;
158 int addr;
159
160 iicbus_reset(dev, IIC_FASTEST, 0, NULL);
161
161
162 iicbus_reset(dev, IIC_FASTEST, 0, NULL);
163
164 /* device probing is meaningless since the bus is supposed to be
165 * hot-plug. Moreover, some I2C chips do not appreciate random
166 * accesses like stop after start to fast, reads for less than
167 * x bytes...
168 */
169#if 0
162 printf("Probing for devices on iicbus%d:", device_get_unit(dev));
163
164 /* probe any devices */
165 for (addr = FIRST_SLAVE_ADDR; addr <= LAST_SLAVE_ADDR; addr++) {
166 if (iic_probe_device(dev, (u_char)addr)) {
167 printf(" <%x>", addr);
168 }
169 }
170 printf("\n");
170 printf("Probing for devices on iicbus%d:", device_get_unit(dev));
171
172 /* probe any devices */
173 for (addr = FIRST_SLAVE_ADDR; addr <= LAST_SLAVE_ADDR; addr++) {
174 if (iic_probe_device(dev, (u_char)addr)) {
175 printf(" <%x>", addr);
176 }
177 }
178 printf("\n");
179#endif
171
172 /* attach known devices */
173 for (iicdev = iicbus_children; iicdev->iicd_name; iicdev++) {
174 switch (iicdev->iicd_class) {
175 case IICBUS_DEVICE_CLASS:
176 /* check if the devclass exists */
180
181 /* attach known devices */
182 for (iicdev = iicbus_children; iicdev->iicd_name; iicdev++) {
183 switch (iicdev->iicd_class) {
184 case IICBUS_DEVICE_CLASS:
185 /* check if the devclass exists */
177 if (devclass_find(iicdev->iicd_name) &&
178 iic_probe_device(dev, iicdev->iicd_addr))
186 if (devclass_find(iicdev->iicd_name))
179 iicdev->iicd_alive = 1;
187 iicdev->iicd_alive = 1;
188 else if (bootverbose)
189 printf("iicbus: %s devclass not found\n",
190 iicdev->iicd_name);
180 break;
181
182 case IICBUS_DRIVER_CLASS:
183 /* check if the devclass exists */
191 break;
192
193 case IICBUS_DRIVER_CLASS:
194 /* check if the devclass exists */
184 if (!devclass_find(iicdev->iicd_name))
195 if (devclass_find(iicdev->iicd_name))
185 iicdev->iicd_alive = 1;
196 iicdev->iicd_alive = 1;
197 else if (bootverbose)
198 printf("iicbus: %s devclass not found\n",
199 iicdev->iicd_name);
186 break;
187
188 default:
189 panic("%s: unknown class!", __FUNCTION__);
190 }
191
192 if (iicdev->iicd_alive) {
193 child = device_add_child(dev, iicdev->iicd_name,
194 -1, iicdev);
195 device_set_desc(child, iicdev->iicd_desc);
196 }
197 }
198 bus_generic_attach(dev);
199
200 return (0);
201}
202
203int
204iicbus_generic_intr(device_t dev, int event, char *buf)
205{
206 return (0);
207}
208
209int
210iicbus_null_callback(device_t dev, int index, caddr_t data)
211{
212 return (0);
213}
214
215int
216iicbus_null_repeated_start(device_t dev, u_char addr)
217{
218 return (IIC_ENOTSUPP);
219}
220
221static void
222iicbus_print_child(device_t bus, device_t dev)
223{
224 struct iicbus_device* iicdev = DEVTOIICBUS(dev);
225
226 switch (iicdev->iicd_class) {
227 case IICBUS_DEVICE_CLASS:
228 printf(" on %s%d addr 0x%x", device_get_name(bus),
229 device_get_unit(bus), iicdev->iicd_addr);
230 break;
231
232 case IICBUS_DRIVER_CLASS:
233 printf(" on %s%d", device_get_name(bus),
234 device_get_unit(bus));
235 break;
236
237 default:
238 panic("%s: unknown class!", __FUNCTION__);
239 }
240
241 return;
242}
243
244static int
245iicbus_read_ivar(device_t bus, device_t dev, int index, u_long* result)
246{
247 struct iicbus_device* iicdev = DEVTOIICBUS(dev);
248
249 switch (index) {
250 case IICBUS_IVAR_ADDR:
251 *result = (u_long)iicdev->iicd_addr;
252 break;
253
254 default:
255 return (ENOENT);
256 }
257
258 return (0);
259}
260
261static int
262iicbus_write_ivar(device_t bus, device_t dev, int index, u_long val)
263{
264 switch (index) {
265 default:
266 return (ENOENT);
267 }
268
269 return (0);
270}
271
272DRIVER_MODULE(iicbus, pcf, iicbus_driver, iicbus_devclass, 0, 0);
273DRIVER_MODULE(iicbus, iicbb, iicbus_driver, iicbus_devclass, 0, 0);
274DRIVER_MODULE(iicbus, bti2c, iicbus_driver, iicbus_devclass, 0, 0);
200 break;
201
202 default:
203 panic("%s: unknown class!", __FUNCTION__);
204 }
205
206 if (iicdev->iicd_alive) {
207 child = device_add_child(dev, iicdev->iicd_name,
208 -1, iicdev);
209 device_set_desc(child, iicdev->iicd_desc);
210 }
211 }
212 bus_generic_attach(dev);
213
214 return (0);
215}
216
217int
218iicbus_generic_intr(device_t dev, int event, char *buf)
219{
220 return (0);
221}
222
223int
224iicbus_null_callback(device_t dev, int index, caddr_t data)
225{
226 return (0);
227}
228
229int
230iicbus_null_repeated_start(device_t dev, u_char addr)
231{
232 return (IIC_ENOTSUPP);
233}
234
235static void
236iicbus_print_child(device_t bus, device_t dev)
237{
238 struct iicbus_device* iicdev = DEVTOIICBUS(dev);
239
240 switch (iicdev->iicd_class) {
241 case IICBUS_DEVICE_CLASS:
242 printf(" on %s%d addr 0x%x", device_get_name(bus),
243 device_get_unit(bus), iicdev->iicd_addr);
244 break;
245
246 case IICBUS_DRIVER_CLASS:
247 printf(" on %s%d", device_get_name(bus),
248 device_get_unit(bus));
249 break;
250
251 default:
252 panic("%s: unknown class!", __FUNCTION__);
253 }
254
255 return;
256}
257
258static int
259iicbus_read_ivar(device_t bus, device_t dev, int index, u_long* result)
260{
261 struct iicbus_device* iicdev = DEVTOIICBUS(dev);
262
263 switch (index) {
264 case IICBUS_IVAR_ADDR:
265 *result = (u_long)iicdev->iicd_addr;
266 break;
267
268 default:
269 return (ENOENT);
270 }
271
272 return (0);
273}
274
275static int
276iicbus_write_ivar(device_t bus, device_t dev, int index, u_long val)
277{
278 switch (index) {
279 default:
280 return (ENOENT);
281 }
282
283 return (0);
284}
285
286DRIVER_MODULE(iicbus, pcf, iicbus_driver, iicbus_devclass, 0, 0);
287DRIVER_MODULE(iicbus, iicbb, iicbus_driver, iicbus_devclass, 0, 0);
288DRIVER_MODULE(iicbus, bti2c, iicbus_driver, iicbus_devclass, 0, 0);
275DRIVER_MODULE(iicbus, smbtx, iicbus_driver, iicbus_devclass, 0, 0);