sifive.c 29 KB

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