xref: /titanic_50/usr/src/uts/sun4u/io/i2c/nexus/pcf8584.c (revision e07d9cb85217949d497b02d7211de8a197d2f2eb)
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 (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 /*
29  * pcf8584.c is the nexus driver for all pcf8584 controller
30  * implementations.  It supports both interrupt and polled
31  * mode operation, but defaults to interrupt.
32  */
33 
34 #include <sys/types.h>
35 #include <sys/conf.h>
36 #include <sys/file.h>
37 #include <sys/open.h>
38 #include <sys/ddi.h>
39 #include <sys/sunddi.h>
40 #include <sys/sunndi.h>
41 #include <sys/modctl.h>
42 #include <sys/stat.h>
43 #include <sys/kmem.h>
44 #include <sys/archsystm.h>
45 #include <sys/platform_module.h>
46 
47 #include <sys/i2c/clients/i2c_client.h>
48 #include <sys/i2c/misc/i2c_svc.h>
49 #include <sys/i2c/misc/i2c_svc_impl.h>
50 #include <sys/i2c/nexus/pcf8584.h>
51 
52 #include <sys/note.h>
53 
54 /*
55  * static function declarations
56  */
57 static void pcf8584_resume(dev_info_t *dip);
58 static void pcf8584_suspend(dev_info_t *dip);
59 static int pcf8584_bus_ctl(dev_info_t *dip, dev_info_t *rdip,
60 	ddi_ctl_enum_t op, void *arg, void *result);
61 static  void pcf8584_acquire(pcf8584_t *, dev_info_t *dip,
62 	i2c_transfer_t *tp, boolean_t force);
63 static  void pcf8584_release(pcf8584_t *, boolean_t force);
64 static int pcf8584_attach(dev_info_t *dip, ddi_attach_cmd_t cmd);
65 static int pcf8584_detach(dev_info_t *dip, ddi_detach_cmd_t cmd);
66 static int pcf8584_open(dev_t *devp, int flag, int otyp,
67     cred_t *cred_p);
68 static int pcf8584_close(dev_t dev, int flag, int otyp,
69     cred_t *cred_p);
70 static int pcf8584_ioctl(dev_t, int, intptr_t, int, cred_t *, int *);
71 static void pcf8584_select_bus(pcf8584_t *i2c);
72 static enum tran_state pcf8584_type_to_state(int i2c_flags);
73 static void pcf8584_put_s1(pcf8584_t *i2c, char cmd);
74 static void pcf8584_put_s0(pcf8584_t *i2c, char data);
75 static uint8_t pcf8584_get_s0(pcf8584_t *i2c);
76 static uint8_t pcf8584_get_s1(pcf8584_t *i2c);
77 static int pcf8584_bbn_ready(pcf8584_t *i2c);
78 static int pcf8584_error(int status, uint8_t rdwr, pcf8584_t *i2c);
79 static void pcf8584_monitor_mode(pcf8584_t *i2c);
80 static int pcf8584_initchild(dev_info_t *cdip);
81 static void pcf8584_uninitchild(dev_info_t *cdip);
82 static void pcf8584_init(pcf8584_t *i2c);
83 static int pcf8584_setup_regs(dev_info_t *dip, pcf8584_t *i2c);
84 static void pcf8584_free_regs(pcf8584_t *i2c);
85 static void pcf8584_reportdev(dev_info_t *dip, dev_info_t *rdip);
86 static int pcf8584_dip_to_addr(dev_info_t *dip);
87 static uint_t pcf8584_intr(caddr_t arg);
88 static int pcf8584_process(pcf8584_t *i2c, uint8_t s1);
89 int pcf8584_transfer(dev_info_t *dip, i2c_transfer_t *tp);
90 
91 static void pcf8584_do_polled_io(pcf8584_t *i2c);
92 static void pcf8584_take_over(pcf8584_t *i2c, dev_info_t *dip,
93     i2c_transfer_t *tp, kcondvar_t **waiter, int *saved_mode);
94 static void pcf8584_give_up(pcf8584_t *i2c, kcondvar_t *waiter, int saved_mode);
95 
96 static struct bus_ops pcf8584_busops = {
97 	BUSO_REV,
98 	nullbusmap,			/* bus_map */
99 	NULL,				/* bus_get_intrspec */
100 	NULL,				/* bus_add_intrspec */
101 	NULL,				/* bus_remove_intrspec */
102 	NULL,				/* bus_map_fault */
103 	ddi_no_dma_map,			/* bus_dma_map */
104 	ddi_no_dma_allochdl,		/* bus_dma_allochdl */
105 	ddi_no_dma_freehdl,		/* bus_dma_freehdl */
106 	ddi_no_dma_bindhdl,		/* bus_dma_bindhdl */
107 	ddi_no_dma_unbindhdl,		/* bus_unbindhdl */
108 	ddi_no_dma_flush,		/* bus_dma_flush */
109 	ddi_no_dma_win,			/* bus_dma_win */
110 	ddi_no_dma_mctl,		/* bus_dma_ctl */
111 	pcf8584_bus_ctl,		/* bus_ctl */
112 	ddi_bus_prop_op,		/* bus_prop_op */
113 	NULL,				/* bus_get_eventcookie */
114 	NULL,				/* bus_add_eventcall */
115 	NULL,				/* bus_remove_eventcall */
116 	NULL,				/* bus_post_event */
117 	0,				/* bus_intr_ctl */
118 	0,				/* bus_config		*/
119 	0,				/* bus_unconfig		*/
120 	0,				/* bus_fm_init		*/
121 	0,				/* bus_fm_fini		*/
122 	0,				/* bus_fm_access_enter	*/
123 	0,				/* bus_fm_access_exit	*/
124 	0,				/* bus_power		*/
125 	i_ddi_intr_ops			/* bus_intr_op		*/
126 };
127 
128 struct cb_ops pcf8584_cb_ops = {
129 	pcf8584_open,		/* open */
130 	pcf8584_close,	/* close */
131 	nodev,			/* strategy */
132 	nodev,			/* print */
133 	nodev,			/* dump */
134 	nodev,			/* read */
135 	nodev,			/* write */
136 	pcf8584_ioctl,		/* ioctl */
137 	nodev,			/* devmap */
138 	nodev,			/* mmap */
139 	nodev,			/* segmap */
140 	nochpoll,		/* poll */
141 	ddi_prop_op,		/* cb_prop_op */
142 	0,			/* streamtab  */
143 	D_MP | D_NEW		/* Driver compatibility flag */
144 };
145 
146 static struct dev_ops pcf8584_ops = {
147 	DEVO_REV,
148 	0,
149 	ddi_getinfo_1to1,
150 	nulldev,
151 	nulldev,
152 	pcf8584_attach,
153 	pcf8584_detach,
154 	nodev,
155 	&pcf8584_cb_ops,
156 	&pcf8584_busops
157 };
158 
159 static struct modldrv modldrv = {
160 	&mod_driverops, /* Type of module. This one is a driver */
161 	"I2C Nexus Driver %I%",	/* Name of the module. */
162 	&pcf8584_ops,		/* driver ops */
163 };
164 
165 static struct modlinkage modlinkage = {
166 	MODREV_1,
167 	&modldrv,
168 	NULL
169 };
170 
171 /*
172  * pcf8584 soft state
173  */
174 static void	*pcf8584_state;
175 
176 i2c_nexus_reg_t pcf8584_regvec = {
177 	I2C_NEXUS_REV,
178 	pcf8584_transfer,
179 };
180 
181 /*
182  * The "interrupt_priorities" property is how a driver can specify a SPARC
183  * PIL level to associate with each of its interrupt properties.  Most
184  * self-identifying busses have a better mechanism for managing this, but I2C
185  * doesn't.
186  */
187 int	pcf8584_pil = PCF8584_PIL;
188 
189 #ifdef DEBUG
190 int pcf8584_print_lvl = 0;
191 static kmutex_t msg_buf_lock;
192 static char msg_buff[1024];
193 #define	PCF8584_DDB(command)	\
194 	do {			\
195 		{ command; }	\
196 		_NOTE(CONSTANTCONDITION)	\
197 	} while (0)
198 
199 static void
200 pcf8584_print(int flags, const char *fmt, ...)
201 {
202 	if (flags & pcf8584_print_lvl) {
203 		va_list ap;
204 
205 		va_start(ap, fmt);
206 
207 		if (pcf8584_print_lvl & PRT_PROM) {
208 			prom_vprintf(fmt, ap);
209 		} else {
210 			mutex_enter(&msg_buf_lock);
211 			(void) vsprintf(msg_buff, fmt, ap);
212 			if (pcf8584_print_lvl & PRT_BUFFONLY) {
213 				cmn_err(CE_CONT, "?%s", msg_buff);
214 			} else {
215 				cmn_err(CE_CONT, "%s", msg_buff);
216 			}
217 			mutex_exit(&msg_buf_lock);
218 		}
219 		va_end(ap);
220 	}
221 }
222 #else
223 #define	PCF8584_DDB(command) \
224 	do {			\
225 		{ _NOTE(EMPTY); }	\
226 		_NOTE(CONSTANTCONDITION)	\
227 	} while (0)
228 #endif
229 
230 #define	PCF8584_IMPL_DELAY(type, delay)	\
231 	if (type == PIC16F747) {	\
232 		drv_usecwait(delay);	\
233 	}
234 
235 int
236 _init(void)
237 {
238 	int status;
239 
240 	status = ddi_soft_state_init(&pcf8584_state, sizeof (pcf8584_t),
241 		PCF8584_INITIAL_SOFT_SPACE);
242 	if (status != 0) {
243 
244 		return (status);
245 	}
246 
247 	if ((status = mod_install(&modlinkage)) != 0) {
248 		ddi_soft_state_fini(&pcf8584_state);
249 	}
250 
251 	return (status);
252 }
253 
254 int
255 _fini(void)
256 {
257 	int status;
258 
259 	if ((status = mod_remove(&modlinkage)) == 0) {
260 		ddi_soft_state_fini(&pcf8584_state);
261 	}
262 
263 	return (status);
264 }
265 
266 /*
267  * The loadable-module _info(9E) entry point
268  */
269 int
270 _info(struct modinfo *modinfop)
271 {
272 	return (mod_info(&modlinkage, modinfop));
273 }
274 
275 static void
276 pcf8584_dodetach(dev_info_t *dip)
277 {
278 	pcf8584_t *i2c;
279 	int instance = ddi_get_instance(dip);
280 
281 	i2c = (pcf8584_t *)ddi_get_soft_state(pcf8584_state, instance);
282 
283 	if ((i2c->pcf8584_attachflags & ADD_INTR) != 0) {
284 		ddi_remove_intr(dip, 0, i2c->pcf8584_icookie);
285 	}
286 
287 	cv_destroy(&i2c->pcf8584_cv);
288 
289 	if ((i2c->pcf8584_attachflags & IMUTEX) != 0) {
290 		mutex_destroy(&i2c->pcf8584_imutex);
291 		    cv_destroy(&i2c->pcf8584_icv);
292 	}
293 	if ((i2c->pcf8584_attachflags & SETUP_REGS) != 0) {
294 		pcf8584_free_regs(i2c);
295 	}
296 	if ((i2c->pcf8584_attachflags & NEXUS_REGISTER) != 0) {
297 		i2c_nexus_unregister(dip);
298 	}
299 	if ((i2c->pcf8584_attachflags & PROP_CREATE) != 0) {
300 		(void) ddi_prop_remove(DDI_DEV_T_NONE, dip,
301 			"interrupt-priorities");
302 	}
303 	if ((i2c->pcf8584_attachflags & MINOR_NODE) != 0) {
304 		ddi_remove_minor_node(dip, NULL);
305 	}
306 
307 	ddi_soft_state_free(pcf8584_state, instance);
308 }
309 
310 static int
311 pcf8584_doattach(dev_info_t *dip)
312 {
313 	pcf8584_t *i2c;
314 	int instance = ddi_get_instance(dip);
315 
316 	/*
317 	 * Allocate soft state structure.
318 	 */
319 	if (ddi_soft_state_zalloc(pcf8584_state, instance) != DDI_SUCCESS) {
320 
321 		return (DDI_FAILURE);
322 	}
323 
324 	i2c = (pcf8584_t *)ddi_get_soft_state(pcf8584_state, instance);
325 
326 	i2c->pcf8584_dip = dip;
327 
328 	(void) snprintf(i2c->pcf8584_name, sizeof (i2c->pcf8584_name),
329 		"%s_%d", ddi_node_name(dip), instance);
330 
331 	/*
332 	 * Identify which pcf8584 implementation is being attached to.
333 	 */
334 	if (strcmp(ddi_binding_name(i2c->pcf8584_dip), "SUNW,bbc-i2c") == 0) {
335 		i2c->pcf8584_impl_type = BBC;
336 		i2c->pcf8584_impl_delay = PCF8584_GENERIC_DELAY;
337 	} else if (strcmp(ddi_binding_name(i2c->pcf8584_dip),
338 	    "SUNW,i2c-pic16f747") == 0) {
339 		i2c->pcf8584_impl_type = PIC16F747;
340 		i2c->pcf8584_impl_delay = PCF8584_PIC16F747_DELAY;
341 	} else {
342 		i2c->pcf8584_impl_type = GENERIC;
343 		i2c->pcf8584_impl_delay = PCF8584_GENERIC_DELAY;
344 	}
345 
346 	if (ddi_prop_exists(DDI_DEV_T_ANY, dip,
347 	    DDI_PROP_NOTPROM | DDI_PROP_DONTPASS,
348 	    "interrupt-priorities") != 1) {
349 		(void) ddi_prop_create(DDI_DEV_T_NONE, dip,
350 			DDI_PROP_CANSLEEP, "interrupt-priorities",
351 			(caddr_t)&pcf8584_pil,
352 			sizeof (pcf8584_pil));
353 		i2c->pcf8584_attachflags |= PROP_CREATE;
354 	}
355 
356 	cv_init(&i2c->pcf8584_cv, NULL, CV_DRIVER, NULL);
357 
358 	if (pcf8584_setup_regs(dip, i2c) != DDI_SUCCESS) {
359 		goto bad;
360 	}
361 
362 	i2c->pcf8584_attachflags |= SETUP_REGS;
363 
364 	if (ddi_prop_exists(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS |
365 	    DDI_PROP_CANSLEEP, "poll-mode") == 1) {
366 		i2c->pcf8584_mode = PCF8584_POLL_MODE;
367 	} else {
368 
369 		if (ddi_get_iblock_cookie(dip, 0,
370 		    &i2c->pcf8584_icookie) == DDI_SUCCESS) {
371 			mutex_init(&i2c->pcf8584_imutex, NULL, MUTEX_DRIVER,
372 			    (void *)i2c->pcf8584_icookie);
373 			cv_init(&i2c->pcf8584_icv, NULL, CV_DRIVER, NULL);
374 			i2c->pcf8584_attachflags |= IMUTEX;
375 
376 			if (ddi_add_intr(dip, 0, NULL, NULL, pcf8584_intr,
377 			    (caddr_t)i2c) == DDI_SUCCESS) {
378 				i2c->pcf8584_attachflags |= ADD_INTR;
379 				i2c->pcf8584_mode = PCF8584_INTR_MODE;
380 			} else {
381 				cmn_err(CE_WARN, "%s failed to add interrupt",
382 				    i2c->pcf8584_name);
383 				i2c->pcf8584_mode = PCF8584_POLL_MODE;
384 			}
385 		} else {
386 			cmn_err(CE_WARN, "%s failed to retrieve iblock cookie. "
387 			    "Operating in POLL MODE only", i2c->pcf8584_name);
388 			i2c->pcf8584_mode = PCF8584_POLL_MODE;
389 		}
390 	}
391 
392 	/*
393 	 * For polled mode, still initialize a cv and mutex
394 	 */
395 	if ((i2c->pcf8584_attachflags & IMUTEX) == 0) {
396 		cv_init(&i2c->pcf8584_icv, NULL, CV_DRIVER, NULL);
397 		mutex_init(&i2c->pcf8584_imutex, NULL, MUTEX_DRIVER, NULL);
398 		i2c->pcf8584_attachflags |= IMUTEX;
399 	}
400 
401 	i2c_nexus_register(dip, &pcf8584_regvec);
402 	i2c->pcf8584_attachflags |= NEXUS_REGISTER;
403 
404 	if (ddi_create_minor_node(dip, "devctl", S_IFCHR, instance,
405 	    DDI_NT_NEXUS, 0) == DDI_FAILURE) {
406 		cmn_err(CE_WARN, "%s ddi_create_minor_node failed",
407 		    i2c->pcf8584_name);
408 		goto bad;
409 	}
410 
411 	i2c->pcf8584_attachflags |= MINOR_NODE;
412 
413 	pcf8584_init(i2c);
414 
415 	i2c->pcf8584_nexus_dip = dip;
416 
417 	return (DDI_SUCCESS);
418 
419 bad:
420 	pcf8584_dodetach(dip);
421 
422 	return (DDI_FAILURE);
423 }
424 
425 static int
426 pcf8584_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
427 {
428 	switch (cmd) {
429 	case DDI_ATTACH:
430 
431 		return (pcf8584_doattach(dip));
432 	case DDI_RESUME:
433 		pcf8584_resume(dip);
434 
435 		return (DDI_SUCCESS);
436 	default:
437 
438 		return (DDI_FAILURE);
439 	}
440 }
441 
442 static int
443 pcf8584_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
444 {
445 	switch (cmd) {
446 	case DDI_DETACH:
447 		pcf8584_dodetach(dip);
448 
449 		return (DDI_SUCCESS);
450 	case DDI_SUSPEND:
451 		pcf8584_suspend(dip);
452 
453 		return (DDI_SUCCESS);
454 	default:
455 
456 		return (DDI_FAILURE);
457 	}
458 }
459 
460 /*ARGSUSED*/
461 static int
462 pcf8584_open(dev_t  *devp,  int  flag,  int  otyp,  cred_t *cred_p)
463 {
464 	int instance;
465 	pcf8584_t *i2c;
466 
467 	/*
468 	 * Make sure the open is for the right file type
469 	 */
470 	if (otyp != OTYP_CHR)
471 		return (EINVAL);
472 
473 	instance = getminor(*devp);
474 	i2c = (pcf8584_t *)ddi_get_soft_state(pcf8584_state, instance);
475 	if (i2c == NULL)
476 		return (ENXIO);
477 
478 	/*
479 	 * Enforce exclusive access
480 	 */
481 	mutex_enter(&i2c->pcf8584_imutex);
482 	if (i2c->pcf8584_open) {
483 		mutex_exit(&i2c->pcf8584_imutex);
484 
485 		return (EBUSY);
486 	} else
487 		i2c->pcf8584_open = 1;
488 	mutex_exit(&i2c->pcf8584_imutex);
489 
490 	return (0);
491 }
492 
493 /*ARGSUSED*/
494 static int
495 pcf8584_close(dev_t  dev,  int  flag,  int  otyp,  cred_t *cred_p)
496 {
497 	int instance;
498 	pcf8584_t *i2c;
499 
500 	/*
501 	 * Make sure the close is for the right file type
502 	 */
503 	if (otyp != OTYP_CHR)
504 		return (EINVAL);
505 
506 	instance = getminor(dev);
507 	i2c = (pcf8584_t *)ddi_get_soft_state(pcf8584_state, instance);
508 	if (i2c == NULL)
509 		return (ENXIO);
510 
511 	mutex_enter(&i2c->pcf8584_imutex);
512 	i2c->pcf8584_open = 0;
513 	mutex_exit(&i2c->pcf8584_imutex);
514 
515 	return (0);
516 }
517 
518 /*ARGSUSED*/
519 static int
520 pcf8584_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp,
521 	int *rvalp)
522 {
523 	pcf8584_t *i2c;
524 	dev_info_t *self;
525 	struct devctl_iocdata *dcp;
526 	int rv;
527 
528 	i2c = (pcf8584_t *)ddi_get_soft_state(pcf8584_state, getminor(dev));
529 	if (i2c == NULL)
530 		return (ENXIO);
531 
532 	self = (dev_info_t *)i2c->pcf8584_nexus_dip;
533 
534 	/*
535 	 * read devctl ioctl data
536 	 */
537 	if (ndi_dc_allochdl((void *)arg, &dcp) != NDI_SUCCESS) {
538 
539 		return (EFAULT);
540 	}
541 
542 	switch (cmd) {
543 		case DEVCTL_BUS_DEV_CREATE:
544 			rv = ndi_dc_devi_create(dcp, self, 0, NULL);
545 			break;
546 		case DEVCTL_DEVICE_REMOVE:
547 			rv = ndi_devctl_device_remove(self, dcp, 0);
548 			break;
549 		default:
550 			rv = ENOTSUP;
551 	}
552 
553 	ndi_dc_freehdl(dcp);
554 
555 	return (rv);
556 }
557 
558 static int
559 pcf8584_bus_ctl(dev_info_t *dip, dev_info_t *rdip, ddi_ctl_enum_t op,
560     void *arg, void *result)
561 {
562 	switch (op) {
563 	case DDI_CTLOPS_INITCHILD:
564 
565 		return (pcf8584_initchild((dev_info_t *)arg));
566 	case DDI_CTLOPS_UNINITCHILD:
567 		pcf8584_uninitchild((dev_info_t *)arg);
568 
569 		return (DDI_SUCCESS);
570 	case DDI_CTLOPS_REPORTDEV:
571 		pcf8584_reportdev(dip, rdip);
572 
573 		return (DDI_SUCCESS);
574 	case DDI_CTLOPS_DMAPMAPC:
575 	case DDI_CTLOPS_POKE:
576 	case DDI_CTLOPS_PEEK:
577 	case DDI_CTLOPS_IOMIN:
578 	case DDI_CTLOPS_REPORTINT:
579 	case DDI_CTLOPS_SIDDEV:
580 	case DDI_CTLOPS_SLAVEONLY:
581 	case DDI_CTLOPS_AFFINITY:
582 	case DDI_CTLOPS_PTOB:
583 	case DDI_CTLOPS_BTOP:
584 	case DDI_CTLOPS_BTOPR:
585 	case DDI_CTLOPS_DVMAPAGESIZE:
586 
587 		return (DDI_FAILURE);
588 	default:
589 
590 		return (ddi_ctlops(dip, rdip, op, arg, result));
591 	}
592 }
593 
594 /*
595  * pcf8584_suspend() is called before the system suspends.  Existing
596  * transfer in progress or waiting will complete, but new transfers are
597  * effectively blocked by "acquiring" the bus.
598  */
599 static void
600 pcf8584_suspend(dev_info_t *dip)
601 {
602 	pcf8584_t *i2c;
603 	int instance;
604 
605 	instance = ddi_get_instance(dip);
606 	i2c = (pcf8584_t *)ddi_get_soft_state(pcf8584_state, instance);
607 
608 	pcf8584_acquire(i2c, NULL, NULL, B_FALSE);
609 }
610 
611 /*
612  * pcf8584_resume() is called when the system resumes from CPR.  It releases
613  * the hold that was placed on the i2c bus, which allows any real
614  * transfers to continue.
615  */
616 static void
617 pcf8584_resume(dev_info_t *dip)
618 {
619 	pcf8584_t *i2c;
620 	int instance;
621 
622 	instance = ddi_get_instance(dip);
623 	i2c = (pcf8584_t *)ddi_get_soft_state(pcf8584_state, instance);
624 
625 	pcf8584_release(i2c, B_FALSE);
626 
627 	pcf8584_init(i2c);
628 }
629 
630 /*
631  * pcf8584_acquire() is called by a thread wishing to "own" the I2C bus.
632  * It should not be held across multiple transfers. If the 'force' flag
633  * is set, do not try to acquire mutex or do cv_wait.
634  */
635 static void
636 pcf8584_acquire(pcf8584_t *i2c, dev_info_t *dip, i2c_transfer_t *tp,
637     boolean_t force)
638 {
639 	if (force) {
640 		i2c->pcf8584_busy = 1;
641 		i2c->pcf8584_cur_tran = tp;
642 		i2c->pcf8584_cur_dip = dip;
643 		i2c->pcf8584_cur_status = PCF8584_TRANSFER_NEW;
644 		return;
645 	}
646 
647 	mutex_enter(&i2c->pcf8584_imutex);
648 	while (i2c->pcf8584_busy) {
649 		cv_wait(&i2c->pcf8584_cv, &i2c->pcf8584_imutex);
650 	}
651 	i2c->pcf8584_busy = 1;
652 	mutex_exit(&i2c->pcf8584_imutex);
653 	/*
654 	 * On systems where OBP shares a pcf8584 controller with the
655 	 * OS, plat_shared_i2c_enter will serialize access to the
656 	 * pcf8584 controller.  Do not grab this lock during CPR
657 	 * suspend as the CPR thread also acquires this muxex
658 	 * through through prom_setprop which causes recursive
659 	 * mutex enter.
660 	 *
661 	 * dip == NULL during CPR.
662 	 */
663 	if ((&plat_shared_i2c_enter != NULL) && (dip != NULL)) {
664 		plat_shared_i2c_enter(i2c->pcf8584_dip);
665 	}
666 
667 	mutex_enter(&i2c->pcf8584_imutex);
668 	i2c->pcf8584_cur_tran = tp;
669 	i2c->pcf8584_cur_dip = dip;
670 	mutex_exit(&i2c->pcf8584_imutex);
671 }
672 
673 /*
674  * pcf8584_release() is called to release a hold made by pcf8584_acquire().
675  */
676 static void
677 pcf8584_release(pcf8584_t *i2c, boolean_t force)
678 {
679 	if (force) {
680 		i2c->pcf8584_busy = 0;
681 		i2c->pcf8584_cur_tran = NULL;
682 		i2c->pcf8584_cur_dip = NULL;
683 		i2c->pcf8584_cur_status = PCF8584_TRANSFER_OVER;
684 		cv_signal(&i2c->pcf8584_cv);
685 		return;
686 	}
687 
688 	mutex_enter(&i2c->pcf8584_imutex);
689 	i2c->pcf8584_busy = 0;
690 	i2c->pcf8584_cur_tran = NULL;
691 	cv_signal(&i2c->pcf8584_cv);
692 	mutex_exit(&i2c->pcf8584_imutex);
693 
694 	if ((&plat_shared_i2c_exit != NULL) && (i2c->pcf8584_cur_dip != NULL)) {
695 		plat_shared_i2c_exit(i2c->pcf8584_dip);
696 	}
697 }
698 
699 /*
700  * if pcf8584_b_reg exists, it means the current bus controller signals
701  * are multiplexed into more than a single bus.  Select the bus needed
702  * by writing to the mux register.
703  */
704 static void
705 pcf8584_select_bus(pcf8584_t *i2c)
706 {
707 	int bus;
708 	pcf8584_ppvt_t *ppvt;
709 
710 	/*
711 	 * The existence of pcf8584_b_reg means the bus registers
712 	 * are multiplexed.
713 	 */
714 
715 	PCF8584_DDB(pcf8584_print(PRT_SELECT, "bus multiplex: %X\n",
716 	    i2c->pcf8584_b_reg));
717 	if (i2c->pcf8584_b_reg != NULL) {
718 		ppvt = ddi_get_parent_data(i2c->pcf8584_cur_dip);
719 
720 		bus = ppvt->pcf8584_ppvt_bus;
721 
722 		PCF8584_DDB(pcf8584_print(PRT_SELECT,
723 		    "transmitting bus number %d\n", bus));
724 
725 		ddi_put8(i2c->pcf8584_b_rhandle, i2c->pcf8584_b_reg, bus);
726 	}
727 }
728 
729 /*
730  * pcf8584_type_to_state() converts a transfer type to the
731  * next state of the I2C state machine based on the requested
732  * transfer type.
733  */
734 static enum tran_state
735 pcf8584_type_to_state(int i2c_flags)
736 {
737 	switch (i2c_flags) {
738 	case I2C_WR:
739 
740 		return (TRAN_STATE_WR);
741 	case I2C_RD:
742 
743 		return (TRAN_STATE_DUMMY_RD);
744 	case I2C_WR_RD:
745 
746 		return (TRAN_STATE_WR_RD);
747 	}
748 	/*NOTREACHED*/
749 	return (TRAN_STATE_NULL);
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%p "
883 		    " addr = 0x%x", i2c->pcf8584_name,
884 		    (void *)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%p addr = 0x%x", i2c->pcf8584_name,
891 		    (void *)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