sifive_uart.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  1. /*-
  2. * SPDX-License-Identifier: BSD-2-Clause
  3. *
  4. * Copyright (c) 2019 Axiado Corporation
  5. * All rights reserved.
  6. *
  7. * This software was developed in part by Kristof Provost under contract for
  8. * Axiado Corporation.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in the
  17. * documentation and/or other materials provided with the distribution.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  20. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  23. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  24. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  25. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  26. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  27. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  28. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  29. * SUCH DAMAGE.
  30. */
  31. #include <sys/param.h>
  32. #include <sys/systm.h>
  33. #include <sys/bus.h>
  34. #include <sys/kernel.h>
  35. #include <sys/lock.h>
  36. #include <sys/module.h>
  37. #include <sys/mutex.h>
  38. #include <sys/rman.h>
  39. #include <machine/bus.h>
  40. #include <machine/cpu.h>
  41. #include <dev/clk/clk.h>
  42. #include <dev/ofw/ofw_bus.h>
  43. #include <dev/ofw/ofw_bus_subr.h>
  44. #include <dev/ofw/openfirm.h>
  45. #include <dev/uart/uart.h>
  46. #include <dev/uart/uart_bus.h>
  47. #include <dev/uart/uart_cpu.h>
  48. #include <dev/uart/uart_cpu_fdt.h>
  49. #include "uart_if.h"
  50. #define SFUART_TXDATA 0x00
  51. #define SFUART_TXDATA_FULL (1 << 31)
  52. #define SFUART_RXDATA 0x04
  53. #define SFUART_RXDATA_EMPTY (1 << 31)
  54. #define SFUART_TXCTRL 0x08
  55. #define SFUART_TXCTRL_ENABLE 0x01
  56. #define SFUART_TXCTRL_NSTOP 0x02
  57. #define SFUART_TXCTRL_TXCNT 0x70000
  58. #define SFUART_TXCTRL_TXCNT_SHIFT 16
  59. #define SFUART_RXCTRL 0x0c
  60. #define SFUART_RXCTRL_ENABLE 0x01
  61. #define SFUART_RXCTRL_RXCNT 0x70000
  62. #define SFUART_RXCTRL_RXCNT_SHIFT 16
  63. #define SFUART_IRQ_ENABLE 0x10
  64. #define SFUART_IRQ_ENABLE_TXWM 0x01
  65. #define SFUART_IRQ_ENABLE_RXWM 0x02
  66. #define SFUART_IRQ_PENDING 0x14
  67. #define SFUART_IRQ_PENDING_TXWM 0x01
  68. #define SFUART_IRQ_PENDING_RXQM 0x02
  69. #define SFUART_DIV 0x18
  70. #define SFUART_REGS_SIZE 0x1c
  71. #define SFUART_RX_FIFO_DEPTH 8
  72. #define SFUART_TX_FIFO_DEPTH 8
  73. struct sfuart_softc {
  74. struct uart_softc uart_softc;
  75. clk_t clk;
  76. };
  77. static int
  78. sfuart_probe(struct uart_bas *bas)
  79. {
  80. bas->regiowidth = 4;
  81. return (0);
  82. }
  83. static void
  84. sfuart_init(struct uart_bas *bas, int baudrate, int databits, int stopbits,
  85. int parity)
  86. {
  87. uint32_t reg;
  88. uart_setreg(bas, SFUART_IRQ_ENABLE, 0);
  89. /* Enable RX and configure the watermark so that we get an interrupt
  90. * when a single character arrives (if interrupts are enabled). */
  91. reg = SFUART_RXCTRL_ENABLE;
  92. reg |= (0 << SFUART_RXCTRL_RXCNT_SHIFT);
  93. uart_setreg(bas, SFUART_RXCTRL, reg);
  94. /* Enable TX and configure the watermark so that we get an interrupt
  95. * when there's room for one more character in the TX fifo (if
  96. * interrupts are enabled). */
  97. reg = SFUART_TXCTRL_ENABLE;
  98. reg |= (1 << SFUART_TXCTRL_TXCNT_SHIFT);
  99. if (stopbits == 2)
  100. reg |= SFUART_TXCTRL_NSTOP;
  101. uart_setreg(bas, SFUART_TXCTRL, reg);
  102. /* Don't touch DIV. Assume that's set correctly until we can
  103. * reconfigure. */
  104. }
  105. static void
  106. sfuart_putc(struct uart_bas *bas, int c)
  107. {
  108. while ((uart_getreg(bas, SFUART_TXDATA) & SFUART_TXDATA_FULL)
  109. != 0)
  110. cpu_spinwait();
  111. uart_setreg(bas, SFUART_TXDATA, c);
  112. }
  113. static int
  114. sfuart_rxready(struct uart_bas *bas)
  115. {
  116. /*
  117. * Unfortunately the FIFO empty flag is in the FIFO data register so
  118. * reading it would dequeue the character. Instead, rely on the fact
  119. * we've configured the watermark to be 0 and that interrupts are off
  120. * when using the low-level console function, and read the interrupt
  121. * pending state instead.
  122. */
  123. return ((uart_getreg(bas, SFUART_IRQ_PENDING) &
  124. SFUART_IRQ_PENDING_RXQM) != 0);
  125. }
  126. static int
  127. sfuart_getc(struct uart_bas *bas, struct mtx *hwmtx)
  128. {
  129. int c;
  130. uart_lock(hwmtx);
  131. while (((c = uart_getreg(bas, SFUART_RXDATA)) &
  132. SFUART_RXDATA_EMPTY) != 0) {
  133. uart_unlock(hwmtx);
  134. DELAY(4);
  135. uart_lock(hwmtx);
  136. }
  137. uart_unlock(hwmtx);
  138. return (c & 0xff);
  139. }
  140. static int
  141. sfuart_bus_probe(struct uart_softc *sc)
  142. {
  143. int error;
  144. error = sfuart_probe(&sc->sc_bas);
  145. if (error)
  146. return (error);
  147. sc->sc_rxfifosz = SFUART_RX_FIFO_DEPTH;
  148. sc->sc_txfifosz = SFUART_TX_FIFO_DEPTH;
  149. sc->sc_hwiflow = 0;
  150. sc->sc_hwoflow = 0;
  151. device_set_desc(sc->sc_dev, "SiFive UART");
  152. return (0);
  153. }
  154. static int
  155. sfuart_bus_attach(struct uart_softc *sc)
  156. {
  157. struct uart_bas *bas;
  158. struct sfuart_softc *sfsc;
  159. uint64_t freq;
  160. uint32_t reg;
  161. int error;
  162. sfsc = (struct sfuart_softc *)sc;
  163. bas = &sc->sc_bas;
  164. error = clk_get_by_ofw_index(sc->sc_dev, 0, 0, &sfsc->clk);
  165. if (error) {
  166. device_printf(sc->sc_dev, "couldn't allocate clock\n");
  167. return (ENXIO);
  168. }
  169. error = clk_enable(sfsc->clk);
  170. if (error) {
  171. device_printf(sc->sc_dev, "couldn't enable clock\n");
  172. return (ENXIO);
  173. }
  174. error = clk_get_freq(sfsc->clk, &freq);
  175. if (error || freq == 0) {
  176. clk_disable(sfsc->clk);
  177. device_printf(sc->sc_dev, "couldn't get clock frequency\n");
  178. return (ENXIO);
  179. }
  180. bas->rclk = freq;
  181. /* Enable RX/RX */
  182. reg = SFUART_RXCTRL_ENABLE;
  183. reg |= (0 << SFUART_RXCTRL_RXCNT_SHIFT);
  184. uart_setreg(bas, SFUART_RXCTRL, reg);
  185. reg = SFUART_TXCTRL_ENABLE;
  186. reg |= (1 << SFUART_TXCTRL_TXCNT_SHIFT);
  187. uart_setreg(bas, SFUART_TXCTRL, reg);
  188. /* Enable RX interrupt */
  189. uart_setreg(bas, SFUART_IRQ_ENABLE, SFUART_IRQ_ENABLE_RXWM);
  190. return (0);
  191. }
  192. static int
  193. sfuart_bus_detach(struct uart_softc *sc)
  194. {
  195. struct sfuart_softc *sfsc;
  196. struct uart_bas *bas;
  197. sfsc = (struct sfuart_softc *)sc;
  198. bas = &sc->sc_bas;
  199. /* Disable RX/TX */
  200. uart_setreg(bas, SFUART_RXCTRL, 0);
  201. uart_setreg(bas, SFUART_TXCTRL, 0);
  202. /* Disable interrupts */
  203. uart_setreg(bas, SFUART_IRQ_ENABLE, 0);
  204. clk_disable(sfsc->clk);
  205. return (0);
  206. }
  207. static int
  208. sfuart_bus_flush(struct uart_softc *sc, int what)
  209. {
  210. struct uart_bas *bas;
  211. uint32_t reg;
  212. bas = &sc->sc_bas;
  213. uart_lock(sc->sc_hwmtx);
  214. if (what & UART_FLUSH_TRANSMITTER) {
  215. do {
  216. reg = uart_getreg(bas, SFUART_TXDATA);
  217. } while ((reg & SFUART_TXDATA_FULL) != 0);
  218. }
  219. if (what & UART_FLUSH_RECEIVER) {
  220. do {
  221. reg = uart_getreg(bas, SFUART_RXDATA);
  222. } while ((reg & SFUART_RXDATA_EMPTY) == 0);
  223. }
  224. uart_unlock(sc->sc_hwmtx);
  225. return (0);
  226. }
  227. #define SIGCHG(c, i, s, d) \
  228. do { \
  229. if (c) \
  230. i |= ((i) & (s)) ? (s) : (s) | (d); \
  231. else \
  232. i = ((i) & (s)) ? ((i) & ~(s)) | (d) : (i); \
  233. } while (0)
  234. static int
  235. sfuart_bus_getsig(struct uart_softc *sc)
  236. {
  237. uint32_t new, old, sig;
  238. do {
  239. old = sc->sc_hwsig;
  240. sig = old;
  241. SIGCHG(1, sig, SER_DSR, SER_DDSR);
  242. SIGCHG(1, sig, SER_DCD, SER_DDCD);
  243. SIGCHG(1, sig, SER_CTS, SER_DCTS);
  244. new = sig & ~SER_MASK_DELTA;
  245. } while (!atomic_cmpset_32(&sc->sc_hwsig, old, new));
  246. return (sig);
  247. }
  248. static int
  249. sfuart_bus_setsig(struct uart_softc *sc, int sig)
  250. {
  251. uint32_t new, old;
  252. do {
  253. old = sc->sc_hwsig;
  254. new = old;
  255. if (sig & SER_DDTR) {
  256. SIGCHG(sig & SER_DTR, new, SER_DTR, SER_DDTR);
  257. }
  258. if (sig & SER_DRTS) {
  259. SIGCHG(sig & SER_RTS, new, SER_RTS, SER_DRTS);
  260. }
  261. } while (!atomic_cmpset_32(&sc->sc_hwsig, old, new));
  262. return (0);
  263. }
  264. static int
  265. sfuart_bus_ioctl(struct uart_softc *sc, int request, intptr_t data)
  266. {
  267. struct uart_bas *bas;
  268. uint32_t reg;
  269. int error;
  270. bas = &sc->sc_bas;
  271. uart_lock(sc->sc_hwmtx);
  272. switch (request) {
  273. case UART_IOCTL_BAUD:
  274. reg = uart_getreg(bas, SFUART_DIV);
  275. if (reg == 0) {
  276. /* Possible if the divisor hasn't been set up yet. */
  277. error = ENXIO;
  278. break;
  279. }
  280. *(int*)data = bas->rclk / (reg + 1);
  281. error = 0;
  282. break;
  283. default:
  284. error = EINVAL;
  285. break;
  286. }
  287. uart_unlock(sc->sc_hwmtx);
  288. return (error);
  289. }
  290. static int
  291. sfuart_bus_ipend(struct uart_softc *sc)
  292. {
  293. struct uart_bas *bas;
  294. int ipend;
  295. uint32_t reg, ie;
  296. bas = &sc->sc_bas;
  297. uart_lock(sc->sc_hwmtx);
  298. ipend = 0;
  299. reg = uart_getreg(bas, SFUART_IRQ_PENDING);
  300. ie = uart_getreg(bas, SFUART_IRQ_ENABLE);
  301. if ((reg & SFUART_IRQ_PENDING_TXWM) != 0 &&
  302. (ie & SFUART_IRQ_ENABLE_TXWM) != 0) {
  303. ipend |= SER_INT_TXIDLE;
  304. /* Disable TX interrupt */
  305. ie &= ~(SFUART_IRQ_ENABLE_TXWM);
  306. uart_setreg(bas, SFUART_IRQ_ENABLE, ie);
  307. }
  308. if ((reg & SFUART_IRQ_PENDING_RXQM) != 0)
  309. ipend |= SER_INT_RXREADY;
  310. uart_unlock(sc->sc_hwmtx);
  311. return (ipend);
  312. }
  313. static int
  314. sfuart_bus_param(struct uart_softc *sc, int baudrate, int databits,
  315. int stopbits, int parity)
  316. {
  317. struct uart_bas *bas;
  318. uint32_t reg;
  319. bas = &sc->sc_bas;
  320. if (databits != 8)
  321. return (EINVAL);
  322. if (parity != UART_PARITY_NONE)
  323. return (EINVAL);
  324. uart_lock(sc->sc_hwmtx);
  325. reg = uart_getreg(bas, SFUART_TXCTRL);
  326. if (stopbits == 2) {
  327. reg |= SFUART_TXCTRL_NSTOP;
  328. } else if (stopbits == 1) {
  329. reg &= ~SFUART_TXCTRL_NSTOP;
  330. } else {
  331. uart_unlock(sc->sc_hwmtx);
  332. return (EINVAL);
  333. }
  334. if (baudrate > 0 && bas->rclk != 0) {
  335. reg = (bas->rclk / baudrate) - 1;
  336. uart_setreg(bas, SFUART_DIV, reg);
  337. }
  338. uart_unlock(sc->sc_hwmtx);
  339. return (0);
  340. }
  341. static int
  342. sfuart_bus_receive(struct uart_softc *sc)
  343. {
  344. struct uart_bas *bas;
  345. uint32_t reg;
  346. bas = &sc->sc_bas;
  347. uart_lock(sc->sc_hwmtx);
  348. reg = uart_getreg(bas, SFUART_RXDATA);
  349. while ((reg & SFUART_RXDATA_EMPTY) == 0) {
  350. if (uart_rx_full(sc)) {
  351. sc->sc_rxbuf[sc->sc_rxput] = UART_STAT_OVERRUN;
  352. break;
  353. }
  354. uart_rx_put(sc, reg & 0xff);
  355. reg = uart_getreg(bas, SFUART_RXDATA);
  356. }
  357. uart_unlock(sc->sc_hwmtx);
  358. return (0);
  359. }
  360. static int
  361. sfuart_bus_transmit(struct uart_softc *sc)
  362. {
  363. struct uart_bas *bas;
  364. int i;
  365. uint32_t reg;
  366. bas = &sc->sc_bas;
  367. uart_lock(sc->sc_hwmtx);
  368. reg = uart_getreg(bas, SFUART_IRQ_ENABLE);
  369. reg |= SFUART_IRQ_ENABLE_TXWM;
  370. uart_setreg(bas, SFUART_IRQ_ENABLE, reg);
  371. for (i = 0; i < sc->sc_txdatasz; i++)
  372. sfuart_putc(bas, sc->sc_txbuf[i]);
  373. sc->sc_txbusy = 1;
  374. uart_unlock(sc->sc_hwmtx);
  375. return (0);
  376. }
  377. static void
  378. sfuart_bus_grab(struct uart_softc *sc)
  379. {
  380. struct uart_bas *bas;
  381. uint32_t reg;
  382. bas = &sc->sc_bas;
  383. uart_lock(sc->sc_hwmtx);
  384. reg = uart_getreg(bas, SFUART_IRQ_ENABLE);
  385. reg &= ~(SFUART_IRQ_ENABLE_TXWM | SFUART_IRQ_PENDING_RXQM);
  386. uart_setreg(bas, SFUART_IRQ_ENABLE, reg);
  387. uart_unlock(sc->sc_hwmtx);
  388. }
  389. static void
  390. sfuart_bus_ungrab(struct uart_softc *sc)
  391. {
  392. struct uart_bas *bas;
  393. uint32_t reg;
  394. bas = &sc->sc_bas;
  395. uart_lock(sc->sc_hwmtx);
  396. reg = uart_getreg(bas, SFUART_IRQ_ENABLE);
  397. reg |= SFUART_IRQ_ENABLE_TXWM | SFUART_IRQ_PENDING_RXQM;
  398. uart_setreg(bas, SFUART_IRQ_ENABLE, reg);
  399. uart_unlock(sc->sc_hwmtx);
  400. }
  401. static kobj_method_t sfuart_methods[] = {
  402. KOBJMETHOD(uart_probe, sfuart_bus_probe),
  403. KOBJMETHOD(uart_attach, sfuart_bus_attach),
  404. KOBJMETHOD(uart_detach, sfuart_bus_detach),
  405. KOBJMETHOD(uart_flush, sfuart_bus_flush),
  406. KOBJMETHOD(uart_getsig, sfuart_bus_getsig),
  407. KOBJMETHOD(uart_setsig, sfuart_bus_setsig),
  408. KOBJMETHOD(uart_ioctl, sfuart_bus_ioctl),
  409. KOBJMETHOD(uart_ipend, sfuart_bus_ipend),
  410. KOBJMETHOD(uart_param, sfuart_bus_param),
  411. KOBJMETHOD(uart_receive, sfuart_bus_receive),
  412. KOBJMETHOD(uart_transmit, sfuart_bus_transmit),
  413. KOBJMETHOD(uart_grab, sfuart_bus_grab),
  414. KOBJMETHOD(uart_ungrab, sfuart_bus_ungrab),
  415. KOBJMETHOD_END
  416. };
  417. static struct uart_ops sfuart_ops = {
  418. .probe = sfuart_probe,
  419. .init = sfuart_init,
  420. .term = NULL,
  421. .putc = sfuart_putc,
  422. .rxready = sfuart_rxready,
  423. .getc = sfuart_getc,
  424. };
  425. struct uart_class sfuart_class = {
  426. "sifiveuart",
  427. sfuart_methods,
  428. sizeof(struct sfuart_softc),
  429. .uc_ops = &sfuart_ops,
  430. .uc_range = SFUART_REGS_SIZE,
  431. .uc_rclk = 0,
  432. .uc_rshift = 0
  433. };
  434. static struct ofw_compat_data compat_data[] = {
  435. { "sifive,uart0", (uintptr_t)&sfuart_class },
  436. { NULL, (uintptr_t)NULL }
  437. };
  438. UART_FDT_CLASS_AND_DEVICE(compat_data);