Home | History | Annotate | Line # | Download | only in mpcsa
      1 /*	$Id: mpcsa_usart.c,v 1.4 2012/10/27 17:17:48 chs Exp $	*/
      2 /*	$NetBSD: mpcsa_usart.c,v 1.4 2012/10/27 17:17:48 chs Exp $	*/
      3 
      4 /*
      5  * Copyright (c) 2007 Embedtronics Oy. All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  * SUCH DAMAGE.
     27  */
     28 
     29 #include <sys/cdefs.h>
     30 __KERNEL_RCSID(0, "$NetBSD: mpcsa_usart.c,v 1.4 2012/10/27 17:17:48 chs Exp $");
     31 
     32 #include <sys/types.h>
     33 #include <sys/param.h>
     34 #include <sys/systm.h>
     35 #include <sys/kernel.h>
     36 #include <sys/device.h>
     37 #include <sys/proc.h>
     38 #include <arm/at91/at91reg.h>
     39 #include <arm/at91/at91var.h>
     40 #include <arm/at91/at91usartvar.h>
     41 #include <arm/at91/at91piovar.h>
     42 #include <arm/at91/at91rm9200reg.h>
     43 #include <evbarm/mpcsa/mpcsa_io.h>
     44 #include <evbarm/mpcsa/mpcsa_leds_var.h>
     45 #include <sys/unistd.h>
     46 
     47 #ifdef MPCSA_USART_DEBUG
     48 int mpcsa_usart_debug = MPCSA_USART_DEBUG;
     49 #define DPRINTFN(n,x)	if (mpcsa_usart_debug>(n)) printf x;
     50 #else
     51 #define DPRINTFN(n,x)
     52 #endif
     53 
     54 struct at91pio_softc;
     55 
     56 struct mpcsa_usart_softc {
     57 	struct at91usart_softc	sc_dev;
     58 	struct at91pio_softc	*sc_pioa, *sc_piob, *sc_piod;
     59 	void *sc_cts_ih;
     60 	int sc_tx_busy, sc_rx_busy;
     61 };
     62 
     63 static int mpcsa_usart_match(device_t, cfdata_t, void *);
     64 static void mpcsa_usart_attach(device_t, device_t, void *);
     65 
     66 CFATTACH_DECL_NEW(mpcsa_usart, sizeof(struct mpcsa_usart_softc),
     67 	      mpcsa_usart_match, mpcsa_usart_attach, NULL, NULL);
     68 
     69 static int mpcsa_usart_enable(struct at91usart_softc *sc);
     70 static int mpcsa_usart_disable(struct at91usart_softc *sc);
     71 static void mpcsa_usart_hwflow(struct at91usart_softc *sc, int cflags);
     72 static void mpcsa_usart_start_tx(struct at91usart_softc *sc);
     73 static void mpcsa_usart_stop_tx(struct at91usart_softc *sc);
     74 static void mpcsa_usart_rx_started(struct at91usart_softc *sc);
     75 static void mpcsa_usart_rx_stopped(struct at91usart_softc *sc);
     76 static void mpcsa_usart_rx_rts_ctl(struct at91usart_softc *sc, int enabled);
     77 
     78 static int mpcsa_gsm_cts_intr(void *);
     79 
     80 static __inline int
     81 led_num(struct mpcsa_usart_softc *mpsc)
     82 {
     83 	return (mpsc->sc_dev.sc_pid == PID_US3 ? LED_GSM : LED_SER1 + mpsc->sc_dev.sc_pid - PID_US0);
     84 }
     85 
     86 static __inline void
     87 comm_led(struct mpcsa_usart_softc *mpsc, int count)
     88 {
     89 	mpcsa_comm_led(led_num(mpsc), count);
     90 }
     91 
     92 static __inline void
     93 conn_led(struct mpcsa_usart_softc *mpsc, int count)
     94 {
     95 	mpcsa_conn_led(led_num(mpsc), count);
     96 }
     97 
     98 static int
     99 mpcsa_usart_match(device_t parent, cfdata_t match, void *aux)
    100 {
    101 	if (strcmp(match->cf_name, "at91usart") == 0 && strcmp(match->cf_atname, "mpcsa_usart") == 0)
    102 		return 2;
    103 	return 0;
    104 }
    105 
    106 
    107 static void
    108 mpcsa_usart_attach(device_t parent, device_t self, void *aux)
    109 {
    110 	struct mpcsa_usart_softc *sc = device_private(self);
    111 	struct at91bus_attach_args *sa = aux;
    112 
    113 	sc->sc_dev.sc_dev = self;
    114 
    115 	// initialize softc
    116 	if ((sc->sc_pioa = at91pio_sc(AT91_PIOA)) == NULL) {
    117 		printf("no PIOA!\n");
    118 		return;
    119 	}
    120 	if ((sc->sc_piob = at91pio_sc(AT91_PIOB)) == NULL) {
    121 		printf("no PIOB!\n");
    122 		return;
    123 	}
    124 	if ((sc->sc_piod = at91pio_sc(AT91_PIOD)) == NULL) {
    125 		printf("no PIOD!\n");
    126 		return;
    127 	}
    128 
    129 	// calculate unit number...
    130 	switch (sa->sa_pid) {
    131 	case PID_US0:
    132 	case PID_US1:
    133 	case PID_US2:
    134 	case PID_US3:
    135 		sc->sc_dev.enable = mpcsa_usart_enable;
    136 		sc->sc_dev.disable = mpcsa_usart_disable;
    137 		sc->sc_dev.hwflow = mpcsa_usart_hwflow;
    138 		sc->sc_dev.start_tx = mpcsa_usart_start_tx;
    139 		sc->sc_dev.stop_tx = mpcsa_usart_stop_tx;
    140 		sc->sc_dev.rx_started = mpcsa_usart_rx_started;
    141 		sc->sc_dev.rx_stopped = mpcsa_usart_rx_stopped;
    142 		sc->sc_dev.rx_rts_ctl = mpcsa_usart_rx_rts_ctl;
    143 		break;
    144 	}
    145 
    146 	/* configure pins */
    147 	switch (sa->sa_pid) {
    148 	case PID_US0:
    149 		at91pio_set(sc->sc_piob, PB_RTS1);
    150 		at91pio_set(sc->sc_piod, PD_DTR1);
    151 		at91pio_in(sc->sc_piob, PB_CTS1);
    152 		at91pio_out(sc->sc_piob, PB_RTS1);
    153 		at91pio_in(sc->sc_piod, PD_DSR1);
    154 		at91pio_out(sc->sc_piod, PD_DTR1);
    155 		at91pio_per(sc->sc_piob, PB_CTS1, -1);
    156 		at91pio_per(sc->sc_piob, PB_RTS1, -1);
    157 		at91pio_per(sc->sc_piod, PD_DSR1, -1);
    158 		at91pio_per(sc->sc_piod, PD_DTR1, -1);
    159 		break;
    160 	case PID_US1:
    161 		at91pio_set(sc->sc_piob, PB_RTS2);
    162 		at91pio_set(sc->sc_piod, PD_DTR2);
    163 		at91pio_in(sc->sc_piob, PB_CTS2);
    164 		at91pio_out(sc->sc_piob, PB_RTS2);
    165 		at91pio_in(sc->sc_piod, PD_DSR2);
    166 		at91pio_out(sc->sc_piod, PD_DTR2);
    167 		at91pio_per(sc->sc_piob, PB_CTS2, -1);
    168 		at91pio_per(sc->sc_piob, PB_RTS2, -1);
    169 		at91pio_per(sc->sc_piod, PD_DSR2, -1);
    170 		at91pio_per(sc->sc_piod, PD_DTR2, -1);
    171 		break;
    172 	case PID_US2:
    173 		at91pio_set(sc->sc_piob, PB_RTS3);
    174 		at91pio_set(sc->sc_piod, PD_DTR3);
    175 		at91pio_in(sc->sc_piob, PB_CTS3);
    176 		at91pio_out(sc->sc_piob, PB_RTS3);
    177 		at91pio_in(sc->sc_piod, PD_DSR3);
    178 		at91pio_out(sc->sc_piod, PD_DTR3);
    179 		at91pio_per(sc->sc_piob, PB_CTS3, -1);
    180 		at91pio_per(sc->sc_piob, PB_RTS3, -1);
    181 		at91pio_per(sc->sc_piod, PD_DSR3, -1);
    182 		at91pio_per(sc->sc_piod, PD_DTR3, -1);
    183 		break;
    184 	case PID_US3:
    185 		/* configure pin states... */
    186 		at91pio_clear(sc->sc_pioa, PA_GSMON);
    187 		at91pio_set(sc->sc_pioa, PA_GSMOFF);
    188 		at91pio_set(sc->sc_piob, PB_RTS4);
    189 		at91pio_set(sc->sc_piod, PD_DTR4);
    190 
    191 		/* configure pin directions.. */
    192 		at91pio_out(sc->sc_pioa, PA_GSMOFF);
    193 		at91pio_out(sc->sc_pioa, PA_GSMON);
    194 		at91pio_in(sc->sc_pioa, PA_TXD4);
    195 		at91pio_in(sc->sc_piob, PB_RTS4);
    196 		at91pio_in(sc->sc_piob, PB_CTS4);
    197 		at91pio_in(sc->sc_piod, PD_DTR4);
    198 		at91pio_in(sc->sc_piod, PD_DSR4);
    199 		at91pio_in(sc->sc_piod, PD_DCD4);
    200 
    201 		/* make sure all related pins are configured as PIO */
    202 		at91pio_per(sc->sc_pioa, PA_GSMOFF, -1);
    203 		at91pio_per(sc->sc_pioa, PA_GSMON, -1);
    204 		at91pio_per(sc->sc_pioa, PA_TXD4, -1);
    205 		at91pio_per(sc->sc_piob, PB_CTS4, -1);
    206 		at91pio_per(sc->sc_piob, PB_RTS4, -1);
    207 		at91pio_per(sc->sc_piod, PD_DSR4, -1);
    208 		at91pio_per(sc->sc_piod, PD_DTR4, -1);
    209 		at91pio_per(sc->sc_piod, PD_DCD4, -1);
    210 		break;
    211 	}
    212 
    213 	// and call common routine
    214 	at91usart_attach_subr(&sc->sc_dev, sa);
    215 }
    216 
    217 static int
    218 mpcsa_usart_enable(struct at91usart_softc *dev)
    219 {
    220 	struct mpcsa_usart_softc *sc = (struct mpcsa_usart_softc *)dev;
    221 	conn_led(sc, 1);
    222 	switch (sc->sc_dev.sc_pid) {
    223 	case PID_US3:
    224 		/* turn gsm on */
    225 		at91pio_clear(sc->sc_pioa, PA_GSMOFF);
    226 		kpause("gsmond", false, 4 * hz, NULL);
    227 		at91pio_set(sc->sc_pioa, PA_GSMON);
    228 		kpause("gsmon", false, 2 * hz, NULL);
    229 		at91pio_clear(sc->sc_pioa, PA_GSMON);
    230 		/* then attach pins to devices etc */
    231 		at91pio_per(sc->sc_pioa, PA_TXD4, 1);
    232 		at91pio_clear(sc->sc_piob, PB_RTS4);
    233 		at91pio_clear(sc->sc_piod, PD_DTR4);
    234 		at91pio_out(sc->sc_piob, PB_RTS4);
    235 		at91pio_out(sc->sc_piod, PD_DTR4);
    236 		/* catch CTS interrupt */
    237 		sc->sc_cts_ih = at91pio_intr_establish(sc->sc_piob, PB_CTS4,
    238 						       IPL_TTY, mpcsa_gsm_cts_intr,
    239 						       sc);
    240 		break;
    241 	}
    242 	return 0;
    243 }
    244 
    245 static int
    246 mpcsa_usart_disable(struct at91usart_softc *dev)
    247 {
    248 	struct mpcsa_usart_softc *sc = (struct mpcsa_usart_softc *)dev;
    249 	if (sc->sc_tx_busy || sc->sc_rx_busy) {
    250 		sc->sc_tx_busy = sc->sc_rx_busy = 0;
    251 		comm_led(sc, 1);
    252 	}
    253 	switch (sc->sc_dev.sc_pid) {
    254 	case PID_US3:
    255 		at91pio_intr_disestablish(sc->sc_piob, PB_CTS4, sc->sc_cts_ih);
    256 
    257 		at91pio_clear(sc->sc_pioa, PA_GSMON);
    258 		kpause("gsmoffd", false, (hz * 350 + 999) / 1000, NULL);
    259 
    260 		at91pio_per(sc->sc_pioa, PA_TXD4, -1);
    261 		at91pio_in(sc->sc_piob, PB_RTS4);
    262 		at91pio_in(sc->sc_piod, PD_DTR4);
    263 
    264 		at91pio_set(sc->sc_pioa, PA_GSMOFF);
    265 		kpause("gsmoff", false, hz * 4, NULL);
    266 		at91pio_clear(sc->sc_pioa, PA_GSMOFF);
    267 
    268 		break;
    269 	}
    270 	conn_led(sc, 0);
    271 	return 0;
    272 }
    273 
    274 static int mpcsa_gsm_cts_intr(void *cookie)
    275 {
    276 	struct mpcsa_usart_softc *sc = (struct mpcsa_usart_softc *)cookie;
    277 	if (ISSET(sc->sc_dev.sc_swflags, TIOCFLAG_CRTSCTS)) {
    278 		/* hardware flow control is enabled */
    279 		if (!(PIOB_READ(PIO_PDSR) & (1U << PB_CTS4))) {
    280 			if (bus_space_read_4(sc->sc_dev.sc_iot, sc->sc_dev.sc_ioh,
    281 					     US_PDC + PDC_TCR) && !sc->sc_tx_busy) {
    282 				sc->sc_tx_busy = 1;
    283 				if (!sc->sc_rx_busy)
    284 					comm_led(sc, INFINITE_BLINK);
    285 			}
    286 
    287 			bus_space_write_4(sc->sc_dev.sc_iot, sc->sc_dev.sc_ioh,
    288 					  US_PDC + PDC_PTCR, PDC_PTCR_TXTEN);
    289 			SET(sc->sc_dev.sc_ier, US_CSR_TXEMPTY | US_CSR_ENDTX);
    290 			bus_space_write_4(sc->sc_dev.sc_iot, sc->sc_dev.sc_ioh,
    291 					  US_IER, US_CSR_ENDTX);
    292 		} else {
    293 			bus_space_write_4(sc->sc_dev.sc_iot, sc->sc_dev.sc_ioh,
    294 					  US_PDC + PDC_PTCR, PDC_PTCR_TXTDIS);
    295 			if (sc->sc_tx_busy) {
    296 				sc->sc_tx_busy = 0;
    297 				if (!sc->sc_rx_busy)
    298 					comm_led(sc, 1);
    299 			}
    300 		}
    301 	}
    302 	return 0;
    303 }
    304 
    305 static void
    306 mpcsa_usart_hwflow(struct at91usart_softc *dev, int flags)
    307 {
    308 }
    309 
    310 static void
    311 mpcsa_usart_start_tx(struct at91usart_softc *sc)
    312 {
    313 	if (!ISSET(sc->sc_swflags, TIOCFLAG_CRTSCTS)
    314 	    || bus_space_read_4(sc->sc_iot, sc->sc_ioh, US_PDC + PDC_PTSR) & PDC_PTSR_TXTEN) {
    315 		bus_space_write_4(sc->sc_iot, sc->sc_ioh,
    316 				  US_PDC + PDC_PTCR, PDC_PTCR_TXTEN);
    317 		struct mpcsa_usart_softc *mpsc = (void*)sc;
    318 		if (!mpsc->sc_tx_busy) {
    319 			mpsc->sc_tx_busy = 1;
    320 			if (!mpsc->sc_rx_busy)
    321 				comm_led(mpsc, INFINITE_BLINK);
    322 		}
    323 		return;
    324 	}
    325 }
    326 
    327 static void
    328 mpcsa_usart_stop_tx(struct at91usart_softc *sc)
    329 {
    330 	struct mpcsa_usart_softc *mpsc = (void*)sc;
    331 	mpsc->sc_tx_busy = 0;
    332 	if (!mpsc->sc_rx_busy)
    333 		comm_led(mpsc, 1);
    334 	if (!ISSET(sc->sc_swflags, TIOCFLAG_CRTSCTS)) {
    335 		bus_space_write_4(sc->sc_iot, sc->sc_ioh,
    336 				  US_PDC + PDC_PTCR, PDC_PTCR_TXTDIS);
    337 	}
    338 }
    339 
    340 static void
    341 mpcsa_usart_rx_started(struct at91usart_softc *sc)
    342 {
    343 	struct mpcsa_usart_softc *mpsc = (void*)sc;
    344 	if (!mpsc->sc_rx_busy) {
    345 		mpsc->sc_rx_busy = 1;
    346 		if (!mpsc->sc_tx_busy)
    347 			comm_led(mpsc, INFINITE_BLINK);
    348 	}
    349 }
    350 
    351 static void
    352 mpcsa_usart_rx_stopped(struct at91usart_softc *sc)
    353 {
    354 	struct mpcsa_usart_softc *mpsc = (void*)sc;
    355 	mpsc->sc_rx_busy = 0;
    356 	if (!mpsc->sc_tx_busy)
    357 		comm_led(mpsc, 1);
    358 }
    359 
    360 static void
    361 mpcsa_usart_rx_rts_ctl(struct at91usart_softc *sc, int enabled)
    362 {
    363 	struct mpcsa_usart_softc *mpsc = (void*)sc;
    364 
    365 	switch (mpsc->sc_dev.sc_pid) {
    366 	case PID_US0:
    367 		if (enabled)
    368 			at91pio_set(mpsc->sc_piob, PB_RTS1);
    369 		else
    370 			at91pio_clear(mpsc->sc_piob, PB_RTS1);
    371 		break;
    372 
    373 	case PID_US1:
    374 		if (enabled)
    375 			at91pio_set(mpsc->sc_piob, PB_RTS2);
    376 		else
    377 			at91pio_clear(mpsc->sc_piob, PB_RTS2);
    378 		break;
    379 
    380 	case PID_US2:
    381 		if (enabled)
    382 			at91pio_set(mpsc->sc_piob, PB_RTS3);
    383 		else
    384 			at91pio_clear(mpsc->sc_piob, PB_RTS3);
    385 		break;
    386 
    387 	case PID_US3:
    388 		if (enabled)
    389 			at91pio_set(mpsc->sc_piob, PB_RTS4);
    390 		else
    391 			at91pio_clear(mpsc->sc_piob, PB_RTS4);
    392 		break;
    393 
    394 	}
    395 
    396 }
    397 
    398