1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575
|
/*-
* Copyright (c) 2014 Ganbold Tsagaankhuu <ganbold@freebsd.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/* Qualcomm MSM7K/8K uart driver */
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include "opt_ddb.h"
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/bus.h>
#include <sys/conf.h>
#include <sys/kdb.h>
#include <machine/bus.h>
#include <machine/fdt.h>
#include <dev/uart/uart.h>
#include <dev/uart/uart_cpu.h>
#include <dev/uart/uart_cpu_fdt.h>
#include <dev/uart/uart_bus.h>
#include <dev/uart/uart_dev_msm.h>
#include "uart_if.h"
#define DEF_CLK 7372800
#define GETREG(bas, reg) \
bus_space_read_4((bas)->bst, (bas)->bsh, (reg))
#define SETREG(bas, reg, value) \
bus_space_write_4((bas)->bst, (bas)->bsh, (reg), (value))
static int msm_uart_param(struct uart_bas *, int, int, int, int);
/*
* Low-level UART interface.
*/
static int msm_probe(struct uart_bas *bas);
static void msm_init(struct uart_bas *bas, int, int, int, int);
static void msm_term(struct uart_bas *bas);
static void msm_putc(struct uart_bas *bas, int);
static int msm_rxready(struct uart_bas *bas);
static int msm_getc(struct uart_bas *bas, struct mtx *mtx);
extern SLIST_HEAD(uart_devinfo_list, uart_devinfo) uart_sysdevs;
static int
msm_uart_param(struct uart_bas *bas, int baudrate, int databits,
int stopbits, int parity)
{
int ulcon;
ulcon = 0;
switch (databits) {
case 5:
ulcon |= (UART_DM_5_BPS << 4);
break;
case 6:
ulcon |= (UART_DM_6_BPS << 4);
break;
case 7:
ulcon |= (UART_DM_7_BPS << 4);
break;
case 8:
ulcon |= (UART_DM_8_BPS << 4);
break;
default:
return (EINVAL);
}
switch (parity) {
case UART_PARITY_NONE:
ulcon |= UART_DM_NO_PARITY;
break;
case UART_PARITY_ODD:
ulcon |= UART_DM_ODD_PARITY;
break;
case UART_PARITY_EVEN:
ulcon |= UART_DM_EVEN_PARITY;
break;
case UART_PARITY_SPACE:
ulcon |= UART_DM_SPACE_PARITY;
break;
case UART_PARITY_MARK:
default:
return (EINVAL);
}
switch (stopbits) {
case 1:
ulcon |= (UART_DM_SBL_1 << 2);
break;
case 2:
ulcon |= (UART_DM_SBL_2 << 2);
break;
default:
return (EINVAL);
}
uart_setreg(bas, UART_DM_MR2, ulcon);
/* Set 115200 for both TX and RX. */;
uart_setreg(bas, UART_DM_CSR, UART_DM_CSR_115200);
uart_barrier(bas);
return (0);
}
struct uart_ops uart_msm_ops = {
.probe = msm_probe,
.init = msm_init,
.term = msm_term,
.putc = msm_putc,
.rxready = msm_rxready,
.getc = msm_getc,
};
static int
msm_probe(struct uart_bas *bas)
{
return (0);
}
static void
msm_init(struct uart_bas *bas, int baudrate, int databits, int stopbits,
int parity)
{
if (bas->rclk == 0)
bas->rclk = DEF_CLK;
KASSERT(bas->rclk != 0, ("msm_init: Invalid rclk"));
/* Set default parameters */
msm_uart_param(bas, baudrate, databits, stopbits, parity);
/*
* Configure UART mode registers MR1 and MR2.
* Hardware flow control isn't supported.
*/
uart_setreg(bas, UART_DM_MR1, 0x0);
/* Reset interrupt mask register. */
uart_setreg(bas, UART_DM_IMR, 0);
/*
* Configure Tx and Rx watermarks configuration registers.
* TX watermark value is set to 0 - interrupt is generated when
* FIFO level is less than or equal to 0.
*/
uart_setreg(bas, UART_DM_TFWR, UART_DM_TFW_VALUE);
/* Set RX watermark value */
uart_setreg(bas, UART_DM_RFWR, UART_DM_RFW_VALUE);
/*
* Configure Interrupt Programming Register.
* Set initial Stale timeout value.
*/
uart_setreg(bas, UART_DM_IPR, UART_DM_STALE_TIMEOUT_LSB);
/* Disable IRDA mode */
uart_setreg(bas, UART_DM_IRDA, 0x0);
/*
* Configure and enable sim interface if required.
* Configure hunt character value in HCR register.
* Keep it in reset state.
*/
uart_setreg(bas, UART_DM_HCR, 0x0);
/* Issue soft reset command */
SETREG(bas, UART_DM_CR, UART_DM_RESET_TX);
SETREG(bas, UART_DM_CR, UART_DM_RESET_RX);
SETREG(bas, UART_DM_CR, UART_DM_RESET_ERROR_STATUS);
SETREG(bas, UART_DM_CR, UART_DM_RESET_BREAK_INT);
SETREG(bas, UART_DM_CR, UART_DM_RESET_STALE_INT);
/* Enable/Disable Rx/Tx DM interfaces */
/* Disable Data Mover for now. */
uart_setreg(bas, UART_DM_DMEN, 0x0);
/* Enable transmitter and receiver */
uart_setreg(bas, UART_DM_CR, UART_DM_CR_RX_ENABLE);
uart_setreg(bas, UART_DM_CR, UART_DM_CR_TX_ENABLE);
uart_barrier(bas);
}
static void
msm_term(struct uart_bas *bas)
{
/* XXX */
}
static void
msm_putc(struct uart_bas *bas, int c)
{
int limit;
/*
* Write to NO_CHARS_FOR_TX register the number of characters
* to be transmitted. However, before writing TX_FIFO must
* be empty as indicated by TX_READY interrupt in IMR register
*/
/*
* Check if transmit FIFO is empty.
* If not wait for TX_READY interrupt.
*/
limit = 1000;
if (!(uart_getreg(bas, UART_DM_SR) & UART_DM_SR_TXEMT)) {
while ((uart_getreg(bas, UART_DM_ISR) & UART_DM_TX_READY) == 0
&& --limit)
DELAY(4);
}
/* FIFO is ready, write number of characters to be written */
uart_setreg(bas, UART_DM_NO_CHARS_FOR_TX, 1);
/* Wait till TX FIFO has space */
while ((uart_getreg(bas, UART_DM_SR) & UART_DM_SR_TXRDY) == 0)
DELAY(4);
/* TX FIFO has space. Write char */
SETREG(bas, UART_DM_TF(0), (c & 0xff));
}
static int
msm_rxready(struct uart_bas *bas)
{
/* Wait for a character to come ready */
return ((uart_getreg(bas, UART_DM_SR) & UART_DM_SR_RXRDY) ==
UART_DM_SR_RXRDY);
}
static int
msm_getc(struct uart_bas *bas, struct mtx *mtx)
{
int c;
uart_lock(mtx);
/* Wait for a character to come ready */
while ((uart_getreg(bas, UART_DM_SR) & UART_DM_SR_RXRDY) !=
UART_DM_SR_RXRDY)
DELAY(4);
/* Check for Overrun error. If so reset Error Status */
if (uart_getreg(bas, UART_DM_SR) & UART_DM_SR_UART_OVERRUN)
uart_setreg(bas, UART_DM_CR, UART_DM_RESET_ERROR_STATUS);
/* Read char */
c = uart_getreg(bas, UART_DM_RF(0));
uart_unlock(mtx);
return (c);
}
/*
* High-level UART interface.
*/
struct msm_uart_softc {
struct uart_softc base;
uint32_t ier;
};
static int msm_bus_probe(struct uart_softc *sc);
static int msm_bus_attach(struct uart_softc *sc);
static int msm_bus_flush(struct uart_softc *, int);
static int msm_bus_getsig(struct uart_softc *);
static int msm_bus_ioctl(struct uart_softc *, int, intptr_t);
static int msm_bus_ipend(struct uart_softc *);
static int msm_bus_param(struct uart_softc *, int, int, int, int);
static int msm_bus_receive(struct uart_softc *);
static int msm_bus_setsig(struct uart_softc *, int);
static int msm_bus_transmit(struct uart_softc *);
static void msm_bus_grab(struct uart_softc *);
static void msm_bus_ungrab(struct uart_softc *);
static kobj_method_t msm_methods[] = {
KOBJMETHOD(uart_probe, msm_bus_probe),
KOBJMETHOD(uart_attach, msm_bus_attach),
KOBJMETHOD(uart_flush, msm_bus_flush),
KOBJMETHOD(uart_getsig, msm_bus_getsig),
KOBJMETHOD(uart_ioctl, msm_bus_ioctl),
KOBJMETHOD(uart_ipend, msm_bus_ipend),
KOBJMETHOD(uart_param, msm_bus_param),
KOBJMETHOD(uart_receive, msm_bus_receive),
KOBJMETHOD(uart_setsig, msm_bus_setsig),
KOBJMETHOD(uart_transmit, msm_bus_transmit),
KOBJMETHOD(uart_grab, msm_bus_grab),
KOBJMETHOD(uart_ungrab, msm_bus_ungrab),
{0, 0 }
};
int
msm_bus_probe(struct uart_softc *sc)
{
sc->sc_txfifosz = 64;
sc->sc_rxfifosz = 64;
device_set_desc(sc->sc_dev, "Qualcomm HSUART");
return (0);
}
static int
msm_bus_attach(struct uart_softc *sc)
{
struct msm_uart_softc *u = (struct msm_uart_softc *)sc;
struct uart_bas *bas = &sc->sc_bas;
sc->sc_hwiflow = 0;
sc->sc_hwoflow = 0;
/* Set TX_READY, TXLEV, RXLEV, RXSTALE */
u->ier = UART_DM_IMR_ENABLED;
/* Configure Interrupt Mask register IMR */
uart_setreg(bas, UART_DM_IMR, u->ier);
return (0);
}
/*
* Write the current transmit buffer to the TX FIFO.
*/
static int
msm_bus_transmit(struct uart_softc *sc)
{
struct msm_uart_softc *u = (struct msm_uart_softc *)sc;
struct uart_bas *bas = &sc->sc_bas;
int i;
uart_lock(sc->sc_hwmtx);
/* Write some data */
for (i = 0; i < sc->sc_txdatasz; i++) {
/* Write TX data */
msm_putc(bas, sc->sc_txbuf[i]);
uart_barrier(bas);
}
/* TX FIFO is empty now, enable TX_READY interrupt */
u->ier |= UART_DM_TX_READY;
SETREG(bas, UART_DM_IMR, u->ier);
uart_barrier(bas);
/*
* Inform upper layer that it is transmitting data to hardware,
* this will be cleared when TXIDLE interrupt occurs.
*/
sc->sc_txbusy = 1;
uart_unlock(sc->sc_hwmtx);
return (0);
}
static int
msm_bus_setsig(struct uart_softc *sc, int sig)
{
return (0);
}
static int
msm_bus_receive(struct uart_softc *sc)
{
struct msm_uart_softc *u = (struct msm_uart_softc *)sc;
struct uart_bas *bas;
int c;
bas = &sc->sc_bas;
uart_lock(sc->sc_hwmtx);
/* Initialize Receive Path and interrupt */
SETREG(bas, UART_DM_CR, UART_DM_RESET_STALE_INT);
SETREG(bas, UART_DM_CR, UART_DM_STALE_EVENT_ENABLE);
u->ier |= UART_DM_RXLEV;
SETREG(bas, UART_DM_IMR, u->ier);
/* Loop over until we are full, or no data is available */
while (uart_getreg(bas, UART_DM_SR) & UART_DM_SR_RXRDY) {
if (uart_rx_full(sc)) {
/* No space left in input buffer */
sc->sc_rxbuf[sc->sc_rxput] = UART_STAT_OVERRUN;
break;
}
/* Read RX FIFO */
c = uart_getreg(bas, UART_DM_RF(0));
uart_barrier(bas);
uart_rx_put(sc, c);
}
uart_unlock(sc->sc_hwmtx);
return (0);
}
static int
msm_bus_param(struct uart_softc *sc, int baudrate, int databits,
int stopbits, int parity)
{
int error;
if (sc->sc_bas.rclk == 0)
sc->sc_bas.rclk = DEF_CLK;
KASSERT(sc->sc_bas.rclk != 0, ("msm_init: Invalid rclk"));
uart_lock(sc->sc_hwmtx);
error = msm_uart_param(&sc->sc_bas, baudrate, databits, stopbits,
parity);
uart_unlock(sc->sc_hwmtx);
return (error);
}
static int
msm_bus_ipend(struct uart_softc *sc)
{
struct msm_uart_softc *u = (struct msm_uart_softc *)sc;
struct uart_bas *bas = &sc->sc_bas;
uint32_t isr;
int ipend;
uart_lock(sc->sc_hwmtx);
/* Get ISR status */
isr = GETREG(bas, UART_DM_MISR);
ipend = 0;
/* Uart RX starting, notify upper layer */
if (isr & UART_DM_RXLEV) {
u->ier &= ~UART_DM_RXLEV;
SETREG(bas, UART_DM_IMR, u->ier);
uart_barrier(bas);
ipend |= SER_INT_RXREADY;
}
/* Stale RX interrupt */
if (isr & UART_DM_RXSTALE) {
/* Disable and reset it */
SETREG(bas, UART_DM_CR, UART_DM_STALE_EVENT_DISABLE);
SETREG(bas, UART_DM_CR, UART_DM_RESET_STALE_INT);
uart_barrier(bas);
ipend |= SER_INT_RXREADY;
}
/* TX READY interrupt */
if (isr & UART_DM_TX_READY) {
/* Clear TX Ready */
SETREG(bas, UART_DM_CR, UART_DM_CLEAR_TX_READY);
/* Disable TX_READY */
u->ier &= ~UART_DM_TX_READY;
SETREG(bas, UART_DM_IMR, u->ier);
uart_barrier(bas);
if (sc->sc_txbusy != 0)
ipend |= SER_INT_TXIDLE;
}
if (isr & UART_DM_TXLEV) {
/* TX FIFO is empty */
u->ier &= ~UART_DM_TXLEV;
SETREG(bas, UART_DM_IMR, u->ier);
uart_barrier(bas);
if (sc->sc_txbusy != 0)
ipend |= SER_INT_TXIDLE;
}
uart_unlock(sc->sc_hwmtx);
return (ipend);
}
static int
msm_bus_flush(struct uart_softc *sc, int what)
{
return (0);
}
static int
msm_bus_getsig(struct uart_softc *sc)
{
return (0);
}
static int
msm_bus_ioctl(struct uart_softc *sc, int request, intptr_t data)
{
return (EINVAL);
}
static void
msm_bus_grab(struct uart_softc *sc)
{
struct uart_bas *bas = &sc->sc_bas;
/*
* XXX: Turn off all interrupts to enter polling mode. Leave the
* saved mask alone. We'll restore whatever it was in ungrab.
*/
uart_lock(sc->sc_hwmtx);
SETREG(bas, UART_DM_CR, UART_DM_RESET_STALE_INT);
SETREG(bas, UART_DM_IMR, 0);
uart_barrier(bas);
uart_unlock(sc->sc_hwmtx);
}
static void
msm_bus_ungrab(struct uart_softc *sc)
{
struct msm_uart_softc *u = (struct msm_uart_softc *)sc;
struct uart_bas *bas = &sc->sc_bas;
/*
* Restore previous interrupt mask
*/
uart_lock(sc->sc_hwmtx);
SETREG(bas, UART_DM_IMR, u->ier);
uart_barrier(bas);
uart_unlock(sc->sc_hwmtx);
}
static struct uart_class uart_msm_class = {
"msm",
msm_methods,
sizeof(struct msm_uart_softc),
.uc_ops = &uart_msm_ops,
.uc_range = 8,
.uc_rclk = DEF_CLK,
};
static struct ofw_compat_data compat_data[] = {
{"qcom,msm-uartdm", (uintptr_t)&uart_msm_class},
{NULL, (uintptr_t)NULL},
};
UART_FDT_CLASS_AND_DEVICE(compat_data);
|