xref: /illumos-gate/usr/src/uts/sun4u/io/i2c/nexus/pcf8584.c (revision bdfc6d18da790deeec2e0eb09c625902defe2498)
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_INTR_HILEVEL:
587 	case DDI_CTLOPS_XLATE_INTRS:
588 	case DDI_CTLOPS_DVMAPAGESIZE:
589 
590 		return (DDI_FAILURE);
591 	default:
592 
593 		return (ddi_ctlops(dip, rdip, op, arg, result));
594 	}
595 }
596 
597 /*
598  * pcf8584_suspend() is called before the system suspends.  Existing
599  * transfer in progress or waiting will complete, but new transfers are
600  * effectively blocked by "acquiring" the bus.
601  */
602 static void
603 pcf8584_suspend(dev_info_t *dip)
604 {
605 	pcf8584_t *i2c;
606 	int instance;
607 
608 	instance = ddi_get_instance(dip);
609 	i2c = (pcf8584_t *)ddi_get_soft_state(pcf8584_state, instance);
610 
611 	pcf8584_acquire(i2c, NULL, NULL, B_FALSE);
612 }
613 
614 /*
615  * pcf8584_resume() is called when the system resumes from CPR.  It releases
616  * the hold that was placed on the i2c bus, which allows any real
617  * transfers to continue.
618  */
619 static void
620 pcf8584_resume(dev_info_t *dip)
621 {
622 	pcf8584_t *i2c;
623 	int instance;
624 
625 	instance = ddi_get_instance(dip);
626 	i2c = (pcf8584_t *)ddi_get_soft_state(pcf8584_state, instance);
627 
628 	pcf8584_release(i2c, B_FALSE);
629 
630 	pcf8584_init(i2c);
631 }
632 
633 /*
634  * pcf8584_acquire() is called by a thread wishing to "own" the I2C bus.
635  * It should not be held across multiple transfers. If the 'force' flag
636  * is set, do not try to acquire mutex or do cv_wait.
637  */
638 static void
639 pcf8584_acquire(pcf8584_t *i2c, dev_info_t *dip, i2c_transfer_t *tp,
640     boolean_t force)
641 {
642 	if (force) {
643 		i2c->pcf8584_busy = 1;
644 		i2c->pcf8584_cur_tran = tp;
645 		i2c->pcf8584_cur_dip = dip;
646 		i2c->pcf8584_cur_status = PCF8584_TRANSFER_NEW;
647 		return;
648 	}
649 
650 	mutex_enter(&i2c->pcf8584_imutex);
651 	while (i2c->pcf8584_busy) {
652 		cv_wait(&i2c->pcf8584_cv, &i2c->pcf8584_imutex);
653 	}
654 	i2c->pcf8584_busy = 1;
655 	mutex_exit(&i2c->pcf8584_imutex);
656 	/*
657 	 * On systems where OBP shares a pcf8584 controller with the
658 	 * OS, plat_shared_i2c_enter will serialize access to the
659 	 * pcf8584 controller.  Do not grab this lock during CPR
660 	 * suspend as the CPR thread also acquires this muxex
661 	 * through through prom_setprop which causes recursive
662 	 * mutex enter.
663 	 *
664 	 * dip == NULL during CPR.
665 	 */
666 	if ((&plat_shared_i2c_enter != NULL) && (dip != NULL)) {
667 		plat_shared_i2c_enter(i2c->pcf8584_dip);
668 	}
669 
670 	mutex_enter(&i2c->pcf8584_imutex);
671 	i2c->pcf8584_cur_tran = tp;
672 	i2c->pcf8584_cur_dip = dip;
673 	mutex_exit(&i2c->pcf8584_imutex);
674 }
675 
676 /*
677  * pcf8584_release() is called to release a hold made by pcf8584_acquire().
678  */
679 static void
680 pcf8584_release(pcf8584_t *i2c, boolean_t force)
681 {
682 	if (force) {
683 		i2c->pcf8584_busy = 0;
684 		i2c->pcf8584_cur_tran = NULL;
685 		i2c->pcf8584_cur_dip = NULL;
686 		i2c->pcf8584_cur_status = PCF8584_TRANSFER_OVER;
687 		cv_signal(&i2c->pcf8584_cv);
688 		return;
689 	}
690 
691 	mutex_enter(&i2c->pcf8584_imutex);
692 	i2c->pcf8584_busy = 0;
693 	i2c->pcf8584_cur_tran = NULL;
694 	cv_signal(&i2c->pcf8584_cv);
695 	mutex_exit(&i2c->pcf8584_imutex);
696 
697 	if ((&plat_shared_i2c_exit != NULL) && (i2c->pcf8584_cur_dip != NULL)) {
698 		plat_shared_i2c_exit(i2c->pcf8584_dip);
699 	}
700 }
701 
702 /*
703  * if pcf8584_b_reg exists, it means the current bus controller signals
704  * are multiplexed into more than a single bus.  Select the bus needed
705  * by writing to the mux register.
706  */
707 static void
708 pcf8584_select_bus(pcf8584_t *i2c)
709 {
710 	int bus;
711 	pcf8584_ppvt_t *ppvt;
712 
713 	/*
714 	 * The existence of pcf8584_b_reg means the bus registers
715 	 * are multiplexed.
716 	 */
717 
718 	PCF8584_DDB(pcf8584_print(PRT_SELECT, "bus multiplex: %X\n",
719 	    i2c->pcf8584_b_reg));
720 	if (i2c->pcf8584_b_reg != NULL) {
721 		ppvt = ddi_get_parent_data(i2c->pcf8584_cur_dip);
722 
723 		bus = ppvt->pcf8584_ppvt_bus;
724 
725 		PCF8584_DDB(pcf8584_print(PRT_SELECT,
726 		    "transmitting bus number %d\n", bus));
727 
728 		ddi_put8(i2c->pcf8584_b_rhandle, i2c->pcf8584_b_reg, bus);
729 	}
730 }
731 
732 /*
733  * pcf8584_type_to_state() converts a transfer type to the
734  * next state of the I2C state machine based on the requested
735  * transfer type.
736  */
737 static enum tran_state
738 pcf8584_type_to_state(int i2c_flags)
739 {
740 	switch (i2c_flags) {
741 	case I2C_WR:
742 
743 		return (TRAN_STATE_WR);
744 	case I2C_RD:
745 
746 		return (TRAN_STATE_DUMMY_RD);
747 	case I2C_WR_RD:
748 
749 		return (TRAN_STATE_WR_RD);
750 	}
751 /*NOTREACHED*/
752 }
753 
754 /*
755  * pcf8584_put_s1() writes out cmd to register S1.
756  */
757 static void
758 pcf8584_put_s1(pcf8584_t *i2c, char cmd)
759 {
760 	ddi_acc_handle_t hp = i2c->pcf8584_rhandle;
761 	pcf8584_regs_t *rp = &i2c->pcf8584_regs;
762 
763 	ddi_put8(hp, rp->pcf8584_regs_s1, cmd);
764 	PCF8584_IMPL_DELAY(i2c->pcf8584_impl_type,
765 	    i2c->pcf8584_impl_delay);
766 	/*
767 	 * read status to make sure write is flushed
768 	 */
769 	(void) ddi_get8(hp, rp->pcf8584_regs_s1);
770 	PCF8584_IMPL_DELAY(i2c->pcf8584_impl_type,
771 	    i2c->pcf8584_impl_delay);
772 }
773 
774 /*
775  * pcf8584_put_s0() writes out data to register S0.
776  */
777 static void
778 pcf8584_put_s0(pcf8584_t *i2c, char data)
779 {
780 	ddi_acc_handle_t hp = i2c->pcf8584_rhandle;
781 	pcf8584_regs_t *rp = &i2c->pcf8584_regs;
782 
783 	ddi_put8(hp, rp->pcf8584_regs_s0, data);
784 	PCF8584_IMPL_DELAY(i2c->pcf8584_impl_type,
785 	    i2c->pcf8584_impl_delay);
786 	/*
787 	 * read status to make sure write is flushed
788 	 */
789 	(void) ddi_get8(hp, rp->pcf8584_regs_s1);
790 	PCF8584_IMPL_DELAY(i2c->pcf8584_impl_type,
791 	    i2c->pcf8584_impl_delay);
792 }
793 
794 /*
795  * pcf8584_get_s0() reads from register S0.
796  */
797 static uint8_t
798 pcf8584_get_s0(pcf8584_t *i2c)
799 {
800 	ddi_acc_handle_t hp = i2c->pcf8584_rhandle;
801 	pcf8584_regs_t *rp = &i2c->pcf8584_regs;
802 	uint8_t s0;
803 
804 	s0 = ddi_get8(hp, rp->pcf8584_regs_s0);
805 	PCF8584_IMPL_DELAY(i2c->pcf8584_impl_type,
806 	    i2c->pcf8584_impl_delay);
807 
808 	return (s0);
809 }
810 
811 /*
812  * pcf8584_get_s1() reads from register S1.
813  */
814 static uint8_t
815 pcf8584_get_s1(pcf8584_t *i2c)
816 {
817 	ddi_acc_handle_t hp = i2c->pcf8584_rhandle;
818 	pcf8584_regs_t *rp = &i2c->pcf8584_regs;
819 	uint8_t s1;
820 
821 	s1 = ddi_get8(hp, rp->pcf8584_regs_s1);
822 	PCF8584_IMPL_DELAY(i2c->pcf8584_impl_type,
823 	    i2c->pcf8584_impl_delay);
824 
825 	return (s1);
826 }
827 
828 /*
829  * If the previous transaction was a write, the stop
830  * bit may not make it out on the wire before
831  * the next transaction startes.  And unfortunately, there
832  * is no interrupt after the stop bit is written, so this
833  * function will poll to make sure the BBC is ready.
834  */
835 static int
836 pcf8584_bbn_ready(pcf8584_t *i2c)
837 {
838 	uint8_t s1;
839 	int usecwaits = 0;
840 
841 	s1 = pcf8584_get_s1(i2c);
842 
843 	while ((s1 & S1_BBN) == 0) {
844 
845 		if (usecwaits++ == 100) {
846 			/* Try initializing the bus */
847 			pcf8584_monitor_mode(i2c);
848 			pcf8584_put_s1(i2c, S1_STOP);
849 			delay(1);
850 			pcf8584_init(i2c);
851 			(void) pcf8584_get_s0(i2c);
852 			s1 = pcf8584_get_s1(i2c);
853 			if (s1 & S1_BBN) {
854 				cmn_err(CE_WARN,
855 				    "!%s: cleared bus busy.   addr=0x%x",
856 				    i2c->pcf8584_name,
857 				    pcf8584_dip_to_addr(i2c->pcf8584_cur_dip));
858 
859 				return (I2C_SUCCESS);
860 			} else {
861 				cmn_err(CE_WARN,
862 				    "!%s bus busy after init addr=0x%x",
863 				    i2c->pcf8584_name,
864 				    pcf8584_dip_to_addr(i2c->pcf8584_cur_dip));
865 
866 				return (I2C_FAILURE);
867 			}
868 		}
869 		drv_usecwait(1);
870 		s1 = pcf8584_get_s1(i2c);
871 	}
872 
873 	return (I2C_SUCCESS);
874 }
875 
876 static int
877 pcf8584_error(int status, uint8_t rdwr, pcf8584_t *i2c)
878 {
879 	int addr = pcf8584_dip_to_addr(i2c->pcf8584_cur_dip);
880 	pcf8584_regs_t *rp = &i2c->pcf8584_regs;
881 
882 	if (status & S1_BER) {
883 		cmn_err(CE_WARN,
884 		    "!%s bus error; Controller = 0x%x "
885 		    " addr = 0x%x", i2c->pcf8584_name,
886 		    (unsigned int)rp->pcf8584_regs_s1, addr);
887 		pcf8584_init(i2c);
888 
889 		return (I2C_FAILURE);
890 	} else if (status & S1_LAB) {
891 		cmn_err(CE_WARN, "!%s lost arbitration; Controller ="
892 		    " 0x%x addr = 0x%x", i2c->pcf8584_name,
893 		    (unsigned int)rp->pcf8584_regs_s1, addr);
894 		pcf8584_init(i2c);
895 
896 		return (I2C_FAILURE);
897 	} else if ((status & S1_LRB) && (rdwr == I2C_WR)) {
898 		/*
899 		 * No error logged here, because this may be benign.
900 		 * Cf. the "Alert Response Address" feature of SMBUS.
901 		 */
902 		pcf8584_put_s1(i2c, S1_STOP);
903 
904 		return (I2C_FAILURE);
905 	}
906 
907 	return (I2C_SUCCESS);
908 }
909 
910 static void
911 pcf8584_monitor_mode(pcf8584_t *i2c)
912 {
913 	pcf8584_put_s1(i2c, S1_PIN);
914 
915 	pcf8584_put_s0(i2c, MONITOR_ADDRESS);
916 }
917 
918 static int
919 pcf8584_initchild(dev_info_t *cdip)
920 {
921 	int32_t cell_size;
922 	int len;
923 	int32_t regs[2];
924 	int err;
925 	pcf8584_ppvt_t *ppvt;
926 	char name[30];
927 
928 	PCF8584_DDB(pcf8584_print(PRT_INIT, "pcf8584_initchild enter: %s\n",
929 		ddi_node_name(cdip)));
930 
931 	ppvt = kmem_alloc(sizeof (pcf8584_ppvt_t), KM_SLEEP);
932 
933 	len = sizeof (cell_size);
934 	err = ddi_getlongprop_buf(DDI_DEV_T_ANY, cdip,
935 		DDI_PROP_CANSLEEP, "#address-cells",
936 		(caddr_t)&cell_size, &len);
937 	if (err != DDI_PROP_SUCCESS || len != sizeof (cell_size)) {
938 
939 		return (DDI_FAILURE);
940 	}
941 
942 	len = sizeof (regs);
943 	err = ddi_getlongprop_buf(DDI_DEV_T_ANY, cdip,
944 		DDI_PROP_DONTPASS | DDI_PROP_CANSLEEP,
945 		"reg", (caddr_t)regs, &len);
946 	if (err != DDI_PROP_SUCCESS ||
947 		len != (cell_size * sizeof (int32_t))) {
948 
949 		return (DDI_FAILURE);
950 	}
951 
952 	if (cell_size == 1) {
953 		ppvt->pcf8584_ppvt_addr = regs[0];
954 		(void) sprintf(name, "%x", regs[0]);
955 	} else if (cell_size == 2) {
956 		ppvt->pcf8584_ppvt_bus = regs[0];
957 		ppvt->pcf8584_ppvt_addr = regs[1];
958 		(void) sprintf(name, "%x,%x", regs[0], regs[1]);
959 	} else {
960 
961 		return (DDI_FAILURE);
962 	}
963 
964 	ddi_set_parent_data(cdip, ppvt);
965 
966 	ddi_set_name_addr(cdip, name);
967 
968 	PCF8584_DDB(pcf8584_print(PRT_INIT,
969 	    "pcf8584_initchild SUCCESS: %s\n", ddi_node_name(cdip)));
970 
971 	return (DDI_SUCCESS);
972 }
973 
974 static void
975 pcf8584_uninitchild(dev_info_t *cdip)
976 {
977 	pcf8584_ppvt_t *ppvt;
978 
979 	ppvt = ddi_get_parent_data(cdip);
980 	kmem_free(ppvt, sizeof (pcf8584_ppvt_t));
981 
982 	ddi_set_parent_data(cdip, NULL);
983 	ddi_set_name_addr(cdip, NULL);
984 
985 	PCF8584_DDB(pcf8584_print(PRT_INIT, "i2c_uninitchild: %s\n",
986 	    ddi_node_name(cdip)));
987 }
988 
989 static void
990 pcf8584_init(pcf8584_t *i2c)
991 {
992 	uint8_t clk_div = 0x1C;
993 
994 	pcf8584_put_s1(i2c, S1_PIN);
995 
996 	pcf8584_put_s0(i2c, S0_OWN);
997 
998 	pcf8584_put_s1(i2c, S1_PIN | S1_ES1);
999 
1000 	/*
1001 	 * The default case is to set the clock divisor to the least common
1002 	 * denominator to avoid over clocking the I2C bus.  Assume that
1003 	 * BBC based systems are using the Safari clock as input, so select
1004 	 * the clk divisor based on it.
1005 	 */
1006 	if (i2c->pcf8584_impl_type == BBC) {
1007 		dev_info_t *root_node;
1008 		int clock_freq;
1009 		root_node = ddi_root_node();
1010 		clock_freq = ddi_prop_get_int(DDI_DEV_T_ANY, root_node,
1011 		    DDI_PROP_DONTPASS, "clock-frequency", 0);
1012 
1013 		if (clock_freq < 105000000) {
1014 			clk_div = 0x00;
1015 		} else if (clock_freq < 160000000) {
1016 			clk_div = 0x10;
1017 		} else {
1018 			clk_div = 0x1C;
1019 		}
1020 	}
1021 
1022 	/* set I2C clock speed */
1023 	pcf8584_put_s0(i2c, clk_div);
1024 
1025 	pcf8584_put_s1(i2c, S1_PIN | S1_ESO | S1_ACK);
1026 
1027 	/*
1028 	 * Multi-Master: Wait for a period of time equal to the
1029 	 * longest I2C message.  This accounts for the case
1030 	 * where multiple controllers and, if this particular one
1031 	 * is "lagging", misses the BB(bus busy) condition.
1032 	 * We wait 200 ms since the longest transaction at this time
1033 	 * on the i2c bus is a 256 byte read from the seprom which takes
1034 	 * about 75 ms. Some additional buffer does no harm to the driver.
1035 	 */
1036 
1037 	delay(drv_usectohz(PCF8584_INIT_WAIT));
1038 }
1039 
1040 /*
1041  * pcf8584_setup_regs() is called to map in registers specific to
1042  * the pcf8584.
1043  */
1044 static int
1045 pcf8584_setup_regs(dev_info_t *dip, pcf8584_t *i2c)
1046 {
1047 	int nregs;
1048 	ddi_device_acc_attr_t attr;
1049 	caddr_t reg_base;
1050 
1051 	attr.devacc_attr_version = DDI_DEVICE_ATTR_V0;
1052 	attr.devacc_attr_endian_flags = DDI_STRUCTURE_LE_ACC;
1053 	attr.devacc_attr_dataorder = DDI_STRICTORDER_ACC;
1054 
1055 	if (ddi_dev_nregs(dip, &nregs) != DDI_SUCCESS) {
1056 
1057 		return (DDI_FAILURE);
1058 	}
1059 
1060 	if (ddi_regs_map_setup(dip, 0,
1061 	    (caddr_t *)&reg_base, 0, 0, &attr,
1062 	    &i2c->pcf8584_rhandle) != DDI_SUCCESS) {
1063 
1064 		return (DDI_FAILURE);
1065 	}
1066 
1067 	/*
1068 	 * If i2c controller is on BBC, then s1 comes before s0.
1069 	 */
1070 	if (i2c->pcf8584_impl_type == BBC) {
1071 		i2c->pcf8584_regs.pcf8584_regs_s0 =
1072 		    (uint8_t *)&reg_base[1];
1073 		i2c->pcf8584_regs.pcf8584_regs_s1 =
1074 		    (uint8_t *)&reg_base[0];
1075 	} else {
1076 		i2c->pcf8584_regs.pcf8584_regs_s0 =
1077 		    (uint8_t *)&reg_base[0];
1078 		i2c->pcf8584_regs.pcf8584_regs_s1 =
1079 		    (uint8_t *)&reg_base[1];
1080 	}
1081 
1082 	if (nregs > 1) {
1083 		if (ddi_regs_map_setup(dip,
1084 			1, (caddr_t *)&i2c->pcf8584_b_reg,
1085 			0, 0, &attr, &i2c->pcf8584_b_rhandle) !=
1086 			DDI_SUCCESS) {
1087 
1088 			return (DDI_FAILURE);
1089 		}
1090 	}
1091 
1092 	return (DDI_SUCCESS);
1093 }
1094 
1095 /*
1096  * pcf8584_free_regs() frees any registers previously
1097  * allocated.
1098  */
1099 static void
1100 pcf8584_free_regs(pcf8584_t *i2c)
1101 {
1102 	if (i2c->pcf8584_regs.pcf8584_regs_s0 != NULL) {
1103 		ddi_regs_map_free(&i2c->pcf8584_rhandle);
1104 	}
1105 	if (i2c->pcf8584_b_reg != NULL) {
1106 		ddi_regs_map_free(&i2c->pcf8584_b_rhandle);
1107 	}
1108 }
1109 
1110 static void
1111 pcf8584_reportdev(dev_info_t *dip, dev_info_t *rdip)
1112 {
1113 	pcf8584_ppvt_t *ppvt;
1114 
1115 	ppvt = ddi_get_parent_data(rdip);
1116 
1117 	cmn_err(CE_CONT, "?%s%d at %s%d: addr 0x%x",
1118 	    ddi_driver_name(rdip), ddi_get_instance(rdip),
1119 	    ddi_driver_name(dip), ddi_get_instance(dip),
1120 	    ppvt->pcf8584_ppvt_addr);
1121 }
1122 
1123 /*
1124  * i2_nexus_dip_to_addr() takes a dip and returns an I2C address.
1125  */
1126 static int
1127 pcf8584_dip_to_addr(dev_info_t *dip)
1128 {
1129 	pcf8584_ppvt_t *ppvt;
1130 
1131 	ppvt = ddi_get_parent_data(dip);
1132 
1133 	return (ppvt->pcf8584_ppvt_addr);
1134 }
1135 
1136 /*
1137  * pcf8584_intr() is the interrupt service routine registered during
1138  * attach, and remains registered even if the driver is in POLLED mode.  So if
1139  * this is called from POLLED mode, it needs to return without doing
1140  * any work to prevent the I2C bus from entering an unknown state.
1141  */
1142 static uint_t
1143 pcf8584_intr(caddr_t arg)
1144 {
1145 	pcf8584_t *i2c = (pcf8584_t *)arg;
1146 	uint8_t s1;
1147 
1148 	ASSERT(i2c->pcf8584_mode != PCF8584_POLL_MODE);
1149 	PCF8584_DDB(pcf8584_print(PRT_INTR, "pcf8584_intr: enter\n"));
1150 
1151 	mutex_enter(&i2c->pcf8584_imutex);
1152 
1153 	/*
1154 	 * It is necessary to check both whether the hardware is interrupting
1155 	 * and that there is a current transaction for the bus in progress.
1156 	 * Checking just one but not the other will lead to a panic on xcal
1157 	 * since both controllers share the same ino, and also because OBP
1158 	 * shares a controller with the kernel even while the kernel is running.
1159 	 */
1160 
1161 	if (i2c->pcf8584_cur_tran == NULL) {
1162 		mutex_exit(&i2c->pcf8584_imutex);
1163 
1164 		return (DDI_INTR_UNCLAIMED);
1165 	}
1166 
1167 
1168 	s1 = pcf8584_get_s1(i2c);
1169 	if (s1 & S1_PIN) {
1170 		mutex_exit(&i2c->pcf8584_imutex);
1171 
1172 		return (DDI_INTR_UNCLAIMED);
1173 	}
1174 
1175 	if (pcf8584_process(i2c, s1) == I2C_COMPLETE) {
1176 		i2c->pcf8584_tran_state = TRAN_STATE_NULL;
1177 		i2c->pcf8584_cur_status = PCF8584_TRANSFER_OVER;
1178 		cv_signal(&i2c->pcf8584_icv);
1179 	} else
1180 		i2c->pcf8584_cur_status = PCF8584_TRANSFER_ON;
1181 
1182 	mutex_exit(&i2c->pcf8584_imutex);
1183 
1184 	return (DDI_INTR_CLAIMED);
1185 }
1186 
1187 /*
1188  * Interrupt occurs after a byte is transmitted or received, indicating
1189  * the device is ready to be serviced.
1190  */
1191 static int
1192 pcf8584_process(pcf8584_t *i2c, uint8_t s1)
1193 {
1194 	i2c_transfer_t *tp = i2c->pcf8584_cur_tran;
1195 	int addr = pcf8584_dip_to_addr(i2c->pcf8584_cur_dip);
1196 	int dummy_read;
1197 
1198 	ASSERT(i2c->pcf8584_tran_state != TRAN_STATE_NULL);
1199 
1200 	switch (i2c->pcf8584_tran_state) {
1201 	case TRAN_STATE_DUMMY_DATA:
1202 		PCF8584_DDB(pcf8584_print(PRT_TRAN,
1203 		    "TRAN_STATE_DUMMY DATA: write dummy %x\n", DUMMY_DATA));
1204 		if (pcf8584_error(s1, I2C_RD, i2c) != I2C_SUCCESS) {
1205 			tp->i2c_result = I2C_FAILURE;
1206 
1207 			return (I2C_COMPLETE);
1208 		}
1209 		i2c->pcf8584_tran_state = TRAN_STATE_START;
1210 		pcf8584_put_s0(i2c, DUMMY_DATA);
1211 
1212 		return (I2C_PENDING);
1213 	case TRAN_STATE_START:
1214 		if (pcf8584_error(s1, I2C_RD, i2c) != I2C_SUCCESS) {
1215 			PCF8584_DDB(pcf8584_print(PRT_TRAN,
1216 			    "TRAN_STATE_START failure\n"));
1217 			tp->i2c_result = I2C_FAILURE;
1218 
1219 			return (I2C_COMPLETE);
1220 		}
1221 		i2c->pcf8584_tran_state =
1222 			pcf8584_type_to_state(tp->i2c_flags);
1223 
1224 		/* Set read bit if this is a read transaction */
1225 		if (tp->i2c_flags == I2C_RD) {
1226 			addr |= I2C_READ;
1227 		}
1228 		if (i2c->pcf8584_mode == PCF8584_POLL_MODE)
1229 			pcf8584_put_s1(i2c, S1_START2);
1230 		else
1231 			pcf8584_put_s1(i2c, S1_START2 | S1_ENI);
1232 		pcf8584_put_s0(i2c, addr);
1233 		PCF8584_DDB(pcf8584_print(PRT_TRAN,
1234 		    "TRAN_STATE_START: write addr: %x\n", addr));
1235 
1236 		return (I2C_PENDING);
1237 	case TRAN_STATE_WR:
1238 
1239 		if (pcf8584_error(s1, I2C_WR, i2c) != I2C_SUCCESS) {
1240 			PCF8584_DDB(pcf8584_print(PRT_TRAN,
1241 			    "TRAN_STATE_WR failure\n"));
1242 			tp->i2c_result = I2C_FAILURE;
1243 
1244 			return (I2C_COMPLETE);
1245 		}
1246 		/* check to see if at end of buffer */
1247 		if (tp->i2c_w_resid == 0) {
1248 			pcf8584_put_s1(i2c, S1_STOP);
1249 			PCF8584_DDB(pcf8584_print(PRT_TRAN,
1250 			    "TRAN_STATE_WR: write STOP\n"));
1251 
1252 			return (I2C_COMPLETE);
1253 		}
1254 
1255 		pcf8584_put_s0(i2c, tp->i2c_wbuf[tp->i2c_wlen -
1256 		    tp->i2c_w_resid--]);
1257 		PCF8584_DDB(pcf8584_print(PRT_TRAN,
1258 		    "TRAN_STATE_WR:  write data %x\n",
1259 		    tp->i2c_wbuf[tp->i2c_wlen - (tp->i2c_w_resid + 1)]));
1260 
1261 		return (I2C_PENDING);
1262 	case TRAN_STATE_DUMMY_RD:
1263 
1264 		if (pcf8584_error(s1, I2C_WR, i2c) != I2C_SUCCESS) {
1265 			tp->i2c_result = I2C_FAILURE;
1266 
1267 			return (I2C_COMPLETE);
1268 		}
1269 		/*
1270 		 * The first read is always a dummy read, because reading S0
1271 		 * is what starts bit shifting and ACK on the I2c bus.
1272 		 * This byte is accessed during the next read, which starts
1273 		 * another 8 bit bus shift.
1274 		 *
1275 		 * special case for 1 byte reads:  Clear the ACK bit
1276 		 * here since this read causes the last and only byte
1277 		 * to be sent on the I2C bus.
1278 		 */
1279 		if (tp->i2c_r_resid  == 1) {
1280 			if (i2c->pcf8584_mode == PCF8584_POLL_MODE)
1281 				pcf8584_put_s1(i2c, S1_ESO);
1282 			else
1283 				pcf8584_put_s1(i2c, S1_ESO | S1_ENI);
1284 		}
1285 
1286 		/*
1287 		 * dummy read
1288 		 */
1289 		dummy_read = pcf8584_get_s0(i2c);
1290 
1291 		i2c->pcf8584_tran_state = TRAN_STATE_RD;
1292 		PCF8584_DDB(pcf8584_print(PRT_TRAN,
1293 		    "TRAN_STATE_DUMMY_RD: read dummy %d\n", dummy_read));
1294 
1295 		return (I2C_PENDING);
1296 	case TRAN_STATE_RD:
1297 		if (pcf8584_error(s1, I2C_RD, i2c) != I2C_SUCCESS) {
1298 			tp->i2c_result = I2C_FAILURE;
1299 			PCF8584_DDB(pcf8584_print(PRT_TRAN,
1300 			    "TRAN_STATE_RD failure\n"));
1301 
1302 			return (I2C_COMPLETE);
1303 		}
1304 
1305 		/*
1306 		 * If resid == 1, the last byte has already been shifted into
1307 		 * the accumulator.  Send the stop bit.  This also prevents the
1308 		 * last S0 read from shifting in another byte from the I2C bus.
1309 		 */
1310 		if (tp->i2c_r_resid  == 1) {
1311 			pcf8584_put_s1(i2c, S1_STOP);
1312 		}
1313 
1314 		/*
1315 		 * If resid == 2, then the next read will cause the I2C bus to
1316 		 * start shifting in the last byte on the I2C bus, which we
1317 		 * don't want to be ACK'd, so clear the ACK bit.
1318 		 */
1319 		if (tp->i2c_r_resid  == 2) {
1320 			if (i2c->pcf8584_mode == PCF8584_POLL_MODE)
1321 				pcf8584_put_s1(i2c, S1_ESO);
1322 			else
1323 				pcf8584_put_s1(i2c, S1_ESO | S1_ENI);
1324 		}
1325 
1326 		tp->i2c_rbuf[tp->i2c_rlen - tp->i2c_r_resid] =
1327 			pcf8584_get_s0(i2c);
1328 
1329 		PCF8584_DDB(pcf8584_print(PRT_TRAN,
1330 		    "TRAN_STATE_RD: returning. i2c_rlen = %d "
1331 		    "i2c_r_resid = %d,  data =%x\n", tp->i2c_rlen,
1332 		    tp->i2c_r_resid, tp->i2c_rbuf[tp->i2c_rlen -
1333 		    tp->i2c_r_resid]));
1334 
1335 		if (--tp->i2c_r_resid == 0) {
1336 
1337 			return (I2C_COMPLETE);
1338 		}
1339 
1340 		return (I2C_PENDING);
1341 	case TRAN_STATE_WR_RD:
1342 
1343 		if (pcf8584_error(s1, I2C_WR, i2c) != I2C_SUCCESS) {
1344 			tp->i2c_result = I2C_FAILURE;
1345 
1346 			return (I2C_COMPLETE);
1347 		}
1348 		if ((s1 & S1_LRB)) {
1349 			pcf8584_put_s1(i2c, S1_STOP);
1350 			PCF8584_DDB(pcf8584_print(PRT_TRAN,
1351 			    "TRAN_STATE_WR_RD sending STOP\n"));
1352 
1353 			return (I2C_COMPLETE);
1354 		}
1355 		if (tp->i2c_w_resid != 0) {
1356 			pcf8584_put_s0(i2c, tp->i2c_wbuf[tp->i2c_wlen -
1357 				tp->i2c_w_resid--]);
1358 			PCF8584_DDB(pcf8584_print(PRT_TRAN,
1359 			    "TRAN_STATE_WR_RD: write data %x\n",
1360 			    tp->i2c_wbuf[tp->i2c_wlen -
1361 			    (tp->i2c_w_resid + 1)]));
1362 		} else {
1363 			if (i2c->pcf8584_mode == PCF8584_POLL_MODE)
1364 				pcf8584_put_s1(i2c, S1_START2);
1365 			else
1366 				pcf8584_put_s1(i2c, S1_START2 | S1_ENI);
1367 			pcf8584_put_s0(i2c, addr | I2C_READ);
1368 			i2c->pcf8584_tran_state =
1369 				TRAN_STATE_DUMMY_RD;
1370 			PCF8584_DDB(pcf8584_print(PRT_TRAN,
1371 			    "TRAN_STATE_WR_RD: write addr "
1372 			    "%x\n", addr | I2C_READ));
1373 		}
1374 
1375 		return (I2C_PENDING);
1376 	default:
1377 
1378 		return (I2C_COMPLETE);
1379 	}
1380 }
1381 
1382 /*
1383  * pcf8584_transfer() is the function that is registered with
1384  * I2C services to be called from pcf8584_transfer() for each transfer.
1385  *
1386  * This function starts the transfer, and then waits for the
1387  * interrupt or polled thread to signal that the transfer has
1388  * completed.
1389  */
1390 int
1391 pcf8584_transfer(dev_info_t *dip, i2c_transfer_t *tp)
1392 {
1393 	pcf8584_t *i2c;
1394 	int saved_mode, took_over = 0;
1395 	kcondvar_t *waiter = NULL;
1396 	extern int do_polled_io;
1397 
1398 	i2c = (pcf8584_t *)ddi_get_soft_state(pcf8584_state,
1399 		ddi_get_instance(ddi_get_parent(dip)));
1400 
1401 	tp->i2c_r_resid = tp->i2c_rlen;
1402 	tp->i2c_w_resid = tp->i2c_wlen;
1403 	tp->i2c_result = I2C_SUCCESS;
1404 
1405 begin:
1406 	/*
1407 	 * If we're explicitly asked to do polled io (or if we are panic'ing),
1408 	 * we need to usurp ownership of the I2C bus, bypassing any other
1409 	 * waiters.
1410 	 */
1411 	if (do_polled_io || ddi_in_panic()) {
1412 		pcf8584_take_over(i2c, dip, tp, &waiter, &saved_mode);
1413 		took_over = 1;
1414 	} else {
1415 		pcf8584_acquire(i2c, dip, tp, B_FALSE);
1416 		mutex_enter(&i2c->pcf8584_imutex);
1417 
1418 		/*
1419 		 * See if someone else had intruded and taken over the bus
1420 		 * between the 'pcf8584_acquire' and 'mutex_enter' above.
1421 		 * If so, we'll have to start all over again.
1422 		 */
1423 		if (i2c->pcf8584_cur_tran != tp) {
1424 			mutex_exit(&i2c->pcf8584_imutex);
1425 			goto begin;
1426 		}
1427 	}
1428 
1429 	if (pcf8584_bbn_ready(i2c) != I2C_SUCCESS) {
1430 		if (took_over)
1431 			pcf8584_give_up(i2c, waiter, saved_mode);
1432 		else {
1433 			mutex_exit(&i2c->pcf8584_imutex);
1434 			pcf8584_release(i2c, B_FALSE);
1435 		}
1436 
1437 		return (tp->i2c_result = I2C_FAILURE);
1438 	}
1439 
1440 	/*
1441 	 * Bus selection must be followed by pcf8584_bbn_ready(),
1442 	 * otherwise the bus can be switched before the stop
1443 	 * bit is written out, causing the stop bit to get
1444 	 * sent to the wrong (new) bus.  This causes the
1445 	 * previous bus to permanently hang waiting for the
1446 	 * stop bit.
1447 	 */
1448 	pcf8584_select_bus(i2c);
1449 
1450 	i2c->pcf8584_tran_state = TRAN_STATE_DUMMY_DATA;
1451 	pcf8584_put_s0(i2c, DUMMY_ADDR);
1452 	PCF8584_DDB(pcf8584_print(PRT_TRAN,
1453 	    "FIRST WRITE DUMMY ADDR: write %x\n", DUMMY_ADDR));
1454 	if (i2c->pcf8584_mode ==  PCF8584_POLL_MODE)
1455 		pcf8584_put_s1(i2c, S1_START);
1456 	else
1457 		pcf8584_put_s1(i2c, S1_START | S1_ENI);
1458 
1459 	/*
1460 	 * Update transfer status so any polled i/o request coming in
1461 	 * after this will complete this transfer for us, before issuing
1462 	 * its own.
1463 	 */
1464 	i2c->pcf8584_cur_status = PCF8584_TRANSFER_ON;
1465 
1466 	if (i2c->pcf8584_mode ==  PCF8584_POLL_MODE)
1467 		pcf8584_do_polled_io(i2c);
1468 
1469 	if (took_over)
1470 		pcf8584_give_up(i2c, waiter, saved_mode);
1471 	else {
1472 		if (i2c->pcf8584_mode != PCF8584_POLL_MODE)
1473 			cv_wait(&i2c->pcf8584_icv, &i2c->pcf8584_imutex);
1474 		mutex_exit(&i2c->pcf8584_imutex);
1475 
1476 		/*
1477 		 * Release the I2C bus only if we still own it. If we don't
1478 		 * own it (someone usurped it from us while we were waiting),
1479 		 * we still need to drop the lock that serializes access to
1480 		 * the pcf8584 controller on systems where OBP shares the
1481 		 * controller with the OS.
1482 		 */
1483 		if (i2c->pcf8584_cur_tran == tp)
1484 			pcf8584_release(i2c, B_FALSE);
1485 		else if (&plat_shared_i2c_exit && dip)
1486 			plat_shared_i2c_exit(i2c->pcf8584_dip);
1487 	}
1488 
1489 	return (tp->i2c_result);
1490 }
1491 
1492 static void
1493 pcf8584_do_polled_io(pcf8584_t *i2c)
1494 {
1495 	int completed = I2C_PENDING;
1496 	uint8_t s1;
1497 
1498 	while (completed != I2C_COMPLETE) {
1499 		s1 = pcf8584_get_s1(i2c);
1500 		if (!(s1 & S1_PIN)) {
1501 			ASSERT(i2c->pcf8584_cur_tran);
1502 			completed = pcf8584_process(i2c, s1);
1503 		}
1504 		drv_usecwait(1);
1505 	}
1506 
1507 	i2c->pcf8584_cur_status = PCF8584_TRANSFER_OVER;
1508 }
1509 
1510 /*
1511  * pcf8584_take_over() grabs the I2C bus and other resources by force and
1512  * flushes any pending transaction. This is called if a polled i/o
1513  * request comes in.
1514  */
1515 static void
1516 pcf8584_take_over(pcf8584_t *i2c, dev_info_t *dip, i2c_transfer_t *tp,
1517     kcondvar_t **waiter, int *saved_mode)
1518 {
1519 	mutex_enter(&i2c->pcf8584_imutex);
1520 	*saved_mode = i2c->pcf8584_mode;
1521 	i2c->pcf8584_mode = PCF8584_POLL_MODE;
1522 
1523 	/*
1524 	 * We need to flush out any currently pending transaction before
1525 	 * issuing ours.
1526 	 */
1527 	if (i2c->pcf8584_busy) {
1528 		if (i2c->pcf8584_cur_tran &&
1529 		    i2c->pcf8584_cur_status == PCF8584_TRANSFER_ON) {
1530 			pcf8584_do_polled_io(i2c);
1531 			*waiter = &i2c->pcf8584_icv;
1532 		}
1533 	}
1534 
1535 	/*
1536 	 * Since pcf8584_acquire() is by default a good citizen that
1537 	 * will wait its turn to acquire the I2C bus, we need to set
1538 	 * the 'force' flag on.
1539 	 */
1540 	pcf8584_acquire(i2c, dip, tp, B_TRUE);
1541 }
1542 
1543 /*
1544  * pcf8584_give_up() returns all resources that were taken over forcefully
1545  */
1546 static void
1547 pcf8584_give_up(pcf8584_t *i2c, kcondvar_t *waiter, int saved_mode)
1548 {
1549 	i2c->pcf8584_mode = saved_mode;
1550 
1551 	/*
1552 	 * Note that pcf8584_release only wakes up threads waiting to acquire
1553 	 * the I2C bus. We still need to wake up the waiter from whom we
1554 	 * usurped the bus.
1555 	 */
1556 	pcf8584_release(i2c, B_TRUE);
1557 	if (waiter)
1558 		cv_signal(waiter);
1559 
1560 	mutex_exit(&i2c->pcf8584_imutex);
1561 }
1562