sifive.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * SiFive UART driver
  4. * Copyright (C) 2018 Paul Walmsley <paul@pwsan.com>
  5. * Copyright (C) 2018-2019 SiFive
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * Based partially on:
  18. * - drivers/tty/serial/pxa.c
  19. * - drivers/tty/serial/amba-pl011.c
  20. * - drivers/tty/serial/uartlite.c
  21. * - drivers/tty/serial/omap-serial.c
  22. * - drivers/pwm/pwm-sifive.c
  23. *
  24. * See the following sources for further documentation:
  25. * - Chapter 19 "Universal Asynchronous Receiver/Transmitter (UART)" of
  26. * SiFive FE310-G000 v2p3
  27. * - The tree/master/src/main/scala/devices/uart directory of
  28. * https://github.com/sifive/sifive-blocks/
  29. *
  30. * The SiFive UART design is not 8250-compatible. The following common
  31. * features are not supported:
  32. * - Word lengths other than 8 bits
  33. * - Break handling
  34. * - Parity
  35. * - Flow control
  36. * - Modem signals (DSR, RI, etc.)
  37. * On the other hand, the design is free from the baggage of the 8250
  38. * programming model.
  39. */
  40. #include <linux/clk.h>
  41. #include <linux/console.h>
  42. #include <linux/delay.h>
  43. #include <linux/init.h>
  44. #include <linux/io.h>
  45. #include <linux/irq.h>
  46. #include <linux/module.h>
  47. #include <linux/of.h>
  48. #include <linux/of_irq.h>
  49. #include <linux/platform_device.h>
  50. #include <linux/serial_core.h>
  51. #include <linux/serial_reg.h>
  52. #include <linux/slab.h>
  53. #include <linux/tty.h>
  54. #include <linux/tty_flip.h>
  55. /*
  56. * Register offsets
  57. */
  58. /* TXDATA */
  59. #define SIFIVE_SERIAL_TXDATA_OFFS 0x0
  60. #define SIFIVE_SERIAL_TXDATA_FULL_SHIFT 31
  61. #define SIFIVE_SERIAL_TXDATA_FULL_MASK (1 << SIFIVE_SERIAL_TXDATA_FULL_SHIFT)
  62. #define SIFIVE_SERIAL_TXDATA_DATA_SHIFT 0
  63. #define SIFIVE_SERIAL_TXDATA_DATA_MASK (0xff << SIFIVE_SERIAL_TXDATA_DATA_SHIFT)
  64. /* RXDATA */
  65. #define SIFIVE_SERIAL_RXDATA_OFFS 0x4
  66. #define SIFIVE_SERIAL_RXDATA_EMPTY_SHIFT 31
  67. #define SIFIVE_SERIAL_RXDATA_EMPTY_MASK (1 << SIFIVE_SERIAL_RXDATA_EMPTY_SHIFT)
  68. #define SIFIVE_SERIAL_RXDATA_DATA_SHIFT 0
  69. #define SIFIVE_SERIAL_RXDATA_DATA_MASK (0xff << SIFIVE_SERIAL_RXDATA_DATA_SHIFT)
  70. /* TXCTRL */
  71. #define SIFIVE_SERIAL_TXCTRL_OFFS 0x8
  72. #define SIFIVE_SERIAL_TXCTRL_TXCNT_SHIFT 16
  73. #define SIFIVE_SERIAL_TXCTRL_TXCNT_MASK (0x7 << SIFIVE_SERIAL_TXCTRL_TXCNT_SHIFT)
  74. #define SIFIVE_SERIAL_TXCTRL_NSTOP_SHIFT 1
  75. #define SIFIVE_SERIAL_TXCTRL_NSTOP_MASK (1 << SIFIVE_SERIAL_TXCTRL_NSTOP_SHIFT)
  76. #define SIFIVE_SERIAL_TXCTRL_TXEN_SHIFT 0
  77. #define SIFIVE_SERIAL_TXCTRL_TXEN_MASK (1 << SIFIVE_SERIAL_TXCTRL_TXEN_SHIFT)
  78. /* RXCTRL */
  79. #define SIFIVE_SERIAL_RXCTRL_OFFS 0xC
  80. #define SIFIVE_SERIAL_RXCTRL_RXCNT_SHIFT 16
  81. #define SIFIVE_SERIAL_RXCTRL_RXCNT_MASK (0x7 << SIFIVE_SERIAL_TXCTRL_TXCNT_SHIFT)
  82. #define SIFIVE_SERIAL_RXCTRL_RXEN_SHIFT 0
  83. #define SIFIVE_SERIAL_RXCTRL_RXEN_MASK (1 << SIFIVE_SERIAL_RXCTRL_RXEN_SHIFT)
  84. /* IE */
  85. #define SIFIVE_SERIAL_IE_OFFS 0x10
  86. #define SIFIVE_SERIAL_IE_RXWM_SHIFT 1
  87. #define SIFIVE_SERIAL_IE_RXWM_MASK (1 << SIFIVE_SERIAL_IE_RXWM_SHIFT)
  88. #define SIFIVE_SERIAL_IE_TXWM_SHIFT 0
  89. #define SIFIVE_SERIAL_IE_TXWM_MASK (1 << SIFIVE_SERIAL_IE_TXWM_SHIFT)
  90. /* IP */
  91. #define SIFIVE_SERIAL_IP_OFFS 0x14
  92. #define SIFIVE_SERIAL_IP_RXWM_SHIFT 1
  93. #define SIFIVE_SERIAL_IP_RXWM_MASK (1 << SIFIVE_SERIAL_IP_RXWM_SHIFT)
  94. #define SIFIVE_SERIAL_IP_TXWM_SHIFT 0
  95. #define SIFIVE_SERIAL_IP_TXWM_MASK (1 << SIFIVE_SERIAL_IP_TXWM_SHIFT)
  96. /* DIV */
  97. #define SIFIVE_SERIAL_DIV_OFFS 0x18
  98. #define SIFIVE_SERIAL_DIV_DIV_SHIFT 0
  99. #define SIFIVE_SERIAL_DIV_DIV_MASK (0xffff << SIFIVE_SERIAL_IP_DIV_SHIFT)
  100. /*
  101. * Config macros
  102. */
  103. /*
  104. * SIFIVE_SERIAL_MAX_PORTS: maximum number of UARTs on a device that can
  105. * host a serial console
  106. */
  107. #define SIFIVE_SERIAL_MAX_PORTS 8
  108. /*
  109. * SIFIVE_DEFAULT_BAUD_RATE: default baud rate that the driver should
  110. * configure itself to use
  111. */
  112. #define SIFIVE_DEFAULT_BAUD_RATE 115200
  113. /* SIFIVE_SERIAL_NAME: our driver's name that we pass to the operating system */
  114. #define SIFIVE_SERIAL_NAME "sifive-serial"
  115. /* SIFIVE_TTY_PREFIX: tty name prefix for SiFive serial ports */
  116. #define SIFIVE_TTY_PREFIX "ttySIF"
  117. /* SIFIVE_TX_FIFO_DEPTH: depth of the TX FIFO (in bytes) */
  118. #define SIFIVE_TX_FIFO_DEPTH 8
  119. /* SIFIVE_RX_FIFO_DEPTH: depth of the TX FIFO (in bytes) */
  120. #define SIFIVE_RX_FIFO_DEPTH 8
  121. #if (SIFIVE_TX_FIFO_DEPTH != SIFIVE_RX_FIFO_DEPTH)
  122. #error Driver does not support configurations with different TX, RX FIFO sizes
  123. #endif
  124. /*
  125. *
  126. */
  127. /**
  128. * sifive_serial_port - driver-specific data extension to struct uart_port
  129. * @port: struct uart_port embedded in this struct
  130. * @dev: struct device *
  131. * @ier: shadowed copy of the interrupt enable register
  132. * @clkin_rate: input clock to the UART IP block.
  133. * @baud_rate: UART serial line rate (e.g., 115200 baud)
  134. * @clk_notifier: clock rate change notifier for upstream clock changes
  135. *
  136. * Configuration data specific to this SiFive UART.
  137. */
  138. struct sifive_serial_port {
  139. struct uart_port port;
  140. struct device *dev;
  141. unsigned char ier;
  142. unsigned long clkin_rate;
  143. unsigned long baud_rate;
  144. struct clk *clk;
  145. struct notifier_block clk_notifier;
  146. };
  147. /*
  148. * Structure container-of macros
  149. */
  150. #define port_to_sifive_serial_port(p) (container_of((p), \
  151. struct sifive_serial_port, \
  152. port))
  153. #define notifier_to_sifive_serial_port(nb) (container_of((nb), \
  154. struct sifive_serial_port, \
  155. clk_notifier))
  156. /*
  157. * Forward declarations
  158. */
  159. static void sifive_serial_stop_tx(struct uart_port *port);
  160. /*
  161. * Internal functions
  162. */
  163. /**
  164. * __ssp_early_writel() - write to a SiFive serial port register (early)
  165. * @port: pointer to a struct uart_port record
  166. * @offs: register address offset from the IP block base address
  167. * @v: value to write to the register
  168. *
  169. * Given a pointer @port to a struct uart_port record, write the value
  170. * @v to the IP block register address offset @offs. This function is
  171. * intended for early console use.
  172. *
  173. * Context: Intended to be used only by the earlyconsole code.
  174. */
  175. static void __ssp_early_writel(u32 v, u16 offs, struct uart_port *port)
  176. {
  177. writel_relaxed(v, port->membase + offs);
  178. }
  179. /**
  180. * __ssp_early_readl() - read from a SiFive serial port register (early)
  181. * @port: pointer to a struct uart_port record
  182. * @offs: register address offset from the IP block base address
  183. *
  184. * Given a pointer @port to a struct uart_port record, read the
  185. * contents of the IP block register located at offset @offs from the
  186. * IP block base and return it. This function is intended for early
  187. * console use.
  188. *
  189. * Context: Intended to be called only by the earlyconsole code or by
  190. * __ssp_readl() or __ssp_writel() (in this driver)
  191. *
  192. * Returns: the register value read from the UART.
  193. */
  194. static u32 __ssp_early_readl(struct uart_port *port, u16 offs)
  195. {
  196. return readl_relaxed(port->membase + offs);
  197. }
  198. /**
  199. * __ssp_writel() - write to a SiFive serial port register
  200. * @v: value to write to the register
  201. * @offs: register address offset from the IP block base address
  202. * @ssp: pointer to a struct sifive_serial_port record
  203. *
  204. * Write the value @v to the IP block register located at offset @offs from the
  205. * IP block base, given a pointer @ssp to a struct sifive_serial_port record.
  206. *
  207. * Context: Any context.
  208. */
  209. static void __ssp_writel(u32 v, u16 offs, struct sifive_serial_port *ssp)
  210. {
  211. __ssp_early_writel(v, offs, &ssp->port);
  212. }
  213. /**
  214. * __ssp_readl() - read from a SiFive serial port register
  215. * @ssp: pointer to a struct sifive_serial_port record
  216. * @offs: register address offset from the IP block base address
  217. *
  218. * Read the contents of the IP block register located at offset @offs from the
  219. * IP block base, given a pointer @ssp to a struct sifive_serial_port record.
  220. *
  221. * Context: Any context.
  222. *
  223. * Returns: the value of the UART register
  224. */
  225. static u32 __ssp_readl(struct sifive_serial_port *ssp, u16 offs)
  226. {
  227. return __ssp_early_readl(&ssp->port, offs);
  228. }
  229. /**
  230. * sifive_serial_is_txfifo_full() - is the TXFIFO full?
  231. * @ssp: pointer to a struct sifive_serial_port
  232. *
  233. * Read the transmit FIFO "full" bit, returning a non-zero value if the
  234. * TX FIFO is full, or zero if space remains. Intended to be used to prevent
  235. * writes to the TX FIFO when it's full.
  236. *
  237. * Returns: SIFIVE_SERIAL_TXDATA_FULL_MASK (non-zero) if the transmit FIFO
  238. * is full, or 0 if space remains.
  239. */
  240. static int sifive_serial_is_txfifo_full(struct sifive_serial_port *ssp)
  241. {
  242. return __ssp_readl(ssp, SIFIVE_SERIAL_TXDATA_OFFS) &
  243. SIFIVE_SERIAL_TXDATA_FULL_MASK;
  244. }
  245. /**
  246. * __ssp_transmit_char() - enqueue a byte to transmit onto the TX FIFO
  247. * @ssp: pointer to a struct sifive_serial_port
  248. * @ch: character to transmit
  249. *
  250. * Enqueue a byte @ch onto the transmit FIFO, given a pointer @ssp to the
  251. * struct sifive_serial_port * to transmit on. Caller should first check to
  252. * ensure that the TXFIFO has space; see sifive_serial_is_txfifo_full().
  253. *
  254. * Context: Any context.
  255. */
  256. static void __ssp_transmit_char(struct sifive_serial_port *ssp, int ch)
  257. {
  258. __ssp_writel(ch, SIFIVE_SERIAL_TXDATA_OFFS, ssp);
  259. }
  260. /**
  261. * __ssp_transmit_chars() - enqueue multiple bytes onto the TX FIFO
  262. * @ssp: pointer to a struct sifive_serial_port
  263. *
  264. * Transfer up to a TX FIFO size's worth of characters from the Linux serial
  265. * transmit buffer to the SiFive UART TX FIFO.
  266. *
  267. * Context: Any context. Expects @ssp->port.lock to be held by caller.
  268. */
  269. static void __ssp_transmit_chars(struct sifive_serial_port *ssp)
  270. {
  271. struct circ_buf *xmit = &ssp->port.state->xmit;
  272. int count;
  273. if (ssp->port.x_char) {
  274. __ssp_transmit_char(ssp, ssp->port.x_char);
  275. ssp->port.icount.tx++;
  276. ssp->port.x_char = 0;
  277. return;
  278. }
  279. if (uart_circ_empty(xmit) || uart_tx_stopped(&ssp->port)) {
  280. sifive_serial_stop_tx(&ssp->port);
  281. return;
  282. }
  283. count = SIFIVE_TX_FIFO_DEPTH;
  284. do {
  285. __ssp_transmit_char(ssp, xmit->buf[xmit->tail]);
  286. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  287. ssp->port.icount.tx++;
  288. if (uart_circ_empty(xmit))
  289. break;
  290. } while (--count > 0);
  291. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  292. uart_write_wakeup(&ssp->port);
  293. if (uart_circ_empty(xmit))
  294. sifive_serial_stop_tx(&ssp->port);
  295. }
  296. /**
  297. * __ssp_enable_txwm() - enable transmit watermark interrupts
  298. * @ssp: pointer to a struct sifive_serial_port
  299. *
  300. * Enable interrupt generation when the transmit FIFO watermark is reached
  301. * on the SiFive UART referred to by @ssp.
  302. */
  303. static void __ssp_enable_txwm(struct sifive_serial_port *ssp)
  304. {
  305. if (ssp->ier & SIFIVE_SERIAL_IE_TXWM_MASK)
  306. return;
  307. ssp->ier |= SIFIVE_SERIAL_IE_TXWM_MASK;
  308. __ssp_writel(ssp->ier, SIFIVE_SERIAL_IE_OFFS, ssp);
  309. }
  310. /**
  311. * __ssp_enable_rxwm() - enable receive watermark interrupts
  312. * @ssp: pointer to a struct sifive_serial_port
  313. *
  314. * Enable interrupt generation when the receive FIFO watermark is reached
  315. * on the SiFive UART referred to by @ssp.
  316. */
  317. static void __ssp_enable_rxwm(struct sifive_serial_port *ssp)
  318. {
  319. if (ssp->ier & SIFIVE_SERIAL_IE_RXWM_MASK)
  320. return;
  321. ssp->ier |= SIFIVE_SERIAL_IE_RXWM_MASK;
  322. __ssp_writel(ssp->ier, SIFIVE_SERIAL_IE_OFFS, ssp);
  323. }
  324. /**
  325. * __ssp_disable_txwm() - disable transmit watermark interrupts
  326. * @ssp: pointer to a struct sifive_serial_port
  327. *
  328. * Disable interrupt generation when the transmit FIFO watermark is reached
  329. * on the UART referred to by @ssp.
  330. */
  331. static void __ssp_disable_txwm(struct sifive_serial_port *ssp)
  332. {
  333. if (!(ssp->ier & SIFIVE_SERIAL_IE_TXWM_MASK))
  334. return;
  335. ssp->ier &= ~SIFIVE_SERIAL_IE_TXWM_MASK;
  336. __ssp_writel(ssp->ier, SIFIVE_SERIAL_IE_OFFS, ssp);
  337. }
  338. /**
  339. * __ssp_disable_rxwm() - disable receive watermark interrupts
  340. * @ssp: pointer to a struct sifive_serial_port
  341. *
  342. * Disable interrupt generation when the receive FIFO watermark is reached
  343. * on the UART referred to by @ssp.
  344. */
  345. static void __ssp_disable_rxwm(struct sifive_serial_port *ssp)
  346. {
  347. if (!(ssp->ier & SIFIVE_SERIAL_IE_RXWM_MASK))
  348. return;
  349. ssp->ier &= ~SIFIVE_SERIAL_IE_RXWM_MASK;
  350. __ssp_writel(ssp->ier, SIFIVE_SERIAL_IE_OFFS, ssp);
  351. }
  352. /**
  353. * __ssp_receive_char() - receive a byte from the UART
  354. * @ssp: pointer to a struct sifive_serial_port
  355. * @is_empty: char pointer to return whether the RX FIFO is empty
  356. *
  357. * Try to read a byte from the SiFive UART RX FIFO, referenced by
  358. * @ssp, and to return it. Also returns the RX FIFO empty bit in
  359. * the char pointed to by @ch. The caller must pass the byte back to the
  360. * Linux serial layer if needed.
  361. *
  362. * Returns: the byte read from the UART RX FIFO.
  363. */
  364. static char __ssp_receive_char(struct sifive_serial_port *ssp, char *is_empty)
  365. {
  366. u32 v;
  367. u8 ch;
  368. v = __ssp_readl(ssp, SIFIVE_SERIAL_RXDATA_OFFS);
  369. if (!is_empty)
  370. WARN_ON(1);
  371. else
  372. *is_empty = (v & SIFIVE_SERIAL_RXDATA_EMPTY_MASK) >>
  373. SIFIVE_SERIAL_RXDATA_EMPTY_SHIFT;
  374. ch = (v & SIFIVE_SERIAL_RXDATA_DATA_MASK) >>
  375. SIFIVE_SERIAL_RXDATA_DATA_SHIFT;
  376. return ch;
  377. }
  378. /**
  379. * __ssp_receive_chars() - receive multiple bytes from the UART
  380. * @ssp: pointer to a struct sifive_serial_port
  381. *
  382. * Receive up to an RX FIFO's worth of bytes from the SiFive UART referred
  383. * to by @ssp and pass them up to the Linux serial layer.
  384. *
  385. * Context: Expects ssp->port.lock to be held by caller.
  386. */
  387. static void __ssp_receive_chars(struct sifive_serial_port *ssp)
  388. {
  389. unsigned char ch;
  390. char is_empty;
  391. int c;
  392. for (c = SIFIVE_RX_FIFO_DEPTH; c > 0; --c) {
  393. ch = __ssp_receive_char(ssp, &is_empty);
  394. if (is_empty)
  395. break;
  396. ssp->port.icount.rx++;
  397. uart_insert_char(&ssp->port, 0, 0, ch, TTY_NORMAL);
  398. }
  399. spin_unlock(&ssp->port.lock);
  400. tty_flip_buffer_push(&ssp->port.state->port);
  401. spin_lock(&ssp->port.lock);
  402. }
  403. /**
  404. * __ssp_update_div() - calculate the divisor setting by the line rate
  405. * @ssp: pointer to a struct sifive_serial_port
  406. *
  407. * Calculate the appropriate value of the clock divisor for the UART
  408. * and target line rate referred to by @ssp and write it into the
  409. * hardware.
  410. */
  411. static void __ssp_update_div(struct sifive_serial_port *ssp)
  412. {
  413. u16 div;
  414. div = DIV_ROUND_UP(ssp->clkin_rate, ssp->baud_rate) - 1;
  415. __ssp_writel(div, SIFIVE_SERIAL_DIV_OFFS, ssp);
  416. }
  417. /**
  418. * __ssp_update_baud_rate() - set the UART "baud rate"
  419. * @ssp: pointer to a struct sifive_serial_port
  420. * @rate: new target bit rate
  421. *
  422. * Calculate the UART divisor value for the target bit rate @rate for the
  423. * SiFive UART described by @ssp and program it into the UART. There may
  424. * be some error between the target bit rate and the actual bit rate implemented
  425. * by the UART due to clock ratio granularity.
  426. */
  427. static void __ssp_update_baud_rate(struct sifive_serial_port *ssp,
  428. unsigned int rate)
  429. {
  430. if (ssp->baud_rate == rate)
  431. return;
  432. ssp->baud_rate = rate;
  433. __ssp_update_div(ssp);
  434. }
  435. /**
  436. * __ssp_set_stop_bits() - set the number of stop bits
  437. * @ssp: pointer to a struct sifive_serial_port
  438. * @nstop: 1 or 2 (stop bits)
  439. *
  440. * Program the SiFive UART referred to by @ssp to use @nstop stop bits.
  441. */
  442. static void __ssp_set_stop_bits(struct sifive_serial_port *ssp, char nstop)
  443. {
  444. u32 v;
  445. if (nstop < 1 || nstop > 2) {
  446. WARN_ON(1);
  447. return;
  448. }
  449. v = __ssp_readl(ssp, SIFIVE_SERIAL_TXCTRL_OFFS);
  450. v &= ~SIFIVE_SERIAL_TXCTRL_NSTOP_MASK;
  451. v |= (nstop - 1) << SIFIVE_SERIAL_TXCTRL_NSTOP_SHIFT;
  452. __ssp_writel(v, SIFIVE_SERIAL_TXCTRL_OFFS, ssp);
  453. }
  454. /**
  455. * __ssp_wait_for_xmitr() - wait for an empty slot on the TX FIFO
  456. * @ssp: pointer to a struct sifive_serial_port
  457. *
  458. * Delay while the UART TX FIFO referred to by @ssp is marked as full.
  459. *
  460. * Context: Any context.
  461. */
  462. static void __maybe_unused __ssp_wait_for_xmitr(struct sifive_serial_port *ssp)
  463. {
  464. while (sifive_serial_is_txfifo_full(ssp))
  465. udelay(1); /* XXX Could probably be more intelligent here */
  466. }
  467. /*
  468. * Linux serial API functions
  469. */
  470. static void sifive_serial_stop_tx(struct uart_port *port)
  471. {
  472. struct sifive_serial_port *ssp = port_to_sifive_serial_port(port);
  473. __ssp_disable_txwm(ssp);
  474. }
  475. static void sifive_serial_stop_rx(struct uart_port *port)
  476. {
  477. struct sifive_serial_port *ssp = port_to_sifive_serial_port(port);
  478. __ssp_disable_rxwm(ssp);
  479. }
  480. static void sifive_serial_start_tx(struct uart_port *port)
  481. {
  482. struct sifive_serial_port *ssp = port_to_sifive_serial_port(port);
  483. __ssp_enable_txwm(ssp);
  484. }
  485. static irqreturn_t sifive_serial_irq(int irq, void *dev_id)
  486. {
  487. struct sifive_serial_port *ssp = dev_id;
  488. u32 ip;
  489. spin_lock(&ssp->port.lock);
  490. ip = __ssp_readl(ssp, SIFIVE_SERIAL_IP_OFFS);
  491. if (!ip) {
  492. spin_unlock(&ssp->port.lock);
  493. return IRQ_NONE;
  494. }
  495. if (ip & SIFIVE_SERIAL_IP_RXWM_MASK)
  496. __ssp_receive_chars(ssp);
  497. if (ip & SIFIVE_SERIAL_IP_TXWM_MASK)
  498. __ssp_transmit_chars(ssp);
  499. spin_unlock(&ssp->port.lock);
  500. return IRQ_HANDLED;
  501. }
  502. static unsigned int sifive_serial_tx_empty(struct uart_port *port)
  503. {
  504. return TIOCSER_TEMT;
  505. }
  506. static unsigned int sifive_serial_get_mctrl(struct uart_port *port)
  507. {
  508. return TIOCM_CAR | TIOCM_CTS | TIOCM_DSR;
  509. }
  510. static void sifive_serial_set_mctrl(struct uart_port *port, unsigned int mctrl)
  511. {
  512. /* IP block does not support these signals */
  513. }
  514. static void sifive_serial_break_ctl(struct uart_port *port, int break_state)
  515. {
  516. /* IP block does not support sending a break */
  517. }
  518. static int sifive_serial_startup(struct uart_port *port)
  519. {
  520. struct sifive_serial_port *ssp = port_to_sifive_serial_port(port);
  521. __ssp_enable_rxwm(ssp);
  522. return 0;
  523. }
  524. static void sifive_serial_shutdown(struct uart_port *port)
  525. {
  526. struct sifive_serial_port *ssp = port_to_sifive_serial_port(port);
  527. __ssp_disable_rxwm(ssp);
  528. __ssp_disable_txwm(ssp);
  529. }
  530. /**
  531. * sifive_serial_clk_notifier() - clock post-rate-change notifier
  532. * @nb: pointer to the struct notifier_block, from the notifier code
  533. * @event: event mask from the notifier code
  534. * @data: pointer to the struct clk_notifier_data from the notifier code
  535. *
  536. * On the V0 SoC, the UART IP block is derived from the CPU clock source
  537. * after a synchronous divide-by-two divider, so any CPU clock rate change
  538. * requires the UART baud rate to be updated. This presumably corrupts any
  539. * serial word currently being transmitted or received. In order to avoid
  540. * corrupting the output data stream, we drain the transmit queue before
  541. * allowing the clock's rate to be changed.
  542. */
  543. static int sifive_serial_clk_notifier(struct notifier_block *nb,
  544. unsigned long event, void *data)
  545. {
  546. struct clk_notifier_data *cnd = data;
  547. struct sifive_serial_port *ssp = notifier_to_sifive_serial_port(nb);
  548. if (event == PRE_RATE_CHANGE) {
  549. /*
  550. * The TX watermark is always set to 1 by this driver, which
  551. * means that the TX busy bit will lower when there are 0 bytes
  552. * left in the TX queue -- in other words, when the TX FIFO is
  553. * empty.
  554. */
  555. __ssp_wait_for_xmitr(ssp);
  556. /*
  557. * On the cycle the TX FIFO goes empty there is still a full
  558. * UART frame left to be transmitted in the shift register.
  559. * The UART provides no way for software to directly determine
  560. * when that last frame has been transmitted, so we just sleep
  561. * here instead. As we're not tracking the number of stop bits
  562. * they're just worst cased here. The rest of the serial
  563. * framing parameters aren't configurable by software.
  564. */
  565. udelay(DIV_ROUND_UP(12 * 1000 * 1000, ssp->baud_rate));
  566. }
  567. if (event == POST_RATE_CHANGE && ssp->clkin_rate != cnd->new_rate) {
  568. ssp->clkin_rate = cnd->new_rate;
  569. __ssp_update_div(ssp);
  570. }
  571. return NOTIFY_OK;
  572. }
  573. static void sifive_serial_set_termios(struct uart_port *port,
  574. struct ktermios *termios,
  575. struct ktermios *old)
  576. {
  577. struct sifive_serial_port *ssp = port_to_sifive_serial_port(port);
  578. unsigned long flags;
  579. u32 v, old_v;
  580. int rate;
  581. char nstop;
  582. if ((termios->c_cflag & CSIZE) != CS8)
  583. dev_err_once(ssp->port.dev, "only 8-bit words supported\n");
  584. if (termios->c_iflag & (INPCK | PARMRK))
  585. dev_err_once(ssp->port.dev, "parity checking not supported\n");
  586. if (termios->c_iflag & BRKINT)
  587. dev_err_once(ssp->port.dev, "BREAK detection not supported\n");
  588. /* Set number of stop bits */
  589. nstop = (termios->c_cflag & CSTOPB) ? 2 : 1;
  590. __ssp_set_stop_bits(ssp, nstop);
  591. /* Set line rate */
  592. rate = uart_get_baud_rate(port, termios, old, 0, ssp->clkin_rate / 16);
  593. __ssp_update_baud_rate(ssp, rate);
  594. spin_lock_irqsave(&ssp->port.lock, flags);
  595. /* Update the per-port timeout */
  596. uart_update_timeout(port, termios->c_cflag, rate);
  597. ssp->port.read_status_mask = 0;
  598. /* Ignore all characters if CREAD is not set */
  599. v = __ssp_readl(ssp, SIFIVE_SERIAL_RXCTRL_OFFS);
  600. old_v = v;
  601. if ((termios->c_cflag & CREAD) == 0)
  602. v &= SIFIVE_SERIAL_RXCTRL_RXEN_MASK;
  603. else
  604. v |= SIFIVE_SERIAL_RXCTRL_RXEN_MASK;
  605. if (v != old_v)
  606. __ssp_writel(v, SIFIVE_SERIAL_RXCTRL_OFFS, ssp);
  607. spin_unlock_irqrestore(&ssp->port.lock, flags);
  608. }
  609. static void sifive_serial_release_port(struct uart_port *port)
  610. {
  611. }
  612. static int sifive_serial_request_port(struct uart_port *port)
  613. {
  614. return 0;
  615. }
  616. static void sifive_serial_config_port(struct uart_port *port, int flags)
  617. {
  618. struct sifive_serial_port *ssp = port_to_sifive_serial_port(port);
  619. ssp->port.type = PORT_SIFIVE_V0;
  620. }
  621. static int sifive_serial_verify_port(struct uart_port *port,
  622. struct serial_struct *ser)
  623. {
  624. return -EINVAL;
  625. }
  626. static const char *sifive_serial_type(struct uart_port *port)
  627. {
  628. return port->type == PORT_SIFIVE_V0 ? "SiFive UART v0" : NULL;
  629. }
  630. /*
  631. * Early console support
  632. */
  633. #ifdef CONFIG_SERIAL_EARLYCON
  634. static void early_sifive_serial_putc(struct uart_port *port, int c)
  635. {
  636. while (__ssp_early_readl(port, SIFIVE_SERIAL_TXDATA_OFFS) &
  637. SIFIVE_SERIAL_TXDATA_FULL_MASK)
  638. cpu_relax();
  639. __ssp_early_writel(c, SIFIVE_SERIAL_TXDATA_OFFS, port);
  640. }
  641. static void early_sifive_serial_write(struct console *con, const char *s,
  642. unsigned int n)
  643. {
  644. struct earlycon_device *dev = con->data;
  645. struct uart_port *port = &dev->port;
  646. uart_console_write(port, s, n, early_sifive_serial_putc);
  647. }
  648. static int __init early_sifive_serial_setup(struct earlycon_device *dev,
  649. const char *options)
  650. {
  651. struct uart_port *port = &dev->port;
  652. if (!port->membase)
  653. return -ENODEV;
  654. dev->con->write = early_sifive_serial_write;
  655. return 0;
  656. }
  657. OF_EARLYCON_DECLARE(sifive, "sifive,uart0", early_sifive_serial_setup);
  658. OF_EARLYCON_DECLARE(sifive, "sifive,fu540-c000-uart0",
  659. early_sifive_serial_setup);
  660. #endif /* CONFIG_SERIAL_EARLYCON */
  661. /*
  662. * Linux console interface
  663. */
  664. #ifdef CONFIG_SERIAL_SIFIVE_CONSOLE
  665. static struct sifive_serial_port *sifive_serial_console_ports[SIFIVE_SERIAL_MAX_PORTS];
  666. static void sifive_serial_console_putchar(struct uart_port *port, int ch)
  667. {
  668. struct sifive_serial_port *ssp = port_to_sifive_serial_port(port);
  669. __ssp_wait_for_xmitr(ssp);
  670. __ssp_transmit_char(ssp, ch);
  671. }
  672. static void sifive_serial_console_write(struct console *co, const char *s,
  673. unsigned int count)
  674. {
  675. struct sifive_serial_port *ssp = sifive_serial_console_ports[co->index];
  676. unsigned long flags;
  677. unsigned int ier;
  678. int locked = 1;
  679. if (!ssp)
  680. return;
  681. local_irq_save(flags);
  682. if (ssp->port.sysrq)
  683. locked = 0;
  684. else if (oops_in_progress)
  685. locked = spin_trylock(&ssp->port.lock);
  686. else
  687. spin_lock(&ssp->port.lock);
  688. ier = __ssp_readl(ssp, SIFIVE_SERIAL_IE_OFFS);
  689. __ssp_writel(0, SIFIVE_SERIAL_IE_OFFS, ssp);
  690. uart_console_write(&ssp->port, s, count, sifive_serial_console_putchar);
  691. __ssp_writel(ier, SIFIVE_SERIAL_IE_OFFS, ssp);
  692. if (locked)
  693. spin_unlock(&ssp->port.lock);
  694. local_irq_restore(flags);
  695. }
  696. static int __init sifive_serial_console_setup(struct console *co, char *options)
  697. {
  698. struct sifive_serial_port *ssp;
  699. int baud = SIFIVE_DEFAULT_BAUD_RATE;
  700. int bits = 8;
  701. int parity = 'n';
  702. int flow = 'n';
  703. if (co->index < 0 || co->index >= SIFIVE_SERIAL_MAX_PORTS)
  704. return -ENODEV;
  705. ssp = sifive_serial_console_ports[co->index];
  706. if (!ssp)
  707. return -ENODEV;
  708. if (options)
  709. uart_parse_options(options, &baud, &parity, &bits, &flow);
  710. return uart_set_options(&ssp->port, co, baud, parity, bits, flow);
  711. }
  712. static struct uart_driver sifive_serial_uart_driver;
  713. static struct console sifive_serial_console = {
  714. .name = SIFIVE_TTY_PREFIX,
  715. .write = sifive_serial_console_write,
  716. .device = uart_console_device,
  717. .setup = sifive_serial_console_setup,
  718. .flags = CON_PRINTBUFFER,
  719. .index = -1,
  720. .data = &sifive_serial_uart_driver,
  721. };
  722. static int __init sifive_console_init(void)
  723. {
  724. register_console(&sifive_serial_console);
  725. return 0;
  726. }
  727. console_initcall(sifive_console_init);
  728. static void __ssp_add_console_port(struct sifive_serial_port *ssp)
  729. {
  730. spin_lock_init(&ssp->port.lock);
  731. sifive_serial_console_ports[ssp->port.line] = ssp;
  732. }
  733. static void __ssp_remove_console_port(struct sifive_serial_port *ssp)
  734. {
  735. sifive_serial_console_ports[ssp->port.line] = 0;
  736. }
  737. #define SIFIVE_SERIAL_CONSOLE (&sifive_serial_console)
  738. #else
  739. #define SIFIVE_SERIAL_CONSOLE NULL
  740. static void __ssp_add_console_port(struct sifive_serial_port *ssp)
  741. {}
  742. static void __ssp_remove_console_port(struct sifive_serial_port *ssp)
  743. {}
  744. #endif
  745. static const struct uart_ops sifive_serial_uops = {
  746. .tx_empty = sifive_serial_tx_empty,
  747. .set_mctrl = sifive_serial_set_mctrl,
  748. .get_mctrl = sifive_serial_get_mctrl,
  749. .stop_tx = sifive_serial_stop_tx,
  750. .start_tx = sifive_serial_start_tx,
  751. .stop_rx = sifive_serial_stop_rx,
  752. .break_ctl = sifive_serial_break_ctl,
  753. .startup = sifive_serial_startup,
  754. .shutdown = sifive_serial_shutdown,
  755. .set_termios = sifive_serial_set_termios,
  756. .type = sifive_serial_type,
  757. .release_port = sifive_serial_release_port,
  758. .request_port = sifive_serial_request_port,
  759. .config_port = sifive_serial_config_port,
  760. .verify_port = sifive_serial_verify_port,
  761. };
  762. static struct uart_driver sifive_serial_uart_driver = {
  763. .owner = THIS_MODULE,
  764. .driver_name = SIFIVE_SERIAL_NAME,
  765. .dev_name = SIFIVE_TTY_PREFIX,
  766. .nr = SIFIVE_SERIAL_MAX_PORTS,
  767. .cons = SIFIVE_SERIAL_CONSOLE,
  768. };
  769. static int sifive_serial_probe(struct platform_device *pdev)
  770. {
  771. struct sifive_serial_port *ssp;
  772. struct resource *mem;
  773. struct clk *clk;
  774. void __iomem *base;
  775. int irq, id, r;
  776. irq = platform_get_irq(pdev, 0);
  777. if (irq < 0)
  778. return -EPROBE_DEFER;
  779. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  780. base = devm_ioremap_resource(&pdev->dev, mem);
  781. if (IS_ERR(base)) {
  782. dev_err(&pdev->dev, "could not acquire device memory\n");
  783. return PTR_ERR(base);
  784. }
  785. clk = devm_clk_get(&pdev->dev, NULL);
  786. if (IS_ERR(clk)) {
  787. dev_err(&pdev->dev, "unable to find controller clock\n");
  788. return PTR_ERR(clk);
  789. }
  790. id = of_alias_get_id(pdev->dev.of_node, "serial");
  791. if (id < 0) {
  792. dev_err(&pdev->dev, "missing aliases entry\n");
  793. return id;
  794. }
  795. #ifdef CONFIG_SERIAL_SIFIVE_CONSOLE
  796. if (id > SIFIVE_SERIAL_MAX_PORTS) {
  797. dev_err(&pdev->dev, "too many UARTs (%d)\n", id);
  798. return -EINVAL;
  799. }
  800. #endif
  801. ssp = devm_kzalloc(&pdev->dev, sizeof(*ssp), GFP_KERNEL);
  802. if (!ssp)
  803. return -ENOMEM;
  804. ssp->port.dev = &pdev->dev;
  805. ssp->port.type = PORT_SIFIVE_V0;
  806. ssp->port.iotype = UPIO_MEM;
  807. ssp->port.irq = irq;
  808. ssp->port.fifosize = SIFIVE_TX_FIFO_DEPTH;
  809. ssp->port.ops = &sifive_serial_uops;
  810. ssp->port.line = id;
  811. ssp->port.mapbase = mem->start;
  812. ssp->port.membase = base;
  813. ssp->dev = &pdev->dev;
  814. ssp->clk = clk;
  815. ssp->clk_notifier.notifier_call = sifive_serial_clk_notifier;
  816. r = clk_notifier_register(ssp->clk, &ssp->clk_notifier);
  817. if (r) {
  818. dev_err(&pdev->dev, "could not register clock notifier: %d\n",
  819. r);
  820. goto probe_out1;
  821. }
  822. /* Set up clock divider */
  823. ssp->clkin_rate = clk_get_rate(ssp->clk);
  824. ssp->baud_rate = SIFIVE_DEFAULT_BAUD_RATE;
  825. ssp->port.uartclk = ssp->baud_rate * 16;
  826. __ssp_update_div(ssp);
  827. platform_set_drvdata(pdev, ssp);
  828. /* Enable transmits and set the watermark level to 1 */
  829. __ssp_writel((1 << SIFIVE_SERIAL_TXCTRL_TXCNT_SHIFT) |
  830. SIFIVE_SERIAL_TXCTRL_TXEN_MASK,
  831. SIFIVE_SERIAL_TXCTRL_OFFS, ssp);
  832. /* Enable receives and set the watermark level to 0 */
  833. __ssp_writel((0 << SIFIVE_SERIAL_RXCTRL_RXCNT_SHIFT) |
  834. SIFIVE_SERIAL_RXCTRL_RXEN_MASK,
  835. SIFIVE_SERIAL_RXCTRL_OFFS, ssp);
  836. r = request_irq(ssp->port.irq, sifive_serial_irq, ssp->port.irqflags,
  837. dev_name(&pdev->dev), ssp);
  838. if (r) {
  839. dev_err(&pdev->dev, "could not attach interrupt: %d\n", r);
  840. goto probe_out2;
  841. }
  842. __ssp_add_console_port(ssp);
  843. r = uart_add_one_port(&sifive_serial_uart_driver, &ssp->port);
  844. if (r != 0) {
  845. dev_err(&pdev->dev, "could not add uart: %d\n", r);
  846. goto probe_out3;
  847. }
  848. return 0;
  849. probe_out3:
  850. __ssp_remove_console_port(ssp);
  851. free_irq(ssp->port.irq, ssp);
  852. probe_out2:
  853. clk_notifier_unregister(ssp->clk, &ssp->clk_notifier);
  854. probe_out1:
  855. return r;
  856. }
  857. static int sifive_serial_remove(struct platform_device *dev)
  858. {
  859. struct sifive_serial_port *ssp = platform_get_drvdata(dev);
  860. __ssp_remove_console_port(ssp);
  861. uart_remove_one_port(&sifive_serial_uart_driver, &ssp->port);
  862. free_irq(ssp->port.irq, ssp);
  863. clk_notifier_unregister(ssp->clk, &ssp->clk_notifier);
  864. return 0;
  865. }
  866. static const struct of_device_id sifive_serial_of_match[] = {
  867. { .compatible = "sifive,fu540-c000-uart0" },
  868. { .compatible = "sifive,uart0" },
  869. {},
  870. };
  871. MODULE_DEVICE_TABLE(of, sifive_serial_of_match);
  872. static struct platform_driver sifive_serial_platform_driver = {
  873. .probe = sifive_serial_probe,
  874. .remove = sifive_serial_remove,
  875. .driver = {
  876. .name = SIFIVE_SERIAL_NAME,
  877. .of_match_table = of_match_ptr(sifive_serial_of_match),
  878. },
  879. };
  880. static int __init sifive_serial_init(void)
  881. {
  882. int r;
  883. r = uart_register_driver(&sifive_serial_uart_driver);
  884. if (r)
  885. goto init_out1;
  886. r = platform_driver_register(&sifive_serial_platform_driver);
  887. if (r)
  888. goto init_out2;
  889. return 0;
  890. init_out2:
  891. uart_unregister_driver(&sifive_serial_uart_driver);
  892. init_out1:
  893. return r;
  894. }
  895. static void __exit sifive_serial_exit(void)
  896. {
  897. platform_driver_unregister(&sifive_serial_platform_driver);
  898. uart_unregister_driver(&sifive_serial_uart_driver);
  899. }
  900. module_init(sifive_serial_init);
  901. module_exit(sifive_serial_exit);
  902. MODULE_DESCRIPTION("SiFive UART serial driver");
  903. MODULE_LICENSE("GPL");
  904. MODULE_AUTHOR("Paul Walmsley <paul@pwsan.com>");