xref: /titanic_44/usr/src/uts/sun4u/io/i2c/nexus/pcf8584.c (revision 60b08185ce63023f22fd6b2ed0db8c0d119b2023)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * pcf8584.c is the nexus driver for all pcf8584 controller
31  * implementations.  It supports both interrupt and polled
32  * mode operation, but defaults to interrupt.
33  */
34 
35 #include <sys/types.h>
36 #include <sys/conf.h>
37 #include <sys/file.h>
38 #include <sys/open.h>
39 #include <sys/ddi.h>
40 #include <sys/sunddi.h>
41 #include <sys/sunndi.h>
42 #include <sys/modctl.h>
43 #include <sys/stat.h>
44 #include <sys/kmem.h>
45 #include <sys/archsystm.h>
46 #include <sys/platform_module.h>
47 
48 #include <sys/i2c/clients/i2c_client.h>
49 #include <sys/i2c/misc/i2c_svc.h>
50 #include <sys/i2c/misc/i2c_svc_impl.h>
51 #include <sys/i2c/nexus/pcf8584.h>
52 
53 #include <sys/note.h>
54 
55 /*
56  * static function declarations
57  */
58 static void pcf8584_resume(dev_info_t *dip);
59 static void pcf8584_suspend(dev_info_t *dip);
60 static int pcf8584_bus_ctl(dev_info_t *dip, dev_info_t *rdip,
61 	ddi_ctl_enum_t op, void *arg, void *result);
62 static  void pcf8584_acquire(pcf8584_t *, dev_info_t *dip,
63 	i2c_transfer_t *tp, boolean_t force);
64 static  void pcf8584_release(pcf8584_t *, boolean_t force);
65 static int pcf8584_attach(dev_info_t *dip, ddi_attach_cmd_t cmd);
66 static int pcf8584_detach(dev_info_t *dip, ddi_detach_cmd_t cmd);
67 static int pcf8584_open(dev_t *devp, int flag, int otyp,
68     cred_t *cred_p);
69 static int pcf8584_close(dev_t dev, int flag, int otyp,
70     cred_t *cred_p);
71 static int pcf8584_ioctl(dev_t, int, intptr_t, int, cred_t *, int *);
72 static void pcf8584_select_bus(pcf8584_t *i2c);
73 static enum tran_state pcf8584_type_to_state(int i2c_flags);
74 static void pcf8584_put_s1(pcf8584_t *i2c, char cmd);
75 static void pcf8584_put_s0(pcf8584_t *i2c, char data);
76 static uint8_t pcf8584_get_s0(pcf8584_t *i2c);
77 static uint8_t pcf8584_get_s1(pcf8584_t *i2c);
78 static int pcf8584_bbn_ready(pcf8584_t *i2c);
79 static int pcf8584_error(int status, uint8_t rdwr, pcf8584_t *i2c);
80 static void pcf8584_monitor_mode(pcf8584_t *i2c);
81 static int pcf8584_initchild(dev_info_t *cdip);
82 static void pcf8584_uninitchild(dev_info_t *cdip);
83 static void pcf8584_init(pcf8584_t *i2c);
84 static int pcf8584_setup_regs(dev_info_t *dip, pcf8584_t *i2c);
85 static void pcf8584_free_regs(pcf8584_t *i2c);
86 static void pcf8584_reportdev(dev_info_t *dip, dev_info_t *rdip);
87 static int pcf8584_dip_to_addr(dev_info_t *dip);
88 static uint_t pcf8584_intr(caddr_t arg);
89 static int pcf8584_process(pcf8584_t *i2c, uint8_t s1);
90 int pcf8584_transfer(dev_info_t *dip, i2c_transfer_t *tp);
91 
92 static void pcf8584_do_polled_io(pcf8584_t *i2c);
93 static void pcf8584_take_over(pcf8584_t *i2c, dev_info_t *dip,
94     i2c_transfer_t *tp, kcondvar_t **waiter, int *saved_mode);
95 static void pcf8584_give_up(pcf8584_t *i2c, kcondvar_t *waiter, int saved_mode);
96 
97 static struct bus_ops pcf8584_busops = {
98 	BUSO_REV,
99 	nullbusmap,			/* bus_map */
100 	NULL,				/* bus_get_intrspec */
101 	NULL,				/* bus_add_intrspec */
102 	NULL,				/* bus_remove_intrspec */
103 	NULL,				/* bus_map_fault */
104 	ddi_no_dma_map,			/* bus_dma_map */
105 	ddi_no_dma_allochdl,		/* bus_dma_allochdl */
106 	ddi_no_dma_freehdl,		/* bus_dma_freehdl */
107 	ddi_no_dma_bindhdl,		/* bus_dma_bindhdl */
108 	ddi_no_dma_unbindhdl,		/* bus_unbindhdl */
109 	ddi_no_dma_flush,		/* bus_dma_flush */
110 	ddi_no_dma_win,			/* bus_dma_win */
111 	ddi_no_dma_mctl,		/* bus_dma_ctl */
112 	pcf8584_bus_ctl,		/* bus_ctl */
113 	ddi_bus_prop_op,		/* bus_prop_op */
114 	NULL,				/* bus_get_eventcookie */
115 	NULL,				/* bus_add_eventcall */
116 	NULL,				/* bus_remove_eventcall */
117 	NULL,				/* bus_post_event */
118 	0,				/* bus_intr_ctl */
119 	0,				/* bus_config		*/
120 	0,				/* bus_unconfig		*/
121 	0,				/* bus_fm_init		*/
122 	0,				/* bus_fm_fini		*/
123 	0,				/* bus_fm_access_enter	*/
124 	0,				/* bus_fm_access_exit	*/
125 	0,				/* bus_power		*/
126 	i_ddi_intr_ops			/* bus_intr_op		*/
127 };
128 
129 struct cb_ops pcf8584_cb_ops = {
130 	pcf8584_open,		/* open */
131 	pcf8584_close,	/* close */
132 	nodev,			/* strategy */
133 	nodev,			/* print */
134 	nodev,			/* dump */
135 	nodev,			/* read */
136 	nodev,			/* write */
137 	pcf8584_ioctl,		/* ioctl */
138 	nodev,			/* devmap */
139 	nodev,			/* mmap */
140 	nodev,			/* segmap */
141 	nochpoll,		/* poll */
142 	ddi_prop_op,		/* cb_prop_op */
143 	0,			/* streamtab  */
144 	D_MP | D_NEW		/* Driver compatibility flag */
145 };
146 
147 static struct dev_ops pcf8584_ops = {
148 	DEVO_REV,
149 	0,
150 	ddi_getinfo_1to1,
151 	nulldev,
152 	nulldev,
153 	pcf8584_attach,
154 	pcf8584_detach,
155 	nodev,
156 	&pcf8584_cb_ops,
157 	&pcf8584_busops
158 };
159 
160 static struct modldrv modldrv = {
161 	&mod_driverops, /* Type of module. This one is a driver */
162 	"I2C Nexus Driver %I%",	/* Name of the module. */
163 	&pcf8584_ops,		/* driver ops */
164 };
165 
166 static struct modlinkage modlinkage = {
167 	MODREV_1,
168 	&modldrv,
169 	NULL
170 };
171 
172 /*
173  * pcf8584 soft state
174  */
175 static void	*pcf8584_state;
176 
177 i2c_nexus_reg_t pcf8584_regvec = {
178 	I2C_NEXUS_REV,
179 	pcf8584_transfer,
180 };
181 
182 /*
183  * The "interrupt_priorities" property is how a driver can specify a SPARC
184  * PIL level to associate with each of its interrupt properties.  Most
185  * self-identifying busses have a better mechanism for managing this, but I2C
186  * doesn't.
187  */
188 int	pcf8584_pil = PCF8584_PIL;
189 
190 #ifdef DEBUG
191 int pcf8584_print_lvl = 0;
192 static kmutex_t msg_buf_lock;
193 static char msg_buff[1024];
194 #define	PCF8584_DDB(command)	\
195 	do {			\
196 		{ command; }	\
197 		_NOTE(CONSTANTCONDITION)	\
198 	} while (0)
199 
200 static void
201 pcf8584_print(int flags, const char *fmt, ...)
202 {
203 	if (flags & pcf8584_print_lvl) {
204 		va_list ap;
205 
206 		va_start(ap, fmt);
207 
208 		if (pcf8584_print_lvl & PRT_PROM) {
209 			prom_vprintf(fmt, ap);
210 		} else {
211 			mutex_enter(&msg_buf_lock);
212 			(void) vsprintf(msg_buff, fmt, ap);
213 			if (pcf8584_print_lvl & PRT_BUFFONLY) {
214 				cmn_err(CE_CONT, "?%s", msg_buff);
215 			} else {
216 				cmn_err(CE_CONT, "%s", msg_buff);
217 			}
218 			mutex_exit(&msg_buf_lock);
219 		}
220 		va_end(ap);
221 	}
222 }
223 #else
224 #define	PCF8584_DDB(command) \
225 	do {			\
226 		{ _NOTE(EMPTY); }	\
227 		_NOTE(CONSTANTCONDITION)	\
228 	} while (0)
229 #endif
230 
231 #define	PCF8584_IMPL_DELAY(type, delay)	\
232 	if (type == PIC16F747) {	\
233 		drv_usecwait(delay);	\
234 	}
235 
236 int
237 _init(void)
238 {
239 	int status;
240 
241 	status = ddi_soft_state_init(&pcf8584_state, sizeof (pcf8584_t),
242 		PCF8584_INITIAL_SOFT_SPACE);
243 	if (status != 0) {
244 
245 		return (status);
246 	}
247 
248 	if ((status = mod_install(&modlinkage)) != 0) {
249 		ddi_soft_state_fini(&pcf8584_state);
250 	}
251 
252 	return (status);
253 }
254 
255 int
256 _fini(void)
257 {
258 	int status;
259 
260 	if ((status = mod_remove(&modlinkage)) == 0) {
261 		ddi_soft_state_fini(&pcf8584_state);
262 	}
263 
264 	return (status);
265 }
266 
267 /*
268  * The loadable-module _info(9E) entry point
269  */
270 int
271 _info(struct modinfo *modinfop)
272 {
273 	return (mod_info(&modlinkage, modinfop));
274 }
275 
276 static void
277 pcf8584_dodetach(dev_info_t *dip)
278 {
279 	pcf8584_t *i2c;
280 	int instance = ddi_get_instance(dip);
281 
282 	i2c = (pcf8584_t *)ddi_get_soft_state(pcf8584_state, instance);
283 
284 	if ((i2c->pcf8584_attachflags & ADD_INTR) != 0) {
285 		ddi_remove_intr(dip, 0, i2c->pcf8584_icookie);
286 	}
287 
288 	cv_destroy(&i2c->pcf8584_cv);
289 
290 	if ((i2c->pcf8584_attachflags & IMUTEX) != 0) {
291 		mutex_destroy(&i2c->pcf8584_imutex);
292 		    cv_destroy(&i2c->pcf8584_icv);
293 	}
294 	if ((i2c->pcf8584_attachflags & SETUP_REGS) != 0) {
295 		pcf8584_free_regs(i2c);
296 	}
297 	if ((i2c->pcf8584_attachflags & NEXUS_REGISTER) != 0) {
298 		i2c_nexus_unregister(dip);
299 	}
300 	if ((i2c->pcf8584_attachflags & PROP_CREATE) != 0) {
301 		(void) ddi_prop_remove(DDI_DEV_T_NONE, dip,
302 			"interrupt-priorities");
303 	}
304 	if ((i2c->pcf8584_attachflags & MINOR_NODE) != 0) {
305 		ddi_remove_minor_node(dip, NULL);
306 	}
307 
308 	ddi_soft_state_free(pcf8584_state, instance);
309 }
310 
311 static int
312 pcf8584_doattach(dev_info_t *dip)
313 {
314 	pcf8584_t *i2c;
315 	int instance = ddi_get_instance(dip);
316 
317 	/*
318 	 * Allocate soft state structure.
319 	 */
320 	if (ddi_soft_state_zalloc(pcf8584_state, instance) != DDI_SUCCESS) {
321 
322 		return (DDI_FAILURE);
323 	}
324 
325 	i2c = (pcf8584_t *)ddi_get_soft_state(pcf8584_state, instance);
326 
327 	i2c->pcf8584_dip = dip;
328 
329 	(void) snprintf(i2c->pcf8584_name, sizeof (i2c->pcf8584_name),
330 		"%s_%d", ddi_node_name(dip), instance);
331 
332 	/*
333 	 * Identify which pcf8584 implementation is being attached to.
334 	 */
335 	if (strcmp(ddi_binding_name(i2c->pcf8584_dip), "SUNW,bbc-i2c") == 0) {
336 		i2c->pcf8584_impl_type = BBC;
337 		i2c->pcf8584_impl_delay = PCF8584_GENERIC_DELAY;
338 	} else if (strcmp(ddi_binding_name(i2c->pcf8584_dip),
339 	    "SUNW,i2c-pic16f747") == 0) {
340 		i2c->pcf8584_impl_type = PIC16F747;
341 		i2c->pcf8584_impl_delay = PCF8584_PIC16F747_DELAY;
342 	} else {
343 		i2c->pcf8584_impl_type = GENERIC;
344 		i2c->pcf8584_impl_delay = PCF8584_GENERIC_DELAY;
345 	}
346 
347 	if (ddi_prop_exists(DDI_DEV_T_ANY, dip,
348 	    DDI_PROP_NOTPROM | DDI_PROP_DONTPASS,
349 	    "interrupt-priorities") != 1) {
350 		(void) ddi_prop_create(DDI_DEV_T_NONE, dip,
351 			DDI_PROP_CANSLEEP, "interrupt-priorities",
352 			(caddr_t)&pcf8584_pil,
353 			sizeof (pcf8584_pil));
354 		i2c->pcf8584_attachflags |= PROP_CREATE;
355 	}
356 
357 	cv_init(&i2c->pcf8584_cv, NULL, CV_DRIVER, NULL);
358 
359 	if (pcf8584_setup_regs(dip, i2c) != DDI_SUCCESS) {
360 		goto bad;
361 	}
362 
363 	i2c->pcf8584_attachflags |= SETUP_REGS;
364 
365 	if (ddi_prop_exists(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS |
366 	    DDI_PROP_CANSLEEP, "poll-mode") == 1) {
367 		i2c->pcf8584_mode = PCF8584_POLL_MODE;
368 	} else {
369 
370 		if (ddi_get_iblock_cookie(dip, 0,
371 		    &i2c->pcf8584_icookie) == DDI_SUCCESS) {
372 			mutex_init(&i2c->pcf8584_imutex, NULL, MUTEX_DRIVER,
373 			    (void *)i2c->pcf8584_icookie);
374 			cv_init(&i2c->pcf8584_icv, NULL, CV_DRIVER, NULL);
375 			i2c->pcf8584_attachflags |= IMUTEX;
376 
377 			if (ddi_add_intr(dip, 0, NULL, NULL, pcf8584_intr,
378 			    (caddr_t)i2c) == DDI_SUCCESS) {
379 				i2c->pcf8584_attachflags |= ADD_INTR;
380 				i2c->pcf8584_mode = PCF8584_INTR_MODE;
381 			} else {
382 				cmn_err(CE_WARN, "%s failed to add interrupt",
383 				    i2c->pcf8584_name);
384 				i2c->pcf8584_mode = PCF8584_POLL_MODE;
385 			}
386 		} else {
387 			cmn_err(CE_WARN, "%s failed to retrieve iblock cookie. "
388 			    "Operating in POLL MODE only", i2c->pcf8584_name);
389 			i2c->pcf8584_mode = PCF8584_POLL_MODE;
390 		}
391 	}
392 
393 	/*
394 	 * For polled mode, still initialize a cv and mutex
395 	 */
396 	if ((i2c->pcf8584_attachflags & IMUTEX) == 0) {
397 		cv_init(&i2c->pcf8584_icv, NULL, CV_DRIVER, NULL);
398 		mutex_init(&i2c->pcf8584_imutex, NULL, MUTEX_DRIVER, NULL);
399 		i2c->pcf8584_attachflags |= IMUTEX;
400 	}
401 
402 	i2c_nexus_register(dip, &pcf8584_regvec);
403 	i2c->pcf8584_attachflags |= NEXUS_REGISTER;
404 
405 	if (ddi_create_minor_node(dip, "devctl", S_IFCHR, instance,
406 	    DDI_NT_NEXUS, 0) == DDI_FAILURE) {
407 		cmn_err(CE_WARN, "%s ddi_create_minor_node failed",
408 		    i2c->pcf8584_name);
409 		goto bad;
410 	}
411 
412 	i2c->pcf8584_attachflags |= MINOR_NODE;
413 
414 	pcf8584_init(i2c);
415 
416 	i2c->pcf8584_nexus_dip = dip;
417 
418 	return (DDI_SUCCESS);
419 
420 bad:
421 	pcf8584_dodetach(dip);
422 
423 	return (DDI_FAILURE);
424 }
425 
426 static int
427 pcf8584_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
428 {
429 	switch (cmd) {
430 	case DDI_ATTACH:
431 
432 		return (pcf8584_doattach(dip));
433 	case DDI_RESUME:
434 		pcf8584_resume(dip);
435 
436 		return (DDI_SUCCESS);
437 	default:
438 
439 		return (DDI_FAILURE);
440 	}
441 }
442 
443 static int
444 pcf8584_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
445 {
446 	switch (cmd) {
447 	case DDI_DETACH:
448 		pcf8584_dodetach(dip);
449 
450 		return (DDI_SUCCESS);
451 	case DDI_SUSPEND:
452 		pcf8584_suspend(dip);
453 
454 		return (DDI_SUCCESS);
455 	default:
456 
457 		return (DDI_FAILURE);
458 	}
459 }
460 
461 /*ARGSUSED*/
462 static int
463 pcf8584_open(dev_t  *devp,  int  flag,  int  otyp,  cred_t *cred_p)
464 {
465 	int instance;
466 	pcf8584_t *i2c;
467 
468 	/*
469 	 * Make sure the open is for the right file type
470 	 */
471 	if (otyp != OTYP_CHR)
472 		return (EINVAL);
473 
474 	instance = getminor(*devp);
475 	i2c = (pcf8584_t *)ddi_get_soft_state(pcf8584_state, instance);
476 	if (i2c == NULL)
477 		return (ENXIO);
478 
479 	/*
480 	 * Enforce exclusive access
481 	 */
482 	mutex_enter(&i2c->pcf8584_imutex);
483 	if (i2c->pcf8584_open) {
484 		mutex_exit(&i2c->pcf8584_imutex);
485 
486 		return (EBUSY);
487 	} else
488 		i2c->pcf8584_open = 1;
489 	mutex_exit(&i2c->pcf8584_imutex);
490 
491 	return (0);
492 }
493 
494 /*ARGSUSED*/
495 static int
496 pcf8584_close(dev_t  dev,  int  flag,  int  otyp,  cred_t *cred_p)
497 {
498 	int instance;
499 	pcf8584_t *i2c;
500 
501 	/*
502 	 * Make sure the close is for the right file type
503 	 */
504 	if (otyp != OTYP_CHR)
505 		return (EINVAL);
506 
507 	instance = getminor(dev);
508 	i2c = (pcf8584_t *)ddi_get_soft_state(pcf8584_state, instance);
509 	if (i2c == NULL)
510 		return (ENXIO);
511 
512 	mutex_enter(&i2c->pcf8584_imutex);
513 	i2c->pcf8584_open = 0;
514 	mutex_exit(&i2c->pcf8584_imutex);
515 
516 	return (0);
517 }
518 
519 /*ARGSUSED*/
520 static int
521 pcf8584_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp,
522 	int *rvalp)
523 {
524 	pcf8584_t *i2c;
525 	dev_info_t *self;
526 	struct devctl_iocdata *dcp;
527 	int rv;
528 
529 	i2c = (pcf8584_t *)ddi_get_soft_state(pcf8584_state, getminor(dev));
530 	if (i2c == NULL)
531 		return (ENXIO);
532 
533 	self = (dev_info_t *)i2c->pcf8584_nexus_dip;
534 
535 	/*
536 	 * read devctl ioctl data
537 	 */
538 	if (ndi_dc_allochdl((void *)arg, &dcp) != NDI_SUCCESS) {
539 
540 		return (EFAULT);
541 	}
542 
543 	switch (cmd) {
544 		case DEVCTL_BUS_DEV_CREATE:
545 			rv = ndi_dc_devi_create(dcp, self, 0, NULL);
546 			break;
547 		case DEVCTL_DEVICE_REMOVE:
548 			rv = ndi_devctl_device_remove(self, dcp, 0);
549 			break;
550 		default:
551 			rv = ENOTSUP;
552 	}
553 
554 	ndi_dc_freehdl(dcp);
555 
556 	return (rv);
557 }
558 
559 static int
560 pcf8584_bus_ctl(dev_info_t *dip, dev_info_t *rdip, ddi_ctl_enum_t op,
561     void *arg, void *result)
562 {
563 	switch (op) {
564 	case DDI_CTLOPS_INITCHILD:
565 
566 		return (pcf8584_initchild((dev_info_t *)arg));
567 	case DDI_CTLOPS_UNINITCHILD:
568 		pcf8584_uninitchild((dev_info_t *)arg);
569 
570 		return (DDI_SUCCESS);
571 	CTLOPS_REPORTDEV:
572 		pcf8584_reportdev(dip, rdip);
573 
574 		return (DDI_SUCCESS);
575 	case DDI_CTLOPS_DMAPMAPC:
576 	case DDI_CTLOPS_POKE:
577 	case DDI_CTLOPS_PEEK:
578 	case DDI_CTLOPS_IOMIN:
579 	case DDI_CTLOPS_REPORTINT:
580 	case DDI_CTLOPS_SIDDEV:
581 	case DDI_CTLOPS_SLAVEONLY:
582 	case DDI_CTLOPS_AFFINITY:
583 	case DDI_CTLOPS_PTOB:
584 	case DDI_CTLOPS_BTOP:
585 	case DDI_CTLOPS_BTOPR:
586 	case DDI_CTLOPS_DVMAPAGESIZE:
587 
588 		return (DDI_FAILURE);
589 	default:
590 
591 		return (ddi_ctlops(dip, rdip, op, arg, result));
592 	}
593 }
594 
595 /*
596  * pcf8584_suspend() is called before the system suspends.  Existing
597  * transfer in progress or waiting will complete, but new transfers are
598  * effectively blocked by "acquiring" the bus.
599  */
600 static void
601 pcf8584_suspend(dev_info_t *dip)
602 {
603 	pcf8584_t *i2c;
604 	int instance;
605 
606 	instance = ddi_get_instance(dip);
607 	i2c = (pcf8584_t *)ddi_get_soft_state(pcf8584_state, instance);
608 
609 	pcf8584_acquire(i2c, NULL, NULL, B_FALSE);
610 }
611 
612 /*
613  * pcf8584_resume() is called when the system resumes from CPR.  It releases
614  * the hold that was placed on the i2c bus, which allows any real
615  * transfers to continue.
616  */
617 static void
618 pcf8584_resume(dev_info_t *dip)
619 {
620 	pcf8584_t *i2c;
621 	int instance;
622 
623 	instance = ddi_get_instance(dip);
624 	i2c = (pcf8584_t *)ddi_get_soft_state(pcf8584_state, instance);
625 
626 	pcf8584_release(i2c, B_FALSE);
627 
628 	pcf8584_init(i2c);
629 }
630 
631 /*
632  * pcf8584_acquire() is called by a thread wishing to "own" the I2C bus.
633  * It should not be held across multiple transfers. If the 'force' flag
634  * is set, do not try to acquire mutex or do cv_wait.
635  */
636 static void
637 pcf8584_acquire(pcf8584_t *i2c, dev_info_t *dip, i2c_transfer_t *tp,
638     boolean_t force)
639 {
640 	if (force) {
641 		i2c->pcf8584_busy = 1;
642 		i2c->pcf8584_cur_tran = tp;
643 		i2c->pcf8584_cur_dip = dip;
644 		i2c->pcf8584_cur_status = PCF8584_TRANSFER_NEW;
645 		return;
646 	}
647 
648 	mutex_enter(&i2c->pcf8584_imutex);
649 	while (i2c->pcf8584_busy) {
650 		cv_wait(&i2c->pcf8584_cv, &i2c->pcf8584_imutex);
651 	}
652 	i2c->pcf8584_busy = 1;
653 	mutex_exit(&i2c->pcf8584_imutex);
654 	/*
655 	 * On systems where OBP shares a pcf8584 controller with the
656 	 * OS, plat_shared_i2c_enter will serialize access to the
657 	 * pcf8584 controller.  Do not grab this lock during CPR
658 	 * suspend as the CPR thread also acquires this muxex
659 	 * through through prom_setprop which causes recursive
660 	 * mutex enter.
661 	 *
662 	 * dip == NULL during CPR.
663 	 */
664 	if ((&plat_shared_i2c_enter != NULL) && (dip != NULL)) {
665 		plat_shared_i2c_enter(i2c->pcf8584_dip);
666 	}
667 
668 	mutex_enter(&i2c->pcf8584_imutex);
669 	i2c->pcf8584_cur_tran = tp;
670 	i2c->pcf8584_cur_dip = dip;
671 	mutex_exit(&i2c->pcf8584_imutex);
672 }
673 
674 /*
675  * pcf8584_release() is called to release a hold made by pcf8584_acquire().
676  */
677 static void
678 pcf8584_release(pcf8584_t *i2c, boolean_t force)
679 {
680 	if (force) {
681 		i2c->pcf8584_busy = 0;
682 		i2c->pcf8584_cur_tran = NULL;
683 		i2c->pcf8584_cur_dip = NULL;
684 		i2c->pcf8584_cur_status = PCF8584_TRANSFER_OVER;
685 		cv_signal(&i2c->pcf8584_cv);
686 		return;
687 	}
688 
689 	mutex_enter(&i2c->pcf8584_imutex);
690 	i2c->pcf8584_busy = 0;
691 	i2c->pcf8584_cur_tran = NULL;
692 	cv_signal(&i2c->pcf8584_cv);
693 	mutex_exit(&i2c->pcf8584_imutex);
694 
695 	if ((&plat_shared_i2c_exit != NULL) && (i2c->pcf8584_cur_dip != NULL)) {
696 		plat_shared_i2c_exit(i2c->pcf8584_dip);
697 	}
698 }
699 
700 /*
701  * if pcf8584_b_reg exists, it means the current bus controller signals
702  * are multiplexed into more than a single bus.  Select the bus needed
703  * by writing to the mux register.
704  */
705 static void
706 pcf8584_select_bus(pcf8584_t *i2c)
707 {
708 	int bus;
709 	pcf8584_ppvt_t *ppvt;
710 
711 	/*
712 	 * The existence of pcf8584_b_reg means the bus registers
713 	 * are multiplexed.
714 	 */
715 
716 	PCF8584_DDB(pcf8584_print(PRT_SELECT, "bus multiplex: %X\n",
717 	    i2c->pcf8584_b_reg));
718 	if (i2c->pcf8584_b_reg != NULL) {
719 		ppvt = ddi_get_parent_data(i2c->pcf8584_cur_dip);
720 
721 		bus = ppvt->pcf8584_ppvt_bus;
722 
723 		PCF8584_DDB(pcf8584_print(PRT_SELECT,
724 		    "transmitting bus number %d\n", bus));
725 
726 		ddi_put8(i2c->pcf8584_b_rhandle, i2c->pcf8584_b_reg, bus);
727 	}
728 }
729 
730 /*
731  * pcf8584_type_to_state() converts a transfer type to the
732  * next state of the I2C state machine based on the requested
733  * transfer type.
734  */
735 static enum tran_state
736 pcf8584_type_to_state(int i2c_flags)
737 {
738 	switch (i2c_flags) {
739 	case I2C_WR:
740 
741 		return (TRAN_STATE_WR);
742 	case I2C_RD:
743 
744 		return (TRAN_STATE_DUMMY_RD);
745 	case I2C_WR_RD:
746 
747 		return (TRAN_STATE_WR_RD);
748 	}
749 /*NOTREACHED*/
750 }
751 
752 /*
753  * pcf8584_put_s1() writes out cmd to register S1.
754  */
755 static void
756 pcf8584_put_s1(pcf8584_t *i2c, char cmd)
757 {
758 	ddi_acc_handle_t hp = i2c->pcf8584_rhandle;
759 	pcf8584_regs_t *rp = &i2c->pcf8584_regs;
760 
761 	ddi_put8(hp, rp->pcf8584_regs_s1, cmd);
762 	PCF8584_IMPL_DELAY(i2c->pcf8584_impl_type,
763 	    i2c->pcf8584_impl_delay);
764 	/*
765 	 * read status to make sure write is flushed
766 	 */
767 	(void) ddi_get8(hp, rp->pcf8584_regs_s1);
768 	PCF8584_IMPL_DELAY(i2c->pcf8584_impl_type,
769 	    i2c->pcf8584_impl_delay);
770 }
771 
772 /*
773  * pcf8584_put_s0() writes out data to register S0.
774  */
775 static void
776 pcf8584_put_s0(pcf8584_t *i2c, char data)
777 {
778 	ddi_acc_handle_t hp = i2c->pcf8584_rhandle;
779 	pcf8584_regs_t *rp = &i2c->pcf8584_regs;
780 
781 	ddi_put8(hp, rp->pcf8584_regs_s0, data);
782 	PCF8584_IMPL_DELAY(i2c->pcf8584_impl_type,
783 	    i2c->pcf8584_impl_delay);
784 	/*
785 	 * read status to make sure write is flushed
786 	 */
787 	(void) ddi_get8(hp, rp->pcf8584_regs_s1);
788 	PCF8584_IMPL_DELAY(i2c->pcf8584_impl_type,
789 	    i2c->pcf8584_impl_delay);
790 }
791 
792 /*
793  * pcf8584_get_s0() reads from register S0.
794  */
795 static uint8_t
796 pcf8584_get_s0(pcf8584_t *i2c)
797 {
798 	ddi_acc_handle_t hp = i2c->pcf8584_rhandle;
799 	pcf8584_regs_t *rp = &i2c->pcf8584_regs;
800 	uint8_t s0;
801 
802 	s0 = ddi_get8(hp, rp->pcf8584_regs_s0);
803 	PCF8584_IMPL_DELAY(i2c->pcf8584_impl_type,
804 	    i2c->pcf8584_impl_delay);
805 
806 	return (s0);
807 }
808 
809 /*
810  * pcf8584_get_s1() reads from register S1.
811  */
812 static uint8_t
813 pcf8584_get_s1(pcf8584_t *i2c)
814 {
815 	ddi_acc_handle_t hp = i2c->pcf8584_rhandle;
816 	pcf8584_regs_t *rp = &i2c->pcf8584_regs;
817 	uint8_t s1;
818 
819 	s1 = ddi_get8(hp, rp->pcf8584_regs_s1);
820 	PCF8584_IMPL_DELAY(i2c->pcf8584_impl_type,
821 	    i2c->pcf8584_impl_delay);
822 
823 	return (s1);
824 }
825 
826 /*
827  * If the previous transaction was a write, the stop
828  * bit may not make it out on the wire before
829  * the next transaction startes.  And unfortunately, there
830  * is no interrupt after the stop bit is written, so this
831  * function will poll to make sure the BBC is ready.
832  */
833 static int
834 pcf8584_bbn_ready(pcf8584_t *i2c)
835 {
836 	uint8_t s1;
837 	int usecwaits = 0;
838 
839 	s1 = pcf8584_get_s1(i2c);
840 
841 	while ((s1 & S1_BBN) == 0) {
842 
843 		if (usecwaits++ == 100) {
844 			/* Try initializing the bus */
845 			pcf8584_monitor_mode(i2c);
846 			pcf8584_put_s1(i2c, S1_STOP);
847 			delay(1);
848 			pcf8584_init(i2c);
849 			(void) pcf8584_get_s0(i2c);
850 			s1 = pcf8584_get_s1(i2c);
851 			if (s1 & S1_BBN) {
852 				cmn_err(CE_WARN,
853 				    "!%s: cleared bus busy.   addr=0x%x",
854 				    i2c->pcf8584_name,
855 				    pcf8584_dip_to_addr(i2c->pcf8584_cur_dip));
856 
857 				return (I2C_SUCCESS);
858 			} else {
859 				cmn_err(CE_WARN,
860 				    "!%s bus busy after init addr=0x%x",
861 				    i2c->pcf8584_name,
862 				    pcf8584_dip_to_addr(i2c->pcf8584_cur_dip));
863 
864 				return (I2C_FAILURE);
865 			}
866 		}
867 		drv_usecwait(1);
868 		s1 = pcf8584_get_s1(i2c);
869 	}
870 
871 	return (I2C_SUCCESS);
872 }
873 
874 static int
875 pcf8584_error(int status, uint8_t rdwr, pcf8584_t *i2c)
876 {
877 	int addr = pcf8584_dip_to_addr(i2c->pcf8584_cur_dip);
878 	pcf8584_regs_t *rp = &i2c->pcf8584_regs;
879 
880 	if (status & S1_BER) {
881 		cmn_err(CE_WARN,
882 		    "!%s bus error; Controller = 0x%x "
883 		    " addr = 0x%x", i2c->pcf8584_name,
884 		    (unsigned int)rp->pcf8584_regs_s1, addr);
885 		pcf8584_init(i2c);
886 
887 		return (I2C_FAILURE);
888 	} else if (status & S1_LAB) {
889 		cmn_err(CE_WARN, "!%s lost arbitration; Controller ="
890 		    " 0x%x addr = 0x%x", i2c->pcf8584_name,
891 		    (unsigned int)rp->pcf8584_regs_s1, addr);
892 		pcf8584_init(i2c);
893 
894 		return (I2C_FAILURE);
895 	} else if ((status & S1_LRB) && (rdwr == I2C_WR)) {
896 		/*
897 		 * No error logged here, because this may be benign.
898 		 * Cf. the "Alert Response Address" feature of SMBUS.
899 		 */
900 		pcf8584_put_s1(i2c, S1_STOP);
901 
902 		return (I2C_FAILURE);
903 	}
904 
905 	return (I2C_SUCCESS);
906 }
907 
908 static void
909 pcf8584_monitor_mode(pcf8584_t *i2c)
910 {
911 	pcf8584_put_s1(i2c, S1_PIN);
912 
913 	pcf8584_put_s0(i2c, MONITOR_ADDRESS);
914 }
915 
916 static int
917 pcf8584_initchild(dev_info_t *cdip)
918 {
919 	int32_t cell_size;
920 	int len;
921 	int32_t regs[2];
922 	int err;
923 	pcf8584_ppvt_t *ppvt;
924 	char name[30];
925 
926 	PCF8584_DDB(pcf8584_print(PRT_INIT, "pcf8584_initchild enter: %s\n",
927 		ddi_node_name(cdip)));
928 
929 	ppvt = kmem_alloc(sizeof (pcf8584_ppvt_t), KM_SLEEP);
930 
931 	len = sizeof (cell_size);
932 	err = ddi_getlongprop_buf(DDI_DEV_T_ANY, cdip,
933 		DDI_PROP_CANSLEEP, "#address-cells",
934 		(caddr_t)&cell_size, &len);
935 	if (err != DDI_PROP_SUCCESS || len != sizeof (cell_size)) {
936 
937 		return (DDI_FAILURE);
938 	}
939 
940 	len = sizeof (regs);
941 	err = ddi_getlongprop_buf(DDI_DEV_T_ANY, cdip,
942 		DDI_PROP_DONTPASS | DDI_PROP_CANSLEEP,
943 		"reg", (caddr_t)regs, &len);
944 	if (err != DDI_PROP_SUCCESS ||
945 		len != (cell_size * sizeof (int32_t))) {
946 
947 		return (DDI_FAILURE);
948 	}
949 
950 	if (cell_size == 1) {
951 		ppvt->pcf8584_ppvt_addr = regs[0];
952 		(void) sprintf(name, "%x", regs[0]);
953 	} else if (cell_size == 2) {
954 		ppvt->pcf8584_ppvt_bus = regs[0];
955 		ppvt->pcf8584_ppvt_addr = regs[1];
956 		(void) sprintf(name, "%x,%x", regs[0], regs[1]);
957 	} else {
958 
959 		return (DDI_FAILURE);
960 	}
961 
962 	ddi_set_parent_data(cdip, ppvt);
963 
964 	ddi_set_name_addr(cdip, name);
965 
966 	PCF8584_DDB(pcf8584_print(PRT_INIT,
967 	    "pcf8584_initchild SUCCESS: %s\n", ddi_node_name(cdip)));
968 
969 	return (DDI_SUCCESS);
970 }
971 
972 static void
973 pcf8584_uninitchild(dev_info_t *cdip)
974 {
975 	pcf8584_ppvt_t *ppvt;
976 
977 	ppvt = ddi_get_parent_data(cdip);
978 	kmem_free(ppvt, sizeof (pcf8584_ppvt_t));
979 
980 	ddi_set_parent_data(cdip, NULL);
981 	ddi_set_name_addr(cdip, NULL);
982 
983 	PCF8584_DDB(pcf8584_print(PRT_INIT, "i2c_uninitchild: %s\n",
984 	    ddi_node_name(cdip)));
985 }
986 
987 static void
988 pcf8584_init(pcf8584_t *i2c)
989 {
990 	uint8_t clk_div = 0x1C;
991 
992 	pcf8584_put_s1(i2c, S1_PIN);
993 
994 	pcf8584_put_s0(i2c, S0_OWN);
995 
996 	pcf8584_put_s1(i2c, S1_PIN | S1_ES1);
997 
998 	/*
999 	 * The default case is to set the clock divisor to the least common
1000 	 * denominator to avoid over clocking the I2C bus.  Assume that
1001 	 * BBC based systems are using the Safari clock as input, so select
1002 	 * the clk divisor based on it.
1003 	 */
1004 	if (i2c->pcf8584_impl_type == BBC) {
1005 		dev_info_t *root_node;
1006 		int clock_freq;
1007 		root_node = ddi_root_node();
1008 		clock_freq = ddi_prop_get_int(DDI_DEV_T_ANY, root_node,
1009 		    DDI_PROP_DONTPASS, "clock-frequency", 0);
1010 
1011 		if (clock_freq < 105000000) {
1012 			clk_div = 0x00;
1013 		} else if (clock_freq < 160000000) {
1014 			clk_div = 0x10;
1015 		} else {
1016 			clk_div = 0x1C;
1017 		}
1018 	}
1019 
1020 	/* set I2C clock speed */
1021 	pcf8584_put_s0(i2c, clk_div);
1022 
1023 	pcf8584_put_s1(i2c, S1_PIN | S1_ESO | S1_ACK);
1024 
1025 	/*
1026 	 * Multi-Master: Wait for a period of time equal to the
1027 	 * longest I2C message.  This accounts for the case
1028 	 * where multiple controllers and, if this particular one
1029 	 * is "lagging", misses the BB(bus busy) condition.
1030 	 * We wait 200 ms since the longest transaction at this time
1031 	 * on the i2c bus is a 256 byte read from the seprom which takes
1032 	 * about 75 ms. Some additional buffer does no harm to the driver.
1033 	 */
1034 
1035 	delay(drv_usectohz(PCF8584_INIT_WAIT));
1036 }
1037 
1038 /*
1039  * pcf8584_setup_regs() is called to map in registers specific to
1040  * the pcf8584.
1041  */
1042 static int
1043 pcf8584_setup_regs(dev_info_t *dip, pcf8584_t *i2c)
1044 {
1045 	int nregs;
1046 	ddi_device_acc_attr_t attr;
1047 	caddr_t reg_base;
1048 
1049 	attr.devacc_attr_version = DDI_DEVICE_ATTR_V0;
1050 	attr.devacc_attr_endian_flags = DDI_STRUCTURE_LE_ACC;
1051 	attr.devacc_attr_dataorder = DDI_STRICTORDER_ACC;
1052 
1053 	if (ddi_dev_nregs(dip, &nregs) != DDI_SUCCESS) {
1054 
1055 		return (DDI_FAILURE);
1056 	}
1057 
1058 	if (ddi_regs_map_setup(dip, 0,
1059 	    (caddr_t *)&reg_base, 0, 0, &attr,
1060 	    &i2c->pcf8584_rhandle) != DDI_SUCCESS) {
1061 
1062 		return (DDI_FAILURE);
1063 	}
1064 
1065 	/*
1066 	 * If i2c controller is on BBC, then s1 comes before s0.
1067 	 */
1068 	if (i2c->pcf8584_impl_type == BBC) {
1069 		i2c->pcf8584_regs.pcf8584_regs_s0 =
1070 		    (uint8_t *)&reg_base[1];
1071 		i2c->pcf8584_regs.pcf8584_regs_s1 =
1072 		    (uint8_t *)&reg_base[0];
1073 	} else {
1074 		i2c->pcf8584_regs.pcf8584_regs_s0 =
1075 		    (uint8_t *)&reg_base[0];
1076 		i2c->pcf8584_regs.pcf8584_regs_s1 =
1077 		    (uint8_t *)&reg_base[1];
1078 	}
1079 
1080 	if (nregs > 1) {
1081 		if (ddi_regs_map_setup(dip,
1082 			1, (caddr_t *)&i2c->pcf8584_b_reg,
1083 			0, 0, &attr, &i2c->pcf8584_b_rhandle) !=
1084 			DDI_SUCCESS) {
1085 
1086 			return (DDI_FAILURE);
1087 		}
1088 	}
1089 
1090 	return (DDI_SUCCESS);
1091 }
1092 
1093 /*
1094  * pcf8584_free_regs() frees any registers previously
1095  * allocated.
1096  */
1097 static void
1098 pcf8584_free_regs(pcf8584_t *i2c)
1099 {
1100 	if (i2c->pcf8584_regs.pcf8584_regs_s0 != NULL) {
1101 		ddi_regs_map_free(&i2c->pcf8584_rhandle);
1102 	}
1103 	if (i2c->pcf8584_b_reg != NULL) {
1104 		ddi_regs_map_free(&i2c->pcf8584_b_rhandle);
1105 	}
1106 }
1107 
1108 static void
1109 pcf8584_reportdev(dev_info_t *dip, dev_info_t *rdip)
1110 {
1111 	pcf8584_ppvt_t *ppvt;
1112 
1113 	ppvt = ddi_get_parent_data(rdip);
1114 
1115 	cmn_err(CE_CONT, "?%s%d at %s%d: addr 0x%x",
1116 	    ddi_driver_name(rdip), ddi_get_instance(rdip),
1117 	    ddi_driver_name(dip), ddi_get_instance(dip),
1118 	    ppvt->pcf8584_ppvt_addr);
1119 }
1120 
1121 /*
1122  * i2_nexus_dip_to_addr() takes a dip and returns an I2C address.
1123  */
1124 static int
1125 pcf8584_dip_to_addr(dev_info_t *dip)
1126 {
1127 	pcf8584_ppvt_t *ppvt;
1128 
1129 	ppvt = ddi_get_parent_data(dip);
1130 
1131 	return (ppvt->pcf8584_ppvt_addr);
1132 }
1133 
1134 /*
1135  * pcf8584_intr() is the interrupt service routine registered during
1136  * attach, and remains registered even if the driver is in POLLED mode.  So if
1137  * this is called from POLLED mode, it needs to return without doing
1138  * any work to prevent the I2C bus from entering an unknown state.
1139  */
1140 static uint_t
1141 pcf8584_intr(caddr_t arg)
1142 {
1143 	pcf8584_t *i2c = (pcf8584_t *)arg;
1144 	uint8_t s1;
1145 
1146 	ASSERT(i2c->pcf8584_mode != PCF8584_POLL_MODE);
1147 	PCF8584_DDB(pcf8584_print(PRT_INTR, "pcf8584_intr: enter\n"));
1148 
1149 	mutex_enter(&i2c->pcf8584_imutex);
1150 
1151 	/*
1152 	 * It is necessary to check both whether the hardware is interrupting
1153 	 * and that there is a current transaction for the bus in progress.
1154 	 * Checking just one but not the other will lead to a panic on xcal
1155 	 * since both controllers share the same ino, and also because OBP
1156 	 * shares a controller with the kernel even while the kernel is running.
1157 	 */
1158 
1159 	if (i2c->pcf8584_cur_tran == NULL) {
1160 		mutex_exit(&i2c->pcf8584_imutex);
1161 
1162 		return (DDI_INTR_UNCLAIMED);
1163 	}
1164 
1165 
1166 	s1 = pcf8584_get_s1(i2c);
1167 	if (s1 & S1_PIN) {
1168 		mutex_exit(&i2c->pcf8584_imutex);
1169 
1170 		return (DDI_INTR_UNCLAIMED);
1171 	}
1172 
1173 	if (pcf8584_process(i2c, s1) == I2C_COMPLETE) {
1174 		i2c->pcf8584_tran_state = TRAN_STATE_NULL;
1175 		i2c->pcf8584_cur_status = PCF8584_TRANSFER_OVER;
1176 		cv_signal(&i2c->pcf8584_icv);
1177 	} else
1178 		i2c->pcf8584_cur_status = PCF8584_TRANSFER_ON;
1179 
1180 	mutex_exit(&i2c->pcf8584_imutex);
1181 
1182 	return (DDI_INTR_CLAIMED);
1183 }
1184 
1185 /*
1186  * Interrupt occurs after a byte is transmitted or received, indicating
1187  * the device is ready to be serviced.
1188  */
1189 static int
1190 pcf8584_process(pcf8584_t *i2c, uint8_t s1)
1191 {
1192 	i2c_transfer_t *tp = i2c->pcf8584_cur_tran;
1193 	int addr = pcf8584_dip_to_addr(i2c->pcf8584_cur_dip);
1194 	int dummy_read;
1195 
1196 	ASSERT(i2c->pcf8584_tran_state != TRAN_STATE_NULL);
1197 
1198 	switch (i2c->pcf8584_tran_state) {
1199 	case TRAN_STATE_DUMMY_DATA:
1200 		PCF8584_DDB(pcf8584_print(PRT_TRAN,
1201 		    "TRAN_STATE_DUMMY DATA: write dummy %x\n", DUMMY_DATA));
1202 		if (pcf8584_error(s1, I2C_RD, i2c) != I2C_SUCCESS) {
1203 			tp->i2c_result = I2C_FAILURE;
1204 
1205 			return (I2C_COMPLETE);
1206 		}
1207 		i2c->pcf8584_tran_state = TRAN_STATE_START;
1208 		pcf8584_put_s0(i2c, DUMMY_DATA);
1209 
1210 		return (I2C_PENDING);
1211 	case TRAN_STATE_START:
1212 		if (pcf8584_error(s1, I2C_RD, i2c) != I2C_SUCCESS) {
1213 			PCF8584_DDB(pcf8584_print(PRT_TRAN,
1214 			    "TRAN_STATE_START failure\n"));
1215 			tp->i2c_result = I2C_FAILURE;
1216 
1217 			return (I2C_COMPLETE);
1218 		}
1219 		i2c->pcf8584_tran_state =
1220 			pcf8584_type_to_state(tp->i2c_flags);
1221 
1222 		/* Set read bit if this is a read transaction */
1223 		if (tp->i2c_flags == I2C_RD) {
1224 			addr |= I2C_READ;
1225 		}
1226 		if (i2c->pcf8584_mode == PCF8584_POLL_MODE)
1227 			pcf8584_put_s1(i2c, S1_START2);
1228 		else
1229 			pcf8584_put_s1(i2c, S1_START2 | S1_ENI);
1230 		pcf8584_put_s0(i2c, addr);
1231 		PCF8584_DDB(pcf8584_print(PRT_TRAN,
1232 		    "TRAN_STATE_START: write addr: %x\n", addr));
1233 
1234 		return (I2C_PENDING);
1235 	case TRAN_STATE_WR:
1236 
1237 		if (pcf8584_error(s1, I2C_WR, i2c) != I2C_SUCCESS) {
1238 			PCF8584_DDB(pcf8584_print(PRT_TRAN,
1239 			    "TRAN_STATE_WR failure\n"));
1240 			tp->i2c_result = I2C_FAILURE;
1241 
1242 			return (I2C_COMPLETE);
1243 		}
1244 		/* check to see if at end of buffer */
1245 		if (tp->i2c_w_resid == 0) {
1246 			pcf8584_put_s1(i2c, S1_STOP);
1247 			PCF8584_DDB(pcf8584_print(PRT_TRAN,
1248 			    "TRAN_STATE_WR: write STOP\n"));
1249 
1250 			return (I2C_COMPLETE);
1251 		}
1252 
1253 		pcf8584_put_s0(i2c, tp->i2c_wbuf[tp->i2c_wlen -
1254 		    tp->i2c_w_resid--]);
1255 		PCF8584_DDB(pcf8584_print(PRT_TRAN,
1256 		    "TRAN_STATE_WR:  write data %x\n",
1257 		    tp->i2c_wbuf[tp->i2c_wlen - (tp->i2c_w_resid + 1)]));
1258 
1259 		return (I2C_PENDING);
1260 	case TRAN_STATE_DUMMY_RD:
1261 
1262 		if (pcf8584_error(s1, I2C_WR, i2c) != I2C_SUCCESS) {
1263 			tp->i2c_result = I2C_FAILURE;
1264 
1265 			return (I2C_COMPLETE);
1266 		}
1267 		/*
1268 		 * The first read is always a dummy read, because reading S0
1269 		 * is what starts bit shifting and ACK on the I2c bus.
1270 		 * This byte is accessed during the next read, which starts
1271 		 * another 8 bit bus shift.
1272 		 *
1273 		 * special case for 1 byte reads:  Clear the ACK bit
1274 		 * here since this read causes the last and only byte
1275 		 * to be sent on the I2C bus.
1276 		 */
1277 		if (tp->i2c_r_resid  == 1) {
1278 			if (i2c->pcf8584_mode == PCF8584_POLL_MODE)
1279 				pcf8584_put_s1(i2c, S1_ESO);
1280 			else
1281 				pcf8584_put_s1(i2c, S1_ESO | S1_ENI);
1282 		}
1283 
1284 		/*
1285 		 * dummy read
1286 		 */
1287 		dummy_read = pcf8584_get_s0(i2c);
1288 
1289 		i2c->pcf8584_tran_state = TRAN_STATE_RD;
1290 		PCF8584_DDB(pcf8584_print(PRT_TRAN,
1291 		    "TRAN_STATE_DUMMY_RD: read dummy %d\n", dummy_read));
1292 
1293 		return (I2C_PENDING);
1294 	case TRAN_STATE_RD:
1295 		if (pcf8584_error(s1, I2C_RD, i2c) != I2C_SUCCESS) {
1296 			tp->i2c_result = I2C_FAILURE;
1297 			PCF8584_DDB(pcf8584_print(PRT_TRAN,
1298 			    "TRAN_STATE_RD failure\n"));
1299 
1300 			return (I2C_COMPLETE);
1301 		}
1302 
1303 		/*
1304 		 * If resid == 1, the last byte has already been shifted into
1305 		 * the accumulator.  Send the stop bit.  This also prevents the
1306 		 * last S0 read from shifting in another byte from the I2C bus.
1307 		 */
1308 		if (tp->i2c_r_resid  == 1) {
1309 			pcf8584_put_s1(i2c, S1_STOP);
1310 		}
1311 
1312 		/*
1313 		 * If resid == 2, then the next read will cause the I2C bus to
1314 		 * start shifting in the last byte on the I2C bus, which we
1315 		 * don't want to be ACK'd, so clear the ACK bit.
1316 		 */
1317 		if (tp->i2c_r_resid  == 2) {
1318 			if (i2c->pcf8584_mode == PCF8584_POLL_MODE)
1319 				pcf8584_put_s1(i2c, S1_ESO);
1320 			else
1321 				pcf8584_put_s1(i2c, S1_ESO | S1_ENI);
1322 		}
1323 
1324 		tp->i2c_rbuf[tp->i2c_rlen - tp->i2c_r_resid] =
1325 			pcf8584_get_s0(i2c);
1326 
1327 		PCF8584_DDB(pcf8584_print(PRT_TRAN,
1328 		    "TRAN_STATE_RD: returning. i2c_rlen = %d "
1329 		    "i2c_r_resid = %d,  data =%x\n", tp->i2c_rlen,
1330 		    tp->i2c_r_resid, tp->i2c_rbuf[tp->i2c_rlen -
1331 		    tp->i2c_r_resid]));
1332 
1333 		if (--tp->i2c_r_resid == 0) {
1334 
1335 			return (I2C_COMPLETE);
1336 		}
1337 
1338 		return (I2C_PENDING);
1339 	case TRAN_STATE_WR_RD:
1340 
1341 		if (pcf8584_error(s1, I2C_WR, i2c) != I2C_SUCCESS) {
1342 			tp->i2c_result = I2C_FAILURE;
1343 
1344 			return (I2C_COMPLETE);
1345 		}
1346 		if ((s1 & S1_LRB)) {
1347 			pcf8584_put_s1(i2c, S1_STOP);
1348 			PCF8584_DDB(pcf8584_print(PRT_TRAN,
1349 			    "TRAN_STATE_WR_RD sending STOP\n"));
1350 
1351 			return (I2C_COMPLETE);
1352 		}
1353 		if (tp->i2c_w_resid != 0) {
1354 			pcf8584_put_s0(i2c, tp->i2c_wbuf[tp->i2c_wlen -
1355 				tp->i2c_w_resid--]);
1356 			PCF8584_DDB(pcf8584_print(PRT_TRAN,
1357 			    "TRAN_STATE_WR_RD: write data %x\n",
1358 			    tp->i2c_wbuf[tp->i2c_wlen -
1359 			    (tp->i2c_w_resid + 1)]));
1360 		} else {
1361 			if (i2c->pcf8584_mode == PCF8584_POLL_MODE)
1362 				pcf8584_put_s1(i2c, S1_START2);
1363 			else
1364 				pcf8584_put_s1(i2c, S1_START2 | S1_ENI);
1365 			pcf8584_put_s0(i2c, addr | I2C_READ);
1366 			i2c->pcf8584_tran_state =
1367 				TRAN_STATE_DUMMY_RD;
1368 			PCF8584_DDB(pcf8584_print(PRT_TRAN,
1369 			    "TRAN_STATE_WR_RD: write addr "
1370 			    "%x\n", addr | I2C_READ));
1371 		}
1372 
1373 		return (I2C_PENDING);
1374 	default:
1375 
1376 		return (I2C_COMPLETE);
1377 	}
1378 }
1379 
1380 /*
1381  * pcf8584_transfer() is the function that is registered with
1382  * I2C services to be called from pcf8584_transfer() for each transfer.
1383  *
1384  * This function starts the transfer, and then waits for the
1385  * interrupt or polled thread to signal that the transfer has
1386  * completed.
1387  */
1388 int
1389 pcf8584_transfer(dev_info_t *dip, i2c_transfer_t *tp)
1390 {
1391 	pcf8584_t *i2c;
1392 	int saved_mode, took_over = 0;
1393 	kcondvar_t *waiter = NULL;
1394 	extern int do_polled_io;
1395 
1396 	i2c = (pcf8584_t *)ddi_get_soft_state(pcf8584_state,
1397 		ddi_get_instance(ddi_get_parent(dip)));
1398 
1399 	tp->i2c_r_resid = tp->i2c_rlen;
1400 	tp->i2c_w_resid = tp->i2c_wlen;
1401 	tp->i2c_result = I2C_SUCCESS;
1402 
1403 begin:
1404 	/*
1405 	 * If we're explicitly asked to do polled io (or if we are panic'ing),
1406 	 * we need to usurp ownership of the I2C bus, bypassing any other
1407 	 * waiters.
1408 	 */
1409 	if (do_polled_io || ddi_in_panic()) {
1410 		pcf8584_take_over(i2c, dip, tp, &waiter, &saved_mode);
1411 		took_over = 1;
1412 	} else {
1413 		pcf8584_acquire(i2c, dip, tp, B_FALSE);
1414 		mutex_enter(&i2c->pcf8584_imutex);
1415 
1416 		/*
1417 		 * See if someone else had intruded and taken over the bus
1418 		 * between the 'pcf8584_acquire' and 'mutex_enter' above.
1419 		 * If so, we'll have to start all over again.
1420 		 */
1421 		if (i2c->pcf8584_cur_tran != tp) {
1422 			mutex_exit(&i2c->pcf8584_imutex);
1423 			goto begin;
1424 		}
1425 	}
1426 
1427 	if (pcf8584_bbn_ready(i2c) != I2C_SUCCESS) {
1428 		if (took_over)
1429 			pcf8584_give_up(i2c, waiter, saved_mode);
1430 		else {
1431 			mutex_exit(&i2c->pcf8584_imutex);
1432 			pcf8584_release(i2c, B_FALSE);
1433 		}
1434 
1435 		return (tp->i2c_result = I2C_FAILURE);
1436 	}
1437 
1438 	/*
1439 	 * Bus selection must be followed by pcf8584_bbn_ready(),
1440 	 * otherwise the bus can be switched before the stop
1441 	 * bit is written out, causing the stop bit to get
1442 	 * sent to the wrong (new) bus.  This causes the
1443 	 * previous bus to permanently hang waiting for the
1444 	 * stop bit.
1445 	 */
1446 	pcf8584_select_bus(i2c);
1447 
1448 	i2c->pcf8584_tran_state = TRAN_STATE_DUMMY_DATA;
1449 	pcf8584_put_s0(i2c, DUMMY_ADDR);
1450 	PCF8584_DDB(pcf8584_print(PRT_TRAN,
1451 	    "FIRST WRITE DUMMY ADDR: write %x\n", DUMMY_ADDR));
1452 	if (i2c->pcf8584_mode ==  PCF8584_POLL_MODE)
1453 		pcf8584_put_s1(i2c, S1_START);
1454 	else
1455 		pcf8584_put_s1(i2c, S1_START | S1_ENI);
1456 
1457 	/*
1458 	 * Update transfer status so any polled i/o request coming in
1459 	 * after this will complete this transfer for us, before issuing
1460 	 * its own.
1461 	 */
1462 	i2c->pcf8584_cur_status = PCF8584_TRANSFER_ON;
1463 
1464 	if (i2c->pcf8584_mode ==  PCF8584_POLL_MODE)
1465 		pcf8584_do_polled_io(i2c);
1466 
1467 	if (took_over)
1468 		pcf8584_give_up(i2c, waiter, saved_mode);
1469 	else {
1470 		if (i2c->pcf8584_mode != PCF8584_POLL_MODE)
1471 			cv_wait(&i2c->pcf8584_icv, &i2c->pcf8584_imutex);
1472 		mutex_exit(&i2c->pcf8584_imutex);
1473 
1474 		/*
1475 		 * Release the I2C bus only if we still own it. If we don't
1476 		 * own it (someone usurped it from us while we were waiting),
1477 		 * we still need to drop the lock that serializes access to
1478 		 * the pcf8584 controller on systems where OBP shares the
1479 		 * controller with the OS.
1480 		 */
1481 		if (i2c->pcf8584_cur_tran == tp)
1482 			pcf8584_release(i2c, B_FALSE);
1483 		else if (&plat_shared_i2c_exit && dip)
1484 			plat_shared_i2c_exit(i2c->pcf8584_dip);
1485 	}
1486 
1487 	return (tp->i2c_result);
1488 }
1489 
1490 static void
1491 pcf8584_do_polled_io(pcf8584_t *i2c)
1492 {
1493 	int completed = I2C_PENDING;
1494 	uint8_t s1;
1495 
1496 	while (completed != I2C_COMPLETE) {
1497 		s1 = pcf8584_get_s1(i2c);
1498 		if (!(s1 & S1_PIN)) {
1499 			ASSERT(i2c->pcf8584_cur_tran);
1500 			completed = pcf8584_process(i2c, s1);
1501 		}
1502 		drv_usecwait(1);
1503 	}
1504 
1505 	i2c->pcf8584_cur_status = PCF8584_TRANSFER_OVER;
1506 }
1507 
1508 /*
1509  * pcf8584_take_over() grabs the I2C bus and other resources by force and
1510  * flushes any pending transaction. This is called if a polled i/o
1511  * request comes in.
1512  */
1513 static void
1514 pcf8584_take_over(pcf8584_t *i2c, dev_info_t *dip, i2c_transfer_t *tp,
1515     kcondvar_t **waiter, int *saved_mode)
1516 {
1517 	mutex_enter(&i2c->pcf8584_imutex);
1518 	*saved_mode = i2c->pcf8584_mode;
1519 	i2c->pcf8584_mode = PCF8584_POLL_MODE;
1520 
1521 	/*
1522 	 * We need to flush out any currently pending transaction before
1523 	 * issuing ours.
1524 	 */
1525 	if (i2c->pcf8584_busy) {
1526 		if (i2c->pcf8584_cur_tran &&
1527 		    i2c->pcf8584_cur_status == PCF8584_TRANSFER_ON) {
1528 			pcf8584_do_polled_io(i2c);
1529 			*waiter = &i2c->pcf8584_icv;
1530 		}
1531 	}
1532 
1533 	/*
1534 	 * Since pcf8584_acquire() is by default a good citizen that
1535 	 * will wait its turn to acquire the I2C bus, we need to set
1536 	 * the 'force' flag on.
1537 	 */
1538 	pcf8584_acquire(i2c, dip, tp, B_TRUE);
1539 }
1540 
1541 /*
1542  * pcf8584_give_up() returns all resources that were taken over forcefully
1543  */
1544 static void
1545 pcf8584_give_up(pcf8584_t *i2c, kcondvar_t *waiter, int saved_mode)
1546 {
1547 	i2c->pcf8584_mode = saved_mode;
1548 
1549 	/*
1550 	 * Note that pcf8584_release only wakes up threads waiting to acquire
1551 	 * the I2C bus. We still need to wake up the waiter from whom we
1552 	 * usurped the bus.
1553 	 */
1554 	pcf8584_release(i2c, B_TRUE);
1555 	if (waiter)
1556 		cv_signal(waiter);
1557 
1558 	mutex_exit(&i2c->pcf8584_imutex);
1559 }
1560