xref: /titanic_50/usr/src/uts/sun4u/io/rmc_comm.c (revision 0b46ffbad80838700776674f5dea3ad9b16dd6e4)
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 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  *
26  * The "rmc_comm" driver provides access to the RMC so that its clients need
27  * not be concerned with the details of the access mechanism, which in this
28  * case is implemented via a packet-based protocol over a serial link via a
29  * 16550 compatible serial port.
30  */
31 
32 #pragma ident	"%Z%%M%	%I%	%E% SMI"
33 
34 /*
35  *  Header files
36  */
37 #include <sys/conf.h>
38 #include <sys/cyclic.h>
39 #include <sys/membar.h>
40 #include <sys/modctl.h>
41 #include <sys/strlog.h>
42 #include <sys/types.h>
43 #include <sys/sunddi.h>
44 #include <sys/ddi.h>
45 #include <sys/rmc_comm_dp_boot.h>
46 #include <sys/rmc_comm_dp.h>
47 #include <sys/rmc_comm_drvintf.h>
48 #include <sys/rmc_comm.h>
49 #include <sys/cpu_sgnblk_defs.h>
50 
51 /*
52  * Local definitions
53  */
54 
55 #define	ddi_driver_major(dip)	ddi_name_to_major(ddi_binding_name(dip))
56 
57 #define	MYNAME			"rmc_comm"
58 #define	NOMAJOR			(~(major_t)0)
59 #define	DUMMY_VALUE		(~(int8_t)0)
60 
61 /*
62  * Local data
63  */
64 static void *rmc_comm_statep;
65 static major_t rmc_comm_major = NOMAJOR;
66 static kmutex_t rmc_comm_attach_lock;
67 static ddi_device_acc_attr_t rmc_comm_dev_acc_attr[1] =
68 {
69 	DDI_DEVICE_ATTR_V0,
70 	DDI_STRUCTURE_LE_ACC,
71 	DDI_STRICTORDER_ACC
72 };
73 static int watchdog_was_active;
74 extern int watchdog_activated;
75 extern int watchdog_enable;
76 
77 /*
78  * prototypes
79  */
80 
81 extern void dp_reset(struct rmc_comm_state *, uint8_t, boolean_t, boolean_t);
82 static void sio_put_reg(struct rmc_comm_state *, uint_t, uint8_t);
83 static uint8_t sio_get_reg(struct rmc_comm_state *, uint_t);
84 static void sio_check_fault_status(struct rmc_comm_state *);
85 static boolean_t sio_data_ready(struct rmc_comm_state *);
86 static void rmc_comm_set_irq(struct rmc_comm_state *, boolean_t);
87 static uint_t rmc_comm_hi_intr(caddr_t);
88 static uint_t rmc_comm_softint(caddr_t);
89 static void rmc_comm_cyclic(void *);
90 static void rmc_comm_hw_reset(struct rmc_comm_state *);
91 static void rmc_comm_offline(struct rmc_comm_state *);
92 static int rmc_comm_online(struct rmc_comm_state *, dev_info_t *);
93 static void rmc_comm_unattach(struct rmc_comm_state *, dev_info_t *, int,
94     boolean_t, boolean_t, boolean_t);
95 static int rmc_comm_attach(dev_info_t *, ddi_attach_cmd_t);
96 static int rmc_comm_detach(dev_info_t *, ddi_detach_cmd_t);
97 
98 /*
99  * for client leaf drivers to register their desire for rmc_comm
100  * to stay attached
101  */
102 int
103 rmc_comm_register()
104 {
105 	struct rmc_comm_state *rcs;
106 
107 	mutex_enter(&rmc_comm_attach_lock);
108 	rcs = ddi_get_soft_state(rmc_comm_statep, 0);
109 	if ((rcs == NULL) || (!rcs->is_attached)) {
110 		mutex_exit(&rmc_comm_attach_lock);
111 		return (DDI_FAILURE);
112 	}
113 	rcs->n_registrations++;
114 	mutex_exit(&rmc_comm_attach_lock);
115 	return (DDI_SUCCESS);
116 }
117 
118 void
119 rmc_comm_unregister()
120 {
121 	struct rmc_comm_state *rcs;
122 
123 	mutex_enter(&rmc_comm_attach_lock);
124 	rcs = ddi_get_soft_state(rmc_comm_statep, 0);
125 	ASSERT(rcs != NULL);
126 	ASSERT(rcs->n_registrations != 0);
127 	rcs->n_registrations--;
128 	mutex_exit(&rmc_comm_attach_lock);
129 }
130 
131 /*
132  * to get the soft state structure of a specific instance
133  */
134 struct rmc_comm_state *
135 rmc_comm_getstate(dev_info_t *dip, int instance, const char *caller)
136 {
137 	struct rmc_comm_state *rcs = NULL;
138 	dev_info_t *sdip = NULL;
139 	major_t dmaj = NOMAJOR;
140 
141 	if (dip != NULL) {
142 		/*
143 		 * Use the instance number from the <dip>; also,
144 		 * check that it really corresponds to this driver
145 		 */
146 		instance = ddi_get_instance(dip);
147 		dmaj = ddi_driver_major(dip);
148 		if (rmc_comm_major == NOMAJOR && dmaj != NOMAJOR)
149 			rmc_comm_major = dmaj;
150 		else if (dmaj != rmc_comm_major) {
151 			cmn_err(CE_WARN,
152 			    "%s: major number mismatch (%d vs. %d) in %s(),"
153 			    "probably due to child misconfiguration",
154 			    MYNAME, rmc_comm_major, dmaj, caller);
155 			instance = -1;
156 		}
157 	}
158 	if (instance >= 0)
159 		rcs = ddi_get_soft_state(rmc_comm_statep, instance);
160 	if (rcs != NULL) {
161 		sdip = rcs->dip;
162 		if (dip == NULL && sdip == NULL)
163 			rcs = NULL;
164 		else if (dip != NULL && sdip != NULL && sdip != dip) {
165 			cmn_err(CE_WARN,
166 			    "%s: devinfo mismatch (%p vs. %p) in %s(), "
167 			    "probably due to child misconfiguration", MYNAME,
168 			    (void *)dip, (void *)sdip, caller);
169 			rcs = NULL;
170 		}
171 	}
172 
173 	return (rcs);
174 }
175 
176 
177 /*
178  * Lowest-level serial I/O chip register read/write
179  */
180 static void
181 sio_put_reg(struct rmc_comm_state *rcs, uint_t reg, uint8_t val)
182 {
183 	DPRINTF(rcs, DSER, (CE_CONT, "REG[%d]<-$%02x", reg, val));
184 
185 	if (rcs->sd_state.sio_handle != NULL && !rcs->sd_state.sio_fault) {
186 		/*
187 		 * The chip is mapped as "I/O" (e.g. with the side-effect
188 		 * bit on SPARC), therefore accesses are required to be
189 		 * in-order, with no value cacheing.  However, there can
190 		 * still be write-behind buffering, so it is not guaranteed
191 		 * that a write actually reaches the chip in a given time.
192 		 *
193 		 * To force the access right through to the chip, we follow
194 		 * the write with another write (to the SCRATCH register)
195 		 * and a read (of the value just written to the SCRATCH
196 		 * register).  The SCRATCH register is specifically provided
197 		 * for temporary data and has no effect on the SIO's own
198 		 * operation, making it ideal as a synchronising mechanism.
199 		 *
200 		 * If we didn't do this, it would be possible that the new
201 		 * value wouldn't reach the chip (and have the *intended*
202 		 * side-effects, such as disabling interrupts), for such a
203 		 * long time that the processor could execute a *lot* of
204 		 * instructions - including exiting the interrupt service
205 		 * routine and re-enabling interrupts.  This effect was
206 		 * observed to lead to spurious (unclaimed) interrupts in
207 		 * some circumstances.
208 		 *
209 		 * This will no longer be needed once "synchronous" access
210 		 * handles are available (see PSARC/2000/269 and 2000/531).
211 		 */
212 		ddi_put8(rcs->sd_state.sio_handle,
213 		    rcs->sd_state.sio_regs + reg, val);
214 		ddi_put8(rcs->sd_state.sio_handle,
215 		    rcs->sd_state.sio_regs + SIO_SCR, val);
216 		membar_sync();
217 		(void) ddi_get8(rcs->sd_state.sio_handle,
218 		    rcs->sd_state.sio_regs + SIO_SCR);
219 	}
220 }
221 
222 static uint8_t
223 sio_get_reg(struct rmc_comm_state *rcs, uint_t reg)
224 {
225 	uint8_t val;
226 
227 	if (rcs->sd_state.sio_handle && !rcs->sd_state.sio_fault)
228 		val = ddi_get8(rcs->sd_state.sio_handle,
229 		    rcs->sd_state.sio_regs + reg);
230 	else
231 		val = DUMMY_VALUE;
232 	DPRINTF(rcs, DSER, (CE_CONT, "$%02x<-REG[%d]", val, reg));
233 	return (val);
234 }
235 
236 static void
237 sio_check_fault_status(struct rmc_comm_state *rcs)
238 {
239 	rcs->sd_state.sio_fault =
240 		ddi_check_acc_handle(rcs->sd_state.sio_handle) != DDI_SUCCESS;
241 }
242 
243 boolean_t
244 rmc_comm_faulty(struct rmc_comm_state *rcs)
245 {
246 	if (!rcs->sd_state.sio_fault)
247 		sio_check_fault_status(rcs);
248 	return (rcs->sd_state.sio_fault);
249 }
250 
251 /*
252  * Check for data ready.
253  */
254 static boolean_t
255 sio_data_ready(struct rmc_comm_state *rcs)
256 {
257 	uint8_t status;
258 
259 	/*
260 	 * Data is available if the RXDA bit in the LSR is nonzero
261 	 * (if reading it didn't incur a fault).
262 	 */
263 	status = sio_get_reg(rcs, SIO_LSR);
264 	return ((status & SIO_LSR_RXDA) != 0 && !rmc_comm_faulty(rcs));
265 }
266 
267 /*
268  * Enable/disable interrupts
269  */
270 static void
271 rmc_comm_set_irq(struct rmc_comm_state *rcs, boolean_t newstate)
272 {
273 	uint8_t val;
274 
275 	val = newstate ? SIO_IER_RXHDL_IE : 0;
276 	sio_put_reg(rcs, SIO_IER, SIO_IER_STD | val);
277 	rcs->sd_state.hw_int_enabled = newstate;
278 }
279 
280 /*
281  * High-level interrupt handler:
282  *	Checks whether initialisation is complete (to avoid a race
283  *	with mutex_init()), and whether chip interrupts are enabled.
284  *	If not, the interrupt's not for us, so just return UNCLAIMED.
285  *	Otherwise, disable the interrupt, trigger a softint, and return
286  *	CLAIMED.  The softint handler will then do all the real work.
287  *
288  *	NOTE: the chip interrupt capability is only re-enabled once the
289  *	receive code has run, but that can be called from a poll loop
290  *	or cyclic callback as well as from the softint.  So it's *not*
291  *	guaranteed that there really is a chip interrupt pending here,
292  *	'cos the work may already have been done and the reason for the
293  *	interrupt gone away before we get here.
294  *
295  *	OTOH, if we come through here twice without the receive code
296  *	having run in between, that's definitely wrong.  In such an
297  *	event, we would notice that chip interrupts haven't yet been
298  *	re-enabled and return UNCLAIMED, allowing the system's jabber
299  *	protect code (if any) to do its job.
300  */
301 static uint_t
302 rmc_comm_hi_intr(caddr_t arg)
303 {
304 	struct rmc_comm_state *rcs = (void *)arg;
305 	uint_t claim;
306 
307 	claim = DDI_INTR_UNCLAIMED;
308 	if (rcs->sd_state.cycid != CYCLIC_NONE) {
309 		mutex_enter(rcs->sd_state.hw_mutex);
310 		if (rcs->sd_state.hw_int_enabled) {
311 			rmc_comm_set_irq(rcs, B_FALSE);
312 			ddi_trigger_softintr(rcs->sd_state.softid);
313 			claim = DDI_INTR_CLAIMED;
314 		}
315 		mutex_exit(rcs->sd_state.hw_mutex);
316 	}
317 	return (claim);
318 }
319 
320 /*
321  * Packet receive handler
322  *
323  * This routine should be called from the low-level softint, or the
324  * cyclic callback, or rmc_comm_cmd() (for polled operation), with the
325  * low-level mutex already held.
326  */
327 void
328 rmc_comm_serdev_receive(struct rmc_comm_state *rcs)
329 {
330 	uint8_t data;
331 
332 	DPRINTF(rcs, DSER, (CE_CONT, "serdev_receive: soft int handler\n"));
333 
334 	/*
335 	 * Check for access faults before starting the receive
336 	 * loop (we don't want to cause bus errors or suchlike
337 	 * unpleasantness in the event that the SIO has died).
338 	 */
339 	if (!rmc_comm_faulty(rcs)) {
340 
341 		char *rx_buf = rcs->sd_state.serdev_rx_buf;
342 		uint16_t rx_buflen = 0;
343 
344 		/*
345 		 * Read bytes from the FIFO until they're all gone
346 		 * or our buffer overflows (which must be an error)
347 		 */
348 
349 		/*
350 		 * At the moment, the receive buffer is overwritten any
351 		 * time data is received from the serial device.
352 		 * This should not pose problems (probably!) as the data
353 		 * protocol is half-duplex
354 		 * Otherwise, a circular buffer must be implemented!
355 		 */
356 		mutex_enter(rcs->sd_state.hw_mutex);
357 		while (sio_data_ready(rcs)) {
358 			data = sio_get_reg(rcs, SIO_RXD);
359 			rx_buf[rx_buflen++] = data;
360 			if (rx_buflen >= SIO_MAX_RXBUF_SIZE)
361 				break;
362 		}
363 		rcs->sd_state.serdev_rx_count = rx_buflen;
364 
365 		DATASCOPE(rcs, 'R', rx_buf, rx_buflen)
366 
367 		rmc_comm_set_irq(rcs, B_TRUE);
368 		mutex_exit(rcs->sd_state.hw_mutex);
369 
370 		/*
371 		 * call up the data protocol receive handler
372 		 */
373 		rmc_comm_dp_drecv(rcs, (uint8_t *)rx_buf, rx_buflen);
374 	}
375 }
376 
377 /*
378  * Low-level softint handler
379  *
380  * This routine should be triggered whenever there's a byte to be read
381  */
382 static uint_t
383 rmc_comm_softint(caddr_t arg)
384 {
385 	struct rmc_comm_state *rcs = (void *)arg;
386 
387 	mutex_enter(rcs->dp_state.dp_mutex);
388 	rmc_comm_serdev_receive(rcs);
389 	mutex_exit(rcs->dp_state.dp_mutex);
390 	return (DDI_INTR_CLAIMED);
391 }
392 
393 /*
394  * Cyclic handler: just calls the receive routine, in case interrupts
395  * are not being delivered and in order to handle command timeout
396  */
397 static void
398 rmc_comm_cyclic(void *arg)
399 {
400 	struct rmc_comm_state *rcs = (void *)arg;
401 
402 	mutex_enter(rcs->dp_state.dp_mutex);
403 	rmc_comm_serdev_receive(rcs);
404 	mutex_exit(rcs->dp_state.dp_mutex);
405 }
406 
407 /*
408  * Serial protocol
409  *
410  * This routine builds a command and sets it in progress.
411  */
412 void
413 rmc_comm_serdev_send(struct rmc_comm_state *rcs, char *buf, int buflen)
414 {
415 	uint8_t *p;
416 	uint8_t status;
417 
418 	/*
419 	 * Check and update the SIO h/w fault status before accessing
420 	 * the chip registers.  If there's a (new or previous) fault,
421 	 * we'll run through the protocol but won't really touch the
422 	 * hardware and all commands will timeout.  If a previously
423 	 * discovered fault has now gone away (!), then we can (try to)
424 	 * proceed with the new command (probably a probe).
425 	 */
426 	sio_check_fault_status(rcs);
427 
428 	/*
429 	 * Send the command now by stuffing the packet into the Tx FIFO.
430 	 */
431 	DATASCOPE(rcs, 'S', buf, buflen)
432 
433 	mutex_enter(rcs->sd_state.hw_mutex);
434 	p = (uint8_t *)buf;
435 	while (p < (uint8_t *)&buf[buflen]) {
436 
437 		/*
438 		 * before writing to the TX holding register, we make sure that
439 		 * it is empty. In this case, there will be no chance to
440 		 * overflow the serial device FIFO (but, on the other hand,
441 		 * it may introduce some latency)
442 		 */
443 		status = sio_get_reg(rcs, SIO_LSR);
444 		while ((status & SIO_LSR_XHRE) == 0) {
445 			drv_usecwait(100);
446 			status = sio_get_reg(rcs, SIO_LSR);
447 		}
448 		sio_put_reg(rcs, SIO_TXD, *p++);
449 	}
450 	mutex_exit(rcs->sd_state.hw_mutex);
451 }
452 
453 /*
454  * wait for the tx fifo to drain - used for urgent nowait requests
455  */
456 void
457 rmc_comm_serdev_drain(struct rmc_comm_state *rcs)
458 {
459 	uint8_t status;
460 
461 	mutex_enter(rcs->sd_state.hw_mutex);
462 	status = sio_get_reg(rcs, SIO_LSR);
463 	while ((status & SIO_LSR_XHRE) == 0) {
464 		drv_usecwait(100);
465 		status = sio_get_reg(rcs, SIO_LSR);
466 	}
467 	mutex_exit(rcs->sd_state.hw_mutex);
468 }
469 
470 /*
471  * Hardware setup - put the SIO chip in the required operational
472  * state,  with all our favourite parameters programmed correctly.
473  * This routine leaves all SIO interrupts disabled.
474  */
475 
476 static void
477 rmc_comm_hw_reset(struct rmc_comm_state *rcs)
478 {
479 	uint16_t divisor;
480 
481 	/*
482 	 * Disable interrupts, soft reset Tx and Rx circuitry,
483 	 * reselect standard modes (bits/char, parity, etc).
484 	 */
485 	rmc_comm_set_irq(rcs, B_FALSE);
486 	sio_put_reg(rcs, SIO_FCR, SIO_FCR_RXSR | SIO_FCR_TXSR);
487 	sio_put_reg(rcs, SIO_LCR, SIO_LCR_STD);
488 
489 	/*
490 	 * Select the proper baud rate; if the value is invalid
491 	 * (presumably 0, i.e. not specified, but also if the
492 	 * "baud" property is set to some silly value), we assume
493 	 * the default.
494 	 */
495 	if (rcs->baud < SIO_BAUD_MIN || rcs->baud > SIO_BAUD_MAX) {
496 		divisor = SIO_BAUD_TO_DIVISOR(SIO_BAUD_DEFAULT) *
497 		    rcs->baud_divisor_factor;
498 	} else {
499 		divisor = SIO_BAUD_TO_DIVISOR(rcs->baud) *
500 		    rcs->baud_divisor_factor;
501 	}
502 
503 	/*
504 	 * According to the datasheet, it is forbidden for the divisor
505 	 * register to be zero.  So when loading the register in two
506 	 * steps, we have to make sure that the temporary value formed
507 	 * between loads is nonzero.  However, we can't rely on either
508 	 * half already having a nonzero value, as the datasheet also
509 	 * says that these registers are indeterminate after a reset!
510 	 * So, we explicitly set the low byte to a non-zero value first;
511 	 * then we can safely load the high byte, and then the correct
512 	 * value for the low byte, without the result ever being zero.
513 	 */
514 	sio_put_reg(rcs, SIO_BSR, SIO_BSR_BANK1);
515 	sio_put_reg(rcs, SIO_LBGDL, 0xff);
516 	sio_put_reg(rcs, SIO_LBGDH, divisor >> 8);
517 	sio_put_reg(rcs, SIO_LBGDL, divisor & 0xff);
518 	sio_put_reg(rcs, SIO_BSR, SIO_BSR_BANK0);
519 
520 	/*
521 	 * Program the remaining device registers as required
522 	 */
523 	sio_put_reg(rcs, SIO_MCR, SIO_MCR_STD);
524 	sio_put_reg(rcs, SIO_FCR, SIO_FCR_STD);
525 }
526 
527 /*
528  * Higher-level setup & teardown
529  */
530 static void
531 rmc_comm_offline(struct rmc_comm_state *rcs)
532 {
533 	if (rcs->sd_state.sio_handle != NULL)
534 		ddi_regs_map_free(&rcs->sd_state.sio_handle);
535 	rcs->sd_state.sio_handle = NULL;
536 	rcs->sd_state.sio_regs = NULL;
537 }
538 
539 static int
540 rmc_comm_online(struct rmc_comm_state *rcs, dev_info_t *dip)
541 {
542 	ddi_acc_handle_t h;
543 	caddr_t p;
544 	int nregs;
545 	int err;
546 
547 	if (ddi_dev_nregs(dip, &nregs) != DDI_SUCCESS)
548 		nregs = 0;
549 	switch (nregs) {
550 	default:
551 	case 1:
552 		/*
553 		 *  regset 0 represents the SIO operating registers
554 		 */
555 		err = ddi_regs_map_setup(dip, 0, &p, 0, 0,
556 		    rmc_comm_dev_acc_attr, &h);
557 		if (err != DDI_SUCCESS)
558 			return (EIO);
559 		rcs->sd_state.sio_handle = h;
560 		rcs->sd_state.sio_regs = (void *)p;
561 		break;
562 	case 0:
563 		/*
564 		 *  If no registers are defined, succeed vacuously;
565 		 *  commands will be accepted, but we fake the accesses.
566 		 */
567 		break;
568 	}
569 
570 	/*
571 	 * Now that the registers are mapped, we can initialise the SIO h/w
572 	 */
573 	rmc_comm_hw_reset(rcs);
574 	return (0);
575 }
576 
577 
578 /*
579  * Initialization of the serial device (data structure, mutex, cv, hardware
580  * and so on). It is called from the attach routine.
581  */
582 
583 int
584 rmc_comm_serdev_init(struct rmc_comm_state *rcs, dev_info_t *dip)
585 {
586 	cyc_handler_t cychand;
587 	cyc_time_t cyctime;
588 	int err = DDI_SUCCESS;
589 
590 	rcs->sd_state.cycid = CYCLIC_NONE;
591 
592 	/*
593 	 *  Online the hardware ...
594 	 */
595 	err = rmc_comm_online(rcs, dip);
596 	if (err != 0)
597 		return (-1);
598 
599 	/*
600 	 * call ddi_get_soft_iblock_cookie() to retrieve the
601 	 * the interrupt block cookie so that the mutexes are initialized
602 	 * before adding the interrupt (to avoid a potential race condition).
603 	 */
604 
605 	err = ddi_get_soft_iblock_cookie(dip, DDI_SOFTINT_LOW,
606 	    &rcs->dp_state.dp_iblk);
607 	if (err != DDI_SUCCESS)
608 		return (-1);
609 
610 	err = ddi_get_iblock_cookie(dip, 0, &rcs->sd_state.hw_iblk);
611 	if (err != DDI_SUCCESS)
612 		return (-1);
613 
614 	/*
615 	 * initialize mutex here before adding hw/sw interrupt handlers
616 	 */
617 	mutex_init(rcs->dp_state.dp_mutex, NULL, MUTEX_DRIVER,
618 	    rcs->dp_state.dp_iblk);
619 
620 	mutex_init(rcs->sd_state.hw_mutex, NULL, MUTEX_DRIVER,
621 	    rcs->sd_state.hw_iblk);
622 
623 	/*
624 	 * Install soft and hard interrupt handler(s)
625 	 *
626 	 * the soft intr. handler will need the data protocol lock (dp_mutex)
627 	 * So, data protocol mutex and iblock cookie are created/initialized
628 	 * here
629 	 */
630 
631 	err = ddi_add_softintr(dip, DDI_SOFTINT_LOW, &rcs->sd_state.softid,
632 	    &rcs->dp_state.dp_iblk, NULL, rmc_comm_softint, (caddr_t)rcs);
633 	if (err != DDI_SUCCESS) {
634 		mutex_destroy(rcs->dp_state.dp_mutex);
635 		mutex_destroy(rcs->sd_state.hw_mutex);
636 		return (-1);
637 	}
638 
639 	/*
640 	 * hardware interrupt
641 	 */
642 
643 	if (rcs->sd_state.sio_handle != NULL) {
644 		err = ddi_add_intr(dip, 0, &rcs->sd_state.hw_iblk, NULL,
645 			rmc_comm_hi_intr, (caddr_t)rcs);
646 
647 		/*
648 		 * did we successfully install the h/w interrupt handler?
649 		 */
650 		if (err != DDI_SUCCESS) {
651 			ddi_remove_softintr(rcs->sd_state.softid);
652 			mutex_destroy(rcs->dp_state.dp_mutex);
653 			mutex_destroy(rcs->sd_state.hw_mutex);
654 			return (-1);
655 		}
656 	}
657 
658 
659 	/*
660 	 * Start cyclic callbacks
661 	 */
662 
663 	cychand.cyh_func = rmc_comm_cyclic;
664 	cychand.cyh_arg = rcs;
665 	cychand.cyh_level = CY_LOW_LEVEL;
666 	cyctime.cyt_when = 0;			/* from the next second	*/
667 	cyctime.cyt_interval = 5*RMC_COMM_ONE_SEC; /* call at 5s intervals */
668 	mutex_enter(&cpu_lock);
669 	rcs->sd_state.cycid = cyclic_add(&cychand, &cyctime);
670 	mutex_exit(&cpu_lock);
671 
672 	return (0);
673 }
674 
675 /*
676  * Termination of the serial device (data structure, mutex, cv, hardware
677  * and so on). It is called from the detach routine.
678  */
679 
680 void
681 rmc_comm_serdev_fini(struct rmc_comm_state *rcs, dev_info_t *dip)
682 {
683 	rmc_comm_hw_reset(rcs);
684 
685 	if (rcs->sd_state.cycid != CYCLIC_NONE) {
686 		mutex_enter(&cpu_lock);
687 		cyclic_remove(rcs->sd_state.cycid);
688 		mutex_exit(&cpu_lock);
689 
690 		if (rcs->sd_state.sio_handle != NULL)
691 			ddi_remove_intr(dip, 0, rcs->sd_state.hw_iblk);
692 
693 		ddi_remove_softintr(rcs->sd_state.softid);
694 
695 		mutex_destroy(rcs->sd_state.hw_mutex);
696 
697 		mutex_destroy(rcs->dp_state.dp_mutex);
698 	}
699 	rmc_comm_offline(rcs);
700 }
701 
702 /*
703  * device driver entry routines (init/fini, attach/detach, ...)
704  */
705 
706 /*
707  *  Clean up on detach or failure of attach
708  */
709 static void
710 rmc_comm_unattach(struct rmc_comm_state *rcs, dev_info_t *dip, int instance,
711     boolean_t drvi_init, boolean_t dp_init, boolean_t sd_init)
712 {
713 	if (rcs != NULL) {
714 		/*
715 		 * disable interrupts now
716 		 */
717 		rmc_comm_set_irq(rcs, B_FALSE);
718 
719 		/*
720 		 * driver interface termination (if it has been initialized)
721 		 */
722 		if (drvi_init)
723 			rmc_comm_drvintf_fini(rcs);
724 
725 		/*
726 		 * data protocol termination (if it has been initialized)
727 		 */
728 		if (dp_init)
729 			rmc_comm_dp_fini(rcs);
730 
731 		/*
732 		 * serial device termination (if it has been initialized)
733 		 */
734 		if (sd_init)
735 			rmc_comm_serdev_fini(rcs, dip);
736 
737 		ddi_set_driver_private(dip, NULL);
738 	}
739 	ddi_soft_state_free(rmc_comm_statep, instance);
740 }
741 
742 /*
743  *  Autoconfiguration routines
744  */
745 
746 static int
747 rmc_comm_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
748 {
749 	struct rmc_comm_state *rcs = NULL;
750 	sig_state_t *current_sgn_p;
751 	int instance;
752 
753 	/*
754 	 * only allow one instance
755 	 */
756 	instance = ddi_get_instance(dip);
757 	if (instance != 0)
758 		return (DDI_FAILURE);
759 
760 	switch (cmd) {
761 	default:
762 		return (DDI_FAILURE);
763 
764 	case DDI_RESUME:
765 		if ((rcs = rmc_comm_getstate(dip, instance,
766 		    "rmc_comm_attach")) == NULL)
767 			return (DDI_FAILURE);	/* this "can't happen" */
768 
769 		rmc_comm_hw_reset(rcs);
770 		rmc_comm_set_irq(rcs, B_TRUE);
771 		rcs->dip = dip;
772 
773 		mutex_enter(&tod_lock);
774 		if (watchdog_enable && tod_ops.tod_set_watchdog_timer != NULL &&
775 		    watchdog_was_active) {
776 			(void) tod_ops.tod_set_watchdog_timer(0);
777 		}
778 		mutex_exit(&tod_lock);
779 
780 		mutex_enter(rcs->dp_state.dp_mutex);
781 		dp_reset(rcs, INITIAL_SEQID, 1, 1);
782 		mutex_exit(rcs->dp_state.dp_mutex);
783 
784 		current_sgn_p = (sig_state_t *)modgetsymvalue(
785 			"current_sgn", 0);
786 		if ((current_sgn_p != NULL) &&
787 			(current_sgn_p->state_t.sig != 0)) {
788 			CPU_SIGNATURE(current_sgn_p->state_t.sig,
789 				current_sgn_p->state_t.state,
790 				current_sgn_p->state_t.sub_state, -1);
791 		}
792 		return (DDI_SUCCESS);
793 
794 	case DDI_ATTACH:
795 		break;
796 	}
797 
798 	/*
799 	 *  Allocate the soft-state structure
800 	 */
801 	if (ddi_soft_state_zalloc(rmc_comm_statep, instance) != DDI_SUCCESS)
802 		return (DDI_FAILURE);
803 	if ((rcs = rmc_comm_getstate(dip, instance, "rmc_comm_attach")) ==
804 	    NULL) {
805 		rmc_comm_unattach(rcs, dip, instance, 0, 0, 0);
806 		return (DDI_FAILURE);
807 	}
808 	ddi_set_driver_private(dip, rcs);
809 
810 	rcs->dip = NULL;
811 
812 	/*
813 	 *  Set various options from .conf properties
814 	 */
815 	rcs->baud = ddi_prop_get_int(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
816 	    "baud-rate", 0);
817 	rcs->debug = ddi_prop_get_int(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
818 	    "debug", 0);
819 
820 	/*
821 	 * the baud divisor factor tells us how to scale the result of
822 	 * the SIO_BAUD_TO_DIVISOR macro for platforms which do not
823 	 * use the standard 24MHz uart clock
824 	 */
825 	rcs->baud_divisor_factor = ddi_prop_get_int(DDI_DEV_T_ANY, dip,
826 	    DDI_PROP_DONTPASS, "baud-divisor-factor", SIO_BAUD_DIVISOR_MIN);
827 
828 	/*
829 	 * try to be reasonable if the scale factor contains a silly value
830 	 */
831 	if ((rcs->baud_divisor_factor < SIO_BAUD_DIVISOR_MIN) ||
832 	    (rcs->baud_divisor_factor > SIO_BAUD_DIVISOR_MAX))
833 	    rcs->baud_divisor_factor = SIO_BAUD_DIVISOR_MIN;
834 
835 	/*
836 	 * initialize serial device
837 	 */
838 	if (rmc_comm_serdev_init(rcs, dip) != 0) {
839 		rmc_comm_unattach(rcs, dip, instance, 0, 0, 0);
840 		return (DDI_FAILURE);
841 	}
842 
843 	/*
844 	 * initialize data protocol
845 	 */
846 	rmc_comm_dp_init(rcs);
847 
848 	/*
849 	 * initialize driver interface
850 	 */
851 	if (rmc_comm_drvintf_init(rcs) != 0) {
852 		rmc_comm_unattach(rcs, dip, instance, 0, 1, 1);
853 		return (DDI_FAILURE);
854 	}
855 
856 	/*
857 	 *  Initialise devinfo-related fields
858 	 */
859 	rcs->majornum = ddi_driver_major(dip);
860 	rcs->instance = instance;
861 	rcs->dip = dip;
862 
863 	/*
864 	 * enable interrupts now
865 	 */
866 	rmc_comm_set_irq(rcs, B_TRUE);
867 
868 	/*
869 	 *  All done, report success
870 	 */
871 	ddi_report_dev(dip);
872 	mutex_enter(&rmc_comm_attach_lock);
873 	rcs->is_attached = B_TRUE;
874 	mutex_exit(&rmc_comm_attach_lock);
875 	return (DDI_SUCCESS);
876 }
877 
878 static int
879 rmc_comm_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
880 {
881 	struct rmc_comm_state *rcs;
882 	int instance;
883 
884 	instance = ddi_get_instance(dip);
885 	if ((rcs = rmc_comm_getstate(dip, instance, "rmc_comm_detach")) == NULL)
886 		return (DDI_FAILURE);	/* this "can't happen" */
887 
888 	switch (cmd) {
889 	case DDI_SUSPEND:
890 		mutex_enter(&tod_lock);
891 		if (watchdog_enable && watchdog_activated &&
892 		    tod_ops.tod_clear_watchdog_timer != NULL) {
893 			watchdog_was_active = 1;
894 			(void) tod_ops.tod_clear_watchdog_timer();
895 		} else {
896 			watchdog_was_active = 0;
897 		}
898 		mutex_exit(&tod_lock);
899 
900 		rcs->dip = NULL;
901 		rmc_comm_hw_reset(rcs);
902 
903 		return (DDI_SUCCESS);
904 
905 	case DDI_DETACH:
906 		/*
907 		 * reject detach if any client(s) still registered
908 		 */
909 		mutex_enter(&rmc_comm_attach_lock);
910 		if (rcs->n_registrations != 0) {
911 			mutex_exit(&rmc_comm_attach_lock);
912 			return (DDI_FAILURE);
913 		}
914 		/*
915 		 * Committed to complete the detach;
916 		 * mark as no longer attached, to prevent new clients
917 		 * registering (as part of a coincident attach)
918 		 */
919 		rcs->is_attached = B_FALSE;
920 		mutex_exit(&rmc_comm_attach_lock);
921 		rmc_comm_unattach(rcs, dip, instance, 1, 1, 1);
922 		return (DDI_SUCCESS);
923 
924 	default:
925 		return (DDI_FAILURE);
926 	}
927 }
928 
929 /*ARGSUSED*/
930 static int
931 rmc_comm_reset(dev_info_t *dip, ddi_reset_cmd_t cmd)
932 {
933 	struct rmc_comm_state *rcs;
934 
935 	if ((rcs = rmc_comm_getstate(dip, -1, "rmc_comm_reset")) == NULL)
936 		return (DDI_FAILURE);
937 	rmc_comm_hw_reset(rcs);
938 	return (DDI_SUCCESS);
939 }
940 
941 /*
942  * System interface structures
943  */
944 static struct dev_ops rmc_comm_dev_ops =
945 {
946 	DEVO_REV,
947 	0,				/* refcount		*/
948 	nodev,				/* getinfo		*/
949 	nulldev,			/* identify		*/
950 	nulldev,			/* probe		*/
951 	rmc_comm_attach,		/* attach		*/
952 	rmc_comm_detach,		/* detach		*/
953 	rmc_comm_reset,			/* reset		*/
954 	(struct cb_ops *)NULL,		/* driver operations	*/
955 	(struct bus_ops *)NULL,		/* bus operations	*/
956 	nulldev 			/* power()		*/
957 };
958 
959 static struct modldrv modldrv =
960 {
961 	&mod_driverops,
962 	"rmc_comm driver, v%I%",
963 	&rmc_comm_dev_ops
964 };
965 
966 static struct modlinkage modlinkage =
967 {
968 	MODREV_1,
969 	{
970 		&modldrv,
971 		NULL
972 	}
973 };
974 
975 /*
976  *  Dynamic loader interface code
977  */
978 int
979 _init(void)
980 {
981 	int err;
982 
983 	mutex_init(&rmc_comm_attach_lock, NULL, MUTEX_DRIVER, NULL);
984 	err = ddi_soft_state_init(&rmc_comm_statep,
985 		sizeof (struct rmc_comm_state), 0);
986 	if (err == DDI_SUCCESS)
987 		if ((err = mod_install(&modlinkage)) != 0) {
988 			ddi_soft_state_fini(&rmc_comm_statep);
989 		}
990 	if (err != DDI_SUCCESS)
991 		mutex_destroy(&rmc_comm_attach_lock);
992 	return (err);
993 }
994 
995 int
996 _info(struct modinfo *mip)
997 {
998 	return (mod_info(&modlinkage, mip));
999 }
1000 
1001 int
1002 _fini(void)
1003 {
1004 	int err;
1005 
1006 	if ((err = mod_remove(&modlinkage)) == 0) {
1007 		ddi_soft_state_fini(&rmc_comm_statep);
1008 		rmc_comm_major = NOMAJOR;
1009 		mutex_destroy(&rmc_comm_attach_lock);
1010 	}
1011 	return (err);
1012 }
1013