xref: /titanic_50/usr/src/uts/sun/io/zs_common.c (revision c77a61a72b5ecdc507d6cf104142edd371a16c84)
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 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*
28  *	Serial I/O driver for Z8530 chips
29  */
30 
31 #pragma ident	"%Z%%M%	%I%	%E% SMI"
32 
33 #include	<sys/types.h>
34 #include	<sys/param.h>
35 #include	<sys/systm.h>
36 #include	<sys/sysmacros.h>
37 #include	<sys/stropts.h>
38 #include	<sys/stream.h>
39 #include	<sys/stat.h>
40 #include	<sys/mkdev.h>
41 #include	<sys/cmn_err.h>
42 #include	<sys/errno.h>
43 #include	<sys/kmem.h>
44 #include	<sys/zsdev.h>
45 #include	<sys/debug.h>
46 #include	<sys/machsystm.h>
47 
48 #include	<sys/conf.h>
49 #include	<sys/sunddi.h>
50 #include	<sys/errno.h>
51 
52 #define	 ZS_TRACING
53 #ifdef  ZS_TRACING
54 #include <sys/vtrace.h>
55 
56 /*
57  * Temp tracepoint definitions
58  */
59 #define	TR_FAC_ZS		51
60 
61 #define	TR_ZS_H_INT_START	1
62 #define	TR_ZS_H_INT_END		2
63 #define	TR_ZS_INT_START		3
64 #define	TR_ZS_INT_END		4
65 
66 #define	TR_FAC_ZS_INT		52
67 #define	TR_READ_START		1
68 #define	TR_READ_END		2
69 
70 #endif  /* ZSH_TRACING */
71 
72 #define	KIOIP		KSTAT_INTR_PTR(zs->intrstats)
73 
74 #ifndef	MAXZS
75 #define	MAXZS		4
76 #endif
77 int maxzs = MAXZS;
78 
79 int nzs = 0;
80 
81 struct zscom *zscom;
82 struct zscom *zscurr;
83 struct zscom *zslast;
84 struct zs_prog *zs_prog;
85 char  *zssoftCAR;
86 int	zs_watchdog_count = 10;	/* countdown to determine if tx hung */
87 
88 int zs_drain_check = 15000000;		/* tunable: exit drain check time */
89 
90 #ifdef ZS_DEBUG
91 char zs_h_log[ZS_H_LOG_MAX +10];
92 int zs_h_log_n = 0;
93 #define	zs_h_log_add(c) \
94 	{ \
95 		if (zs_h_log_n >= ZS_H_LOG_MAX) \
96 			zs_h_log_n = 0; \
97 		zs_h_log[zs_h_log_n++] = c; \
98 		zs_h_log[zs_h_log_n] = '\0'; \
99 	}
100 
101 #else /* NO_ZS_DEBUG */
102 #define	zs_h_log_add(c)
103 #endif /* ZS_DEBUG */
104 
105 
106 /*
107  * Driver data
108  */
109 
110 #define	GETPROP(dip, str, defval) \
111 	ddi_getprop(DDI_DEV_T_ANY, (dip), DDI_PROP_DONTPASS, (str), (defval))
112 
113 int	zs_usec_delay = 1;
114 int	zssoftpend;
115 ddi_softintr_t zs_softintr_id;
116 time_t	default_dtrlow = 3;	/* hold dtr low nearly this long on close */
117 static ddi_iblock_cookie_t zs_iblock;
118 static ddi_iblock_cookie_t zs_hi_iblock;
119 static int zs_addedsoft = 0;
120 
121 
122 /*
123  * Driver information for auto-configuration stuff.
124  */
125 
126 static int zsprobe(dev_info_t *dev);
127 static int zsattach(dev_info_t *dev, ddi_attach_cmd_t cmd);
128 static int zsdetach(dev_info_t *dev, ddi_detach_cmd_t cmd);
129 void	zsopinit(struct zscom *zs, struct zsops *zso);
130 
131 static void zsnull_intr(struct zscom *zs);
132 static int zsnull_softint(struct zscom *zs);
133 static int zsnull_suspend(struct zscom *zs);
134 static int zsnull_resume(struct zscom *zs);
135 
136 struct zsops zsops_null = {
137 			zsnull_intr,
138 			zsnull_intr,
139 			zsnull_intr,
140 			zsnull_intr,
141 			zsnull_softint,
142 			zsnull_suspend,
143 			zsnull_resume
144 };
145 
146 extern struct streamtab asynctab;	/* default -- from zs_async.c */
147 
148 uint_t zs_high_intr(caddr_t argzs);
149 uint_t zsintr(caddr_t intarg);
150 
151 extern int zsc_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg,
152 void **result);
153 
154 extern int ddi_create_internal_pathname(dev_info_t *dip, char *name,
155     int spec_type, minor_t minor_num);
156 
157 extern struct streamtab zsstab;
158 int		zssoftpend;		/* soft interrupt pending */
159 kmutex_t	zs_soft_lock;		/* adapt.lock,to use to protect data */
160 					/* common to sev. streams or ports   */
161 kmutex_t	zs_curr_lock;		/* lock protecting zscurr */
162 
163 extern kcondvar_t lbolt_cv;
164 
165 /*
166  * curently the only spin lock level 12 for all ocasions
167  */
168 
169 #define	ZSS_CONF_FLAG   (D_NEW | D_MP)
170 
171 static  struct cb_ops cb_zs_ops = {
172 	nulldev,		/* cb_open */
173 	nulldev,		/* cb_close */
174 	nodev,			/* cb_strategy */
175 	nodev,			/* cb_print */
176 	nodev,			/* cb_dump */
177 	nodev,			/* cb_read */
178 	nodev,			/* cb_write */
179 	nodev,			/* cb_ioctl */
180 	nodev,			/* cb_devmap */
181 	nodev,			/* cb_mmap */
182 	nodev,			/* cb_segmap */
183 	nochpoll,		/* cb_chpoll */
184 	ddi_prop_op,		/* cb_prop_op */
185 	&asynctab,		/* cb_stream */
186 	(int)(ZSS_CONF_FLAG)	/* cb_flag */
187 };
188 
189 struct dev_ops zs_ops = {
190 	DEVO_REV,		/* devo_rev */
191 	0,			/* devo_refcnt */
192 	zsc_info,		/* devo_getinfo */
193 	nulldev,		/* devo_identify */
194 	zsprobe,		/* devo_probe */
195 	zsattach,		/* devo_attach */
196 	zsdetach,		/* devo_detach */
197 	nodev,			/* devo_reset */
198 	&cb_zs_ops,		/* devo_cb_ops */
199 	(struct bus_ops *)NULL,	/* devo_bus_ops */
200 	ddi_power		/* devo_power */
201 };
202 
203 
204 /*
205  * This is the loadable module wrapper.
206  */
207 
208 #include <sys/modctl.h>
209 
210 /*
211  * Module linkage information for the kernel.
212  */
213 
214 extern struct mod_ops mod_driverops;
215 
216 static struct modldrv modldrv = {
217 		&mod_driverops, /* Type of module.  This one is a driver */
218 		"Z8530 serial driver V%I%",
219 		&zs_ops,	/* driver ops */
220 };
221 
222 static struct modlinkage modlinkage = {
223 			MODREV_1, (void *)&modldrv, NULL
224 };
225 
226 int
227 _init(void)
228 {
229 	return (mod_install(&modlinkage));
230 }
231 
232 int
233 _fini(void)
234 {
235 	return (EBUSY);
236 }
237 
238 int
239 _info(struct modinfo *modinfop)
240 {
241 	return (mod_info(&modlinkage, modinfop));
242 }
243 
244 static int
245 zsprobe(dev_info_t *dev)
246 {
247 	struct zscc_device *zsaddr;
248 	int	rval;
249 	auto char	c;
250 
251 	rval = DDI_PROBE_FAILURE;
252 
253 	/*
254 	 * temporarily map in  registers
255 	 */
256 	if (ddi_map_regs(dev, 0, (caddr_t *)&zsaddr, 0, 0)) {
257 		cmn_err(CE_WARN, "zsprobe: unable to map registers");
258 		return (rval);
259 	}
260 
261 	/*
262 	 * NON-DDI Compliant call
263 	 */
264 	mon_clock_stop();
265 
266 	/*
267 	 * get in sync with the chip
268 	 */
269 
270 	if (ddi_peek8(dev, (char *)&zsaddr->zscc_control, &c) != DDI_SUCCESS) {
271 		goto out;
272 	}
273 	drv_usecwait(2);
274 
275 	/*
276 	 * The traditional test for the presence of an 8530 has been to write
277 	 * a 15 (octal 017) to its control register address, then read it back.
278 	 * A Z8530 will respond to this with the contents of Read-Register 15.
279 	 * If this address were memory, or something else, we would expect to
280 	 * see the 15 again.  Normally, the contents of RR15 will be entirely
281 	 * different.  A Z8530 does not use the D0 and D2 bits of register 15,
282 	 * so they should equal zero.  Compatable chips should do the same.
283 	 * Beware of "enhanced" SCC's that do not guarantee this.
284 	 */
285 	if (ddi_poke8(dev, (char *)&zsaddr->zscc_control, '\017')
286 	    != DDI_SUCCESS) {
287 		goto out;
288 	}
289 	drv_usecwait(2);
290 	if (ddi_peek8(dev, (char *)&zsaddr->zscc_control, &c) != DDI_SUCCESS) {
291 		goto out;
292 	}
293 	drv_usecwait(2);
294 	if (c & 5) {
295 		goto out;
296 	}
297 
298 	rval = DDI_PROBE_SUCCESS;
299 
300 out:
301 	/*
302 	 * NON-DDI Compliant call
303 	 */
304 	mon_clock_start();
305 
306 	ddi_unmap_regs(dev, 0, (caddr_t *)&zsaddr, 0, 0);
307 	return (rval);
308 }
309 
310 /*ARGSUSED*/
311 static int
312 zsattach(dev_info_t *dev, ddi_attach_cmd_t cmd)
313 {
314 	struct zscom	*zs;
315 	int		loops, i;
316 	uint_t		s;
317 	int		rtsdtr_bits = 0;
318 	char			softcd;
319 	uchar_t	rr;
320 	short			speed[2];
321 	int			current_chip = ddi_get_instance(dev);
322 	struct zscc_device	*tmpzs;		/* for mapping purposes */
323 	char name[16];
324 	int keyboard_prop;
325 
326 	switch (cmd) {
327 	case DDI_ATTACH:
328 		break;
329 
330 	case DDI_RESUME:
331 		zs = &zscom[current_chip*2];
332 		/*
333 		 * Try to resume first channel
334 		 */
335 		if (!zs->zs_resume || (zs->zs_resume)(zs) != DDI_SUCCESS)
336 			return (DDI_FAILURE);
337 		/*
338 		 * And the second channel
339 		 */
340 		zs++;
341 		if (!zs->zs_resume || (zs->zs_resume)(zs) != DDI_SUCCESS) {
342 			zs--;
343 			if (!zs->zs_suspend ||
344 			    (zs->zs_suspend)(zs) != DDI_SUCCESS)
345 				cmn_err(CE_WARN,
346 				    "zs: inconsistent suspend/resume state");
347 			return (DDI_FAILURE);
348 		}
349 		return (DDI_SUCCESS);
350 
351 	default:
352 		return (DDI_FAILURE);
353 	}
354 
355 	if (zscom == NULL) {
356 		mutex_init(&zs_soft_lock, NULL, MUTEX_DRIVER, (void *)ZS_PL);
357 		mutex_init(&zs_curr_lock, NULL, MUTEX_DRIVER, (void *)ZS_PL_HI);
358 		zscom = kmem_zalloc(maxzs * sizeof (struct zscom), KM_SLEEP);
359 		zs_prog = kmem_zalloc(maxzs * sizeof (struct zs_prog),
360 		    KM_SLEEP);
361 		zssoftCAR = kmem_zalloc(maxzs, KM_SLEEP);
362 		/* don't set nzs until arrays are allocated */
363 		membar_producer();
364 		nzs = maxzs;
365 		zscurr = &zscom[(current_chip*2) + 1];
366 		zslast = &zscom[current_chip*2];
367 		i = GETPROP(dev, "zs-usec-delay", 0);
368 		zs_usec_delay = (i <= 0) ? 1 : i;
369 	}
370 
371 	if (2 * current_chip >=  maxzs) {
372 		cmn_err(CE_WARN,
373 		    "zs: unable to allocate resources for  chip %d.",
374 		    current_chip);
375 		cmn_err(CE_WARN, "Change zs:maxzs in /etc/system");
376 		return (DDI_FAILURE);
377 	}
378 	zs = &zscom[current_chip*2];
379 
380 	/*
381 	 * map in the device registers
382 	 */
383 	if (ddi_map_regs(dev, 0, (caddr_t *)&zs->zs_addr, 0, 0)) {
384 		cmn_err(CE_WARN, "zs%d: unable to map registers\n",
385 		    current_chip);
386 		return (DDI_FAILURE);
387 	}
388 
389 	tmpzs = zs->zs_addr;
390 
391 	/*
392 	 * Non-DDI compliant Sun-Ness specfic call(s)
393 	 */
394 
395 	/*
396 	 * Stop the monitor's polling interrupt.
397 	 *
398 	 * I know that this is not exactly obvious. On all sunmon PROM
399 	 * machines, the PROM has can have a high level periodic clock
400 	 * interrupt going at this time. It uses this periodic interrupt
401 	 * to poll the console tty or kbd uart to check for things like
402 	 * BREAK or L1-A (abort). While we're probing this device out we
403 	 * have to shut that off so the PROM won't get confused by what
404 	 * we're doing to the zs. This has caused some pretty funny bugs
405 	 * in its time.
406 	 *
407 	 * For OPENPROM machines, the prom  takes level12 interrupts directly,
408 	 * but we call this routine anyway (I forget why).
409 	 */
410 	mon_clock_stop();
411 
412 	/*
413 	 * Go critical to keep uart from urking.
414 	 */
415 	s = ddi_enter_critical();
416 
417 	/*
418 	 * We are about to issue a full reset to this chip.
419 	 * First, now that interrupts are blocked, we will delay up to a
420 	 * half-second, checking both channels for any stray activity.
421 	 * Next we will preserve the time constants from both channels,
422 	 * so that they can be restored after the reset.  This is especially
423 	 * important for the console device.  Finally, do the reset and
424 	 * follow it with an extended recovery while the chip settles down.
425 	 */
426 	for (loops = 0; loops++ <= 500; DELAY(1000)) {
427 		SCC_READA(1, rr);
428 		if ((rr & ZSRR1_ALL_SENT) == 0) continue;
429 		SCC_READB(1, rr);
430 		if ((rr & ZSRR1_ALL_SENT) == 0) continue;
431 		SCC_READA(0, rr);
432 		if ((rr & ZSRR0_TX_READY) == 0) continue;
433 		SCC_READB(0, rr);
434 		if ((rr & ZSRR0_TX_READY) != 0) break;
435 	}
436 
437 	SCC_READA(12, speed[0]);
438 	SCC_READA(13, rr);
439 	speed[0] |= rr << 8;
440 	SCC_READB(12, speed[1]);
441 	SCC_READB(13, rr);
442 	speed[1] |= rr << 8;
443 
444 	SCC_WRITE(9, ZSWR9_RESET_WORLD);
445 	DELAY(10);
446 
447 	/*
448 	 * Set up the other components of the zscom structs for this chip.
449 	 */
450 	for (i = 0; i < 2; i++) {
451 		/*
452 		 * Property for ignoring DCD.
453 		 * We switch between 'a' and 'b' ports for this device.
454 		 */
455 		static char *prop = "port-a-ignore-cd";
456 
457 		/*
458 		 * For this channel, set the hardware address, allocate the
459 		 * high-level mutex, and update the zscurr pointer.
460 		 * The high-level lock is shared by both channels because
461 		 * 8530 register addressing is non-atomic and asymetrical.
462 		 * Multiple threads crossing paths during this operation
463 		 * could trash the chip, and thus, possibly the system console.
464 		 */
465 		if (i == 0) {		/* port A */
466 			zs->zs_addr = (struct zscc_device *)
467 			    ((uintptr_t)tmpzs | ZSOFF);
468 			(zs+1)->zs_excl_hi = zs->zs_excl_hi = &zs_curr_lock;
469 		} else {		/* port B */
470 			zs++;
471 			zs->zs_addr = (struct zscc_device *)
472 			    ((uintptr_t)tmpzs & ~ZSOFF);
473 			zscurr = zs;
474 		}
475 		zs->zs_unit = current_chip * 2 + i;
476 		zs->zs_dip = dev;
477 		zs->zs_excl = kmem_zalloc(sizeof (kmutex_t), KM_SLEEP);
478 		mutex_init(zs->zs_excl, NULL, MUTEX_DRIVER, (void *)ZS_PL);
479 		zs->zs_ocexcl = kmem_zalloc(sizeof (kmutex_t), KM_SLEEP);
480 		mutex_init(zs->zs_ocexcl, NULL, MUTEX_DRIVER, (void *)ZS_PL);
481 
482 		zsopinit(zs, &zsops_null);
483 
484 		prop[5] = 'a' + i;
485 		softcd = GETPROP((dev_info_t *)(dev), prop, 0);
486 		zssoftCAR[zs->zs_unit] = softcd;
487 		if (softcd)
488 			rtsdtr_bits = ZSWR5_RTS | ZSWR5_DTR;
489 
490 		keyboard_prop = GETPROP((dev_info_t *)(zs->zs_dip),
491 		    "keyboard", 0);
492 
493 		mutex_enter(&zs_curr_lock);
494 
495 		/*
496 		 * Set up the default asynch modes
497 		 * so the monitor will still work
498 		 */
499 		SCC_WRITE(4, ZSWR4_PARITY_EVEN | ZSWR4_1_STOP | ZSWR4_X16_CLK);
500 		SCC_WRITE(3, ZSWR3_RX_8);
501 		SCC_WRITE(11, ZSWR11_TXCLK_BAUD | ZSWR11_RXCLK_BAUD);
502 		SCC_WRITE(12, (speed[i] & 0xff));
503 		SCC_WRITE(13, (speed[i] >> 8) & 0xff);
504 		SCC_WRITE(14, ZSWR14_BAUD_FROM_PCLK);
505 		SCC_WRITE(3, ZSWR3_RX_8 | ZSWR3_RX_ENABLE);
506 		SCC_WRITE(5, ZSWR5_TX_ENABLE | ZSWR5_TX_8 | rtsdtr_bits);
507 		SCC_WRITE(14, ZSWR14_BAUD_ENA | ZSWR14_BAUD_FROM_PCLK);
508 
509 		/*
510 		 * The SYNC pin on the second SCC (keyboard & mouse) may not
511 		 * be connected and noise creates transitions on this line.
512 		 * This floods the system with interrupts, unless the
513 		 * Sync/Hunt Interrupt Enable is cleared.  So write
514 		 * register 15 with everything we need but that one.
515 		 */
516 		if (keyboard_prop) {
517 			SCC_WRITE(15, ZSR15_BREAK | ZSR15_TX_UNDER |
518 			    ZSR15_CTS | ZSR15_CD);
519 		}
520 
521 		SCC_WRITE0(ZSWR0_RESET_ERRORS);
522 		SCC_WRITE0(ZSWR0_RESET_STATUS);
523 		mutex_exit(&zs_curr_lock);
524 
525 		zs->zs_dtrlow = gethrestime_sec() - default_dtrlow;
526 		cv_init(&zs->zs_flags_cv, NULL, CV_DEFAULT, NULL);
527 		zsa_init(zs);
528 	}
529 
530 	mutex_enter(&zs_curr_lock);
531 	SCC_WRITE(9, ZSWR9_MASTER_IE | ZSWR9_VECTOR_INCL_STAT);
532 	DELAY(4000);
533 	mutex_exit(&zs_curr_lock);
534 
535 	/*
536 	 * Two levels of interrupt - chip interrupts at a high level (12),
537 	 * (which is seen below as zs_high_intr), and then as a secondary
538 	 * stage soft interrupt as seen in zsintr below.
539 	 *
540 	 * Because zs_high_intr does a window save, as well as calls to
541 	 * other functions, we cannot install it as a "fast" interrupt
542 	 * that would execute right out of the trap window.  Too bad...
543 	 */
544 	if (ddi_add_intr(dev, (uint_t)0, &zs_hi_iblock,
545 	    (ddi_idevice_cookie_t *)0, zs_high_intr,
546 	    (caddr_t)0) != DDI_SUCCESS) {
547 		cmn_err(CE_PANIC, "cannot set high level zs interrupt");
548 		/*NOTREACHED*/
549 	}
550 
551 	if (zs_addedsoft == 0) {
552 		if (ddi_add_softintr(dev, DDI_SOFTINT_HIGH, &zs_softintr_id,
553 		    &zs_iblock, (ddi_idevice_cookie_t *)0,
554 		    zsintr, (caddr_t)0) != DDI_SUCCESS) {
555 			cmn_err(CE_PANIC,
556 				"cannot set second stage zs interrupt");
557 			/*NOTREACHED*/
558 		}
559 
560 		zs_addedsoft++;	/* we only need one zsintr! */
561 	}
562 
563 	if (zs > zslast)
564 		zslast = zs;
565 
566 	(void) ddi_exit_critical(s);
567 
568 	/*
569 	 * Non-DDI compliant Sun-Ness specific call
570 	 */
571 	mon_clock_start();	/* re-enable monitor's polling interrupt */
572 
573 	if (!GETPROP(zs->zs_dip, "keyboard", 0)) {
574 		static char *serial_line = DDI_NT_SERIAL_MB;
575 		static char *dial_out = DDI_NT_SERIAL_MB_DO;
576 
577 		/*
578 		 * Export names for channel a or b consconfig match...
579 		 * The names exported to the filesystem include the
580 		 * designated tty'a' type name and may not match the PROM
581 		 * pathname.
582 		 * Note the special name "obp-console-name" used in these calls.
583 		 * This keeps the ports and devlinks programs from seeing these
584 		 * names. (But allows ddi_pathname_to_dev_t to see them.)
585 		 * We don't need to do this if the instance number is zero,
586 		 * because we'll create them below, in this case.
587 		 */
588 
589 		if (ddi_get_instance(dev) != 0)  {
590 
591 			/*
592 			 * Select a node type unused by ddi/devfs
593 			 */
594 			static char *obp_type = "obp-console-name";
595 
596 			(void) strcpy(name, "a");
597 			if (ddi_create_minor_node(dev, name, S_IFCHR,
598 			    ddi_get_instance(dev) * 2,
599 			    obp_type, NULL) == DDI_FAILURE) {
600 				ddi_remove_minor_node(dev, NULL);
601 				return (DDI_FAILURE);
602 			}
603 			(void) strcpy(name, "b");
604 			if (ddi_create_minor_node(dev, name, S_IFCHR,
605 			    (ddi_get_instance(dev) * 2) + 1,
606 			    obp_type, NULL) == DDI_FAILURE) {
607 				ddi_remove_minor_node(dev, NULL);
608 				return (DDI_FAILURE);
609 			}
610 		}
611 
612 		/*
613 		 * Export normal device names...
614 		 */
615 		(void) sprintf(name, "%c", (ddi_get_instance(dev) + 'a'));
616 		if (ddi_create_minor_node(dev, name, S_IFCHR,
617 		    ddi_get_instance(dev) * 2,
618 		    serial_line, NULL) == DDI_FAILURE) {
619 			ddi_remove_minor_node(dev, NULL);
620 			return (DDI_FAILURE);
621 		}
622 		(void) sprintf(name, "%c", (ddi_get_instance(dev) + 'b'));
623 		if (ddi_create_minor_node(dev, name, S_IFCHR,
624 		    (ddi_get_instance(dev) * 2) + 1,
625 		    serial_line, NULL) == DDI_FAILURE) {
626 			ddi_remove_minor_node(dev, NULL);
627 			return (DDI_FAILURE);
628 		}
629 		(void) sprintf(name, "%c,cu", (ddi_get_instance(dev) + 'a'));
630 		if (ddi_create_minor_node(dev, name, S_IFCHR,
631 		    (ddi_get_instance(dev) * 2) | OUTLINE,
632 		    dial_out, NULL) == DDI_FAILURE) {
633 			ddi_remove_minor_node(dev, NULL);
634 			return (DDI_FAILURE);
635 		}
636 		(void) sprintf(name, "%c,cu", (ddi_get_instance(dev) + 'b'));
637 		if (ddi_create_minor_node(dev, name, S_IFCHR,
638 		    ((ddi_get_instance(dev)  * 2) + 1) | OUTLINE,
639 		    dial_out, NULL) == DDI_FAILURE) {
640 			ddi_remove_minor_node(dev, NULL);
641 			return (DDI_FAILURE);
642 		}
643 	} else {
644 
645 		/*
646 		 * Create keyboard and mouse nodes which devfs doesn't see
647 		 */
648 
649 		/*
650 		 * This set of minor nodes is for use with the consconfig_dacf
651 		 * module for the sun4u platforms.  (See PSARC/1998/212)
652 		 */
653 		if (ddi_create_internal_pathname(dev, "keyboard", S_IFCHR,
654 		    ddi_get_instance(dev) * 2) == DDI_FAILURE) {
655 			ddi_remove_minor_node(dev, NULL);
656 			return (DDI_FAILURE);
657 		}
658 
659 		if (ddi_create_internal_pathname(dev, "mouse", S_IFCHR,
660 		    (ddi_get_instance(dev) * 2) + 1) == DDI_FAILURE) {
661 			ddi_remove_minor_node(dev, NULL);
662 			return (DDI_FAILURE);
663 		}
664 
665 		/*
666 		 * These minor nodes are for use with pre-sun4u platforms.
667 		 * Either one set or the other will be opened by consconfig.
668 		 */
669 		(void) strcpy(name, "a");
670 		if (ddi_create_internal_pathname(dev, name, S_IFCHR,
671 			ddi_get_instance(dev) * 2) == DDI_FAILURE) {
672 			ddi_remove_minor_node(dev, NULL);
673 			return (DDI_FAILURE);
674 		}
675 
676 		(void) strcpy(name, "b");
677 		if (ddi_create_internal_pathname(dev, name, S_IFCHR,
678 			(ddi_get_instance(dev) * 2) + 1) == DDI_FAILURE) {
679 			ddi_remove_minor_node(dev, NULL);
680 			return (DDI_FAILURE);
681 		}
682 
683 	}
684 
685 	ddi_report_dev(dev);
686 	/*
687 	 * Initialize power management bookkeeping; components are
688 	 * created idle.
689 	 */
690 	if (pm_create_components(dev, 3) == DDI_SUCCESS) {
691 		(void) pm_busy_component(dev, 0);
692 		pm_set_normal_power(dev, 0, 1);
693 		pm_set_normal_power(dev, 1, 1);
694 		pm_set_normal_power(dev, 2, 1);
695 	} else {
696 		return (DDI_FAILURE);
697 	}
698 
699 	(void) sprintf(name, "zsc%d", current_chip);
700 	zs->intrstats = kstat_create("zs", current_chip, name, "controller",
701 		KSTAT_TYPE_INTR, 1, KSTAT_FLAG_PERSISTENT);
702 	if (zs->intrstats) {
703 		kstat_install(zs->intrstats);
704 	}
705 
706 	return (DDI_SUCCESS);
707 }
708 
709 static int
710 zsdetach(dev_info_t *dev, ddi_detach_cmd_t cmd)
711 {
712 	struct zscom *zs;
713 	int	current_chip = ddi_get_instance(dev);
714 
715 	switch (cmd) {
716 	case DDI_DETACH:
717 		return (DDI_FAILURE);
718 
719 	case DDI_SUSPEND:
720 		zs = &zscom[current_chip*2];
721 		/*
722 		 * Try to suspend first channel
723 		 */
724 		if (!zs->zs_suspend || (zs->zs_suspend)(zs) != DDI_SUCCESS)
725 			return (DDI_FAILURE);
726 		/*
727 		 * And the second channel
728 		 */
729 		zs++;
730 		if (!zs->zs_suspend || (zs->zs_suspend)(zs) != DDI_SUCCESS) {
731 			zs--;
732 			if (!zs->zs_resume ||
733 			    (zs->zs_resume)(zs) != DDI_SUCCESS)
734 				cmn_err(CE_WARN,
735 				    "zs: inconsistent suspend/resume state");
736 			return (DDI_FAILURE);
737 		}
738 		return (DDI_SUCCESS);
739 
740 	default:
741 		return (DDI_FAILURE);
742 	}
743 }
744 
745 /*
746  * SCC High Level Interrupt Handler
747  *
748  * This routine fields the level 12 interrupts generated by the 8530 chips.
749  * When the SCC interrupts the conditions that triggered it are available
750  * for reference in Read Register 3 of the A channel (RR3A).  We process
751  * all the pending interrupts before returning.  The maximum interrupts
752  * that will be processed before returning is set to 6, which is twice
753  * the size of RX-FIFO.
754  * We keep a pointer to the B side of the most recently interrupting chip
755  * in zscurr.
756  */
757 
758 /*
759  * 'argzs' actually 'struct zscom *argzs'
760  */
761 
762 #define	ZSRR3_INT_PENDING (ZSRR3_IP_B_STAT | ZSRR3_IP_B_TX | ZSRR3_IP_B_RX |\
763 			ZSRR3_IP_A_STAT | ZSRR3_IP_A_TX | ZSRR3_IP_A_RX)
764 
765 #define	ZSRR1_ANY_ERRORS (ZSRR1_PE | ZSRR1_DO | ZSRR1_FE | ZSRR1_RXEOF)
766 #define	ZS_HIGH_INTR_LOOPLIMIT 6
767 
768 /*ARGSUSED*/
769 uint_t
770 zs_high_intr(caddr_t argzs)
771 {
772 	struct zscom	*zs;
773 	uchar_t	stat, isource, count;
774 	int		unit;
775 
776 	TRACE_0(TR_FAC_ZS, TR_ZS_H_INT_START, "zs_h_int start");
777 	mutex_enter(&zs_curr_lock);
778 	zs = zscurr;				/* Points at Channel B */
779 
780 	ZSNEXTPOLL(zs, zscurr);
781 
782 	SCC_READA(3, isource);
783 start_zs_h:
784 	count = ZS_HIGH_INTR_LOOPLIMIT;
785 	while ((isource & ZSRR3_INT_PENDING) && (count--)) {
786 		if (isource & ZSRR3_IP_B_STAT)
787 			(zs->zs_xsint)(zs);
788 		else {
789 			if (isource & ZSRR3_IP_B_TX)
790 				(zs->zs_txint)(zs);
791 			if (isource & ZSRR3_IP_B_RX) {
792 				SCC_READ(1, stat);
793 				if (stat & ZSRR1_ANY_ERRORS)
794 					(zs->zs_srint)(zs);
795 				else if ((SCC_READ0()) & ZSRR0_RX_READY)
796 					(zs->zs_rxint)(zs);
797 			}
798 		}
799 
800 		zs -= 1;
801 		if (isource & ZSRR3_IP_A_STAT)
802 			(zs->zs_xsint)(zs);
803 		else {
804 			if (isource & ZSRR3_IP_A_TX)
805 				(zs->zs_txint)(zs);
806 			if (isource & ZSRR3_IP_A_RX) {
807 				SCC_READ(1, stat);
808 				if (stat & ZSRR1_ANY_ERRORS)
809 					(zs->zs_srint)(zs);
810 				else if ((SCC_READ0()) & ZSRR0_RX_READY)
811 					(zs->zs_rxint)(zs);
812 			}
813 		}
814 
815 		zs = zscurr;
816 		SCC_READA(3, isource);
817 	}
818 
819 	if (count == ZS_HIGH_INTR_LOOPLIMIT) {
820 		unit = (nzs >> 1) - 1;
821 		while (unit--) {
822 			zs += 2;	/* Always Channel B */
823 			if (zs > zslast)
824 				zs = &zscom[1];
825 			if (!zs->zs_ops)
826 				continue;
827 			SCC_READA(3, isource);
828 			if (isource & ZSRR3_INT_PENDING) {
829 				zscurr = zs;	/* update zscurr and */
830 				goto start_zs_h;
831 			}
832 		}
833 		if (zs->intrstats) {
834 			KIOIP->intrs[KSTAT_INTR_HARD]++;
835 		}
836 		mutex_exit(&zs_curr_lock);
837 		TRACE_0(TR_FAC_ZS, TR_ZS_H_INT_END, "zs_h_int end");
838 		return (DDI_INTR_UNCLAIMED);	/* Must not be for us. */
839 	}
840 	if (zs->intrstats) {
841 		KIOIP->intrs[KSTAT_INTR_HARD]++;
842 	}
843 	mutex_exit(&zs_curr_lock);	/* we're done with zscurr */
844 	TRACE_0(TR_FAC_ZS, TR_ZS_H_INT_END, "zs_h_int end");
845 	return (DDI_INTR_CLAIMED);
846 }
847 
848 /*
849  * Handle a second-stage interrupt.
850  */
851 /*ARGSUSED*/
852 uint_t
853 zsintr(caddr_t intarg)
854 {
855 	struct zscom *zs;
856 	int    rv;
857 
858 	/*
859 	 * Test and clear soft interrupt.
860 	 */
861 	TRACE_0(TR_FAC_ZS, TR_ZS_INT_START,
862 	    "zs_int start");
863 
864 	mutex_enter(&zs_curr_lock);
865 	rv = zssoftpend;
866 	if (rv != 0) {
867 		zssoftpend = 0;
868 	}
869 	mutex_exit(&zs_curr_lock);
870 
871 	if (rv) {
872 		for (zs = &zscom[0]; zs <= zslast; zs++) {
873 			if (zs->zs_flags & ZS_NEEDSOFT) {
874 				zs->zs_flags &= ~ZS_NEEDSOFT;
875 				(*zs->zs_ops->zsop_softint)(zs);
876 				if (zs->intrstats) {
877 					KIOIP->intrs[KSTAT_INTR_SOFT]++;
878 				}
879 			}
880 		}
881 	}
882 	TRACE_0(TR_FAC_ZS, TR_ZS_INT_END,
883 	    "zs_int end");
884 	return (rv);
885 }
886 
887 void
888 setzssoft(void)
889 {
890 	ddi_trigger_softintr(zs_softintr_id);
891 }
892 
893 /*
894  * Install a new ops vector into low level vector routine addresses
895  */
896 void
897 zsopinit(struct zscom *zs, struct zsops *zso)
898 {
899 	zs->zs_txint	= zso->zsop_txint;
900 	zs->zs_xsint	= zso->zsop_xsint;
901 	zs->zs_rxint	= zso->zsop_rxint;
902 	zs->zs_srint	= zso->zsop_srint;
903 	zs->zs_suspend	= zso->zsop_suspend;
904 	zs->zs_resume	= zso->zsop_resume;
905 	zs->zs_ops	= zso;
906 	zs->zs_flags	= 0;
907 }
908 
909 /*
910  * Set or get the modem control status.
911  *
912  * This routine relies on the fact that the bits of interest in RR0 (CD and
913  * CTS) do not overlap the bits of interest in WR5 (RTS and DTR).  Thus, they
914  * can be combined into a single 'int' without harm.
915  */
916 int
917 zsmctl(struct zscom *zs, int bits, int how)
918 {
919 	int mbits, obits;
920 	time_t now, held;
921 
922 	ASSERT(mutex_owned(zs->zs_excl_hi));
923 	ASSERT(mutex_owned(zs->zs_excl));
924 
925 again:
926 	mbits = zs->zs_wreg[5] & (ZSWR5_RTS|ZSWR5_DTR);
927 	SCC_WRITE0(ZSWR0_RESET_STATUS);
928 	mbits |= SCC_READ0() & (ZSRR0_CD|ZSRR0_CTS);
929 	ZSDELAY();
930 	obits = mbits;
931 
932 	switch (how) {
933 
934 	case DMSET:
935 		mbits = bits;
936 		break;
937 
938 	case DMBIS:
939 		mbits |= bits;
940 		break;
941 
942 	case DMBIC:
943 		mbits &= ~bits;
944 		break;
945 
946 	case DMGET:
947 		return (mbits);
948 	}
949 
950 	now = gethrestime_sec();
951 	held = now - zs->zs_dtrlow;
952 
953 	/*
954 	 * if DTR is going low, stash current time away
955 	 */
956 	if (~mbits & obits & ZSWR5_DTR)
957 		zs->zs_dtrlow = now;
958 
959 	/*
960 	 * if DTR is going high, sleep until it has been low a bit
961 	 */
962 	if ((mbits & ~obits & ZSWR5_DTR) && (held < default_dtrlow)) {
963 		mutex_exit(zs->zs_excl_hi);
964 		cv_wait(&lbolt_cv, zs->zs_excl);
965 		if (zs->zs_suspended)
966 			(void) ddi_dev_is_needed(zs->zs_dip, 0, 1);
967 		mutex_enter(zs->zs_excl_hi);
968 		goto again;
969 	}
970 
971 	zs->zs_wreg[5] &= ~(ZSWR5_RTS|ZSWR5_DTR);
972 	SCC_BIS(5, mbits & (ZSWR5_RTS|ZSWR5_DTR));
973 	return (mbits);
974 }
975 
976 /*
977  * Program the Z8530 registers.
978  */
979 void
980 zs_program(struct zs_prog *zspp)
981 {
982 	struct zscom *zs = zspp->zs;
983 	int	loops;
984 	uchar_t	c;
985 	uchar_t wr10 = 0, wr14 = 0;
986 
987 	ASSERT(mutex_owned(zs->zs_excl));
988 	ASSERT(mutex_owned(zs->zs_excl_hi));
989 
990 	/*
991 	 * There are some special cases to account for before reprogramming.
992 	 * We might be transmitting, so delay 100,000 usec (worst case at 110
993 	 * baud) for this to finish, then disable the receiver until later,
994 	 * reset the External Status Change latches and the error bits, and
995 	 * drain the receive FIFO.
996 	 * XXX: Doing any kind of reset (WR9) here causes trouble!
997 	 */
998 	if (zspp->flags & ZSP_SYNC) {
999 		SCC_WRITE(7, SDLCFLAG);
1000 		wr10 = ZSWR10_PRESET_ONES;
1001 		if (zspp->flags & ZSP_NRZI)
1002 			wr10 |= ZSWR10_NRZI;
1003 		SCC_WRITE(10, wr10);
1004 	} else {
1005 		for (loops = 1000; loops > 0; --loops) {
1006 			SCC_READ(1, c);
1007 			if (c & ZSRR1_ALL_SENT)
1008 				break;
1009 			DELAY(100);
1010 		}
1011 		SCC_WRITE(3, 0);
1012 		SCC_WRITE0(ZSWR0_RESET_STATUS);
1013 		SCC_WRITE0(ZSWR0_RESET_ERRORS);
1014 		c = SCC_READDATA();		/* Empty the FIFO */
1015 		c = SCC_READDATA();
1016 		c = SCC_READDATA();
1017 	}
1018 
1019 	/*
1020 	 * Programming the SCC is done in three phases.
1021 	 * Phase one sets operating modes:
1022 	 */
1023 	SCC_WRITE(4, zspp->wr4);
1024 	SCC_WRITE(11, zspp->wr11);
1025 	SCC_WRITE(12, zspp->wr12);
1026 	SCC_WRITE(13, zspp->wr13);
1027 	if (zspp->flags & ZSP_PLL) {
1028 		SCC_WRITE(14, ZSWR14_DPLL_SRC_BAUD);
1029 		SCC_WRITE(14, ZSWR14_DPLL_NRZI);
1030 	} else
1031 		SCC_WRITE(14, ZSWR14_DPLL_DISABLE);
1032 
1033 	/*
1034 	 * Phase two enables special hardware functions:
1035 	 */
1036 	wr14 = ZSWR14_BAUD_FROM_PCLK | ZSWR14_BAUD_ENA;
1037 	if (zspp->flags & ZSP_LOOP)
1038 		wr14 |= ZSWR14_LOCAL_LOOPBACK;
1039 	if (zspp->flags & ZSP_ECHO)
1040 		wr14 |= ZSWR14_AUTO_ECHO;
1041 	SCC_WRITE(14, wr14);
1042 	SCC_WRITE(3, zspp->wr3);
1043 	SCC_WRITE(5, zspp->wr5);
1044 
1045 	SCC_WRITE0(ZSWR0_RESET_TXCRC);
1046 
1047 	if (zspp->flags & ZSP_PARITY_SPECIAL) {
1048 		SCC_WRITE(1, ZSWR1_PARITY_SPECIAL);
1049 	} else {
1050 		SCC_WRITE(1, 0);
1051 	}
1052 
1053 	/*
1054 	 * Phase three enables interrupt sources:
1055 	 */
1056 	SCC_WRITE(15, zspp->wr15);
1057 	SCC_WRITE0(ZSWR0_RESET_STATUS);
1058 	SCC_WRITE0(ZSWR0_RESET_ERRORS);
1059 	SCC_BIS(1, ZSWR1_INIT);
1060 }
1061 
1062 static void
1063 zsnull_intr(struct zscom *zs)
1064 {
1065 	short	c;
1066 
1067 	SCC_WRITE0(ZSWR0_RESET_TXINT);
1068 	SCC_WRITE0(ZSWR0_RESET_STATUS);
1069 	c = SCC_READDATA();
1070 	ZSDELAY();
1071 #ifdef lint
1072 	c = c;
1073 #endif /* lint */
1074 	SCC_WRITE0(ZSWR0_RESET_ERRORS);
1075 }
1076 
1077 static int
1078 zsnull_softint(struct zscom *zs)
1079 {
1080 	cmn_err(CE_WARN, "zs%d: unexpected soft int\n", zs->zs_unit);
1081 	return (0);
1082 }
1083 
1084 /*
1085  * These will be called on suspend/resume for un-opened zs ports.
1086  */
1087 static int
1088 zsnull_suspend(struct zscom *zs)
1089 {
1090 	struct zs_prog	*zspp = &zs_prog[zs->zs_unit];
1091 
1092 	/*
1093 	 * Get a copy of the current registers
1094 	 */
1095 	mutex_enter(zs->zs_excl);
1096 	mutex_enter(zs->zs_excl_hi);
1097 	zspp->zs = zs;
1098 	zspp->flags = 0;
1099 	zspp->wr3 = zs->zs_wreg[3];
1100 	zspp->wr4 = zs->zs_wreg[4];
1101 	zspp->wr5 = zs->zs_wreg[5];
1102 	zspp->wr11 = zs->zs_wreg[11];
1103 	zspp->wr12 = zs->zs_wreg[12];
1104 	zspp->wr13 = zs->zs_wreg[13];
1105 	zspp->wr15 = zs->zs_wreg[15];
1106 	mutex_exit(zs->zs_excl_hi);
1107 	mutex_exit(zs->zs_excl);
1108 
1109 	return (DDI_SUCCESS);
1110 }
1111 
1112 static int
1113 zsnull_resume(struct zscom *zs)
1114 {
1115 	struct zs_prog	*zspp = &zs_prog[zs->zs_unit];
1116 
1117 	/*
1118 	 * Restore registers
1119 	 */
1120 	mutex_enter(zs->zs_excl);
1121 	mutex_enter(zs->zs_excl_hi);
1122 	zs_program(zspp);
1123 	SCC_WRITE(9, ZSWR9_MASTER_IE);
1124 	DELAY(4000);
1125 	mutex_exit(zs->zs_excl_hi);
1126 	mutex_exit(zs->zs_excl);
1127 	return (DDI_SUCCESS);
1128 }
1129