sifive.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150
  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. * 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. * @console_line_ended: indicate that the console line is fully written
  126. *
  127. * Configuration data specific to this SiFive UART.
  128. */
  129. struct sifive_serial_port {
  130. struct uart_port port;
  131. struct device *dev;
  132. unsigned char ier;
  133. unsigned long baud_rate;
  134. struct clk *clk;
  135. struct notifier_block clk_notifier;
  136. bool console_line_ended;
  137. };
  138. /*
  139. * Structure container-of macros
  140. */
  141. #define port_to_sifive_serial_port(p) (container_of((p), \
  142. struct sifive_serial_port, \
  143. port))
  144. #define notifier_to_sifive_serial_port(nb) (container_of((nb), \
  145. struct sifive_serial_port, \
  146. clk_notifier))
  147. /*
  148. * Forward declarations
  149. */
  150. static void sifive_serial_stop_tx(struct uart_port *port);
  151. /*
  152. * Internal functions
  153. */
  154. /**
  155. * __ssp_early_writel() - write to a SiFive serial port register (early)
  156. * @port: pointer to a struct uart_port record
  157. * @offs: register address offset from the IP block base address
  158. * @v: value to write to the register
  159. *
  160. * Given a pointer @port to a struct uart_port record, write the value
  161. * @v to the IP block register address offset @offs. This function is
  162. * intended for early console use.
  163. *
  164. * Context: Intended to be used only by the earlyconsole code.
  165. */
  166. static void __ssp_early_writel(u32 v, u16 offs, struct uart_port *port)
  167. {
  168. writel_relaxed(v, port->membase + offs);
  169. }
  170. /**
  171. * __ssp_early_readl() - read from a SiFive serial port register (early)
  172. * @port: pointer to a struct uart_port record
  173. * @offs: register address offset from the IP block base address
  174. *
  175. * Given a pointer @port to a struct uart_port record, read the
  176. * contents of the IP block register located at offset @offs from the
  177. * IP block base and return it. This function is intended for early
  178. * console use.
  179. *
  180. * Context: Intended to be called only by the earlyconsole code or by
  181. * __ssp_readl() or __ssp_writel() (in this driver)
  182. *
  183. * Returns: the register value read from the UART.
  184. */
  185. static u32 __ssp_early_readl(struct uart_port *port, u16 offs)
  186. {
  187. return readl_relaxed(port->membase + offs);
  188. }
  189. /**
  190. * __ssp_writel() - write to a SiFive serial port register
  191. * @v: value to write to the register
  192. * @offs: register address offset from the IP block base address
  193. * @ssp: pointer to a struct sifive_serial_port record
  194. *
  195. * Write the value @v to the IP block register located at offset @offs from the
  196. * IP block base, given a pointer @ssp to a struct sifive_serial_port record.
  197. *
  198. * Context: Any context.
  199. */
  200. static void __ssp_writel(u32 v, u16 offs, struct sifive_serial_port *ssp)
  201. {
  202. __ssp_early_writel(v, offs, &ssp->port);
  203. }
  204. /**
  205. * __ssp_readl() - read from a SiFive serial port register
  206. * @ssp: pointer to a struct sifive_serial_port record
  207. * @offs: register address offset from the IP block base address
  208. *
  209. * Read the contents of the IP block register located at offset @offs from the
  210. * IP block base, given a pointer @ssp to a struct sifive_serial_port record.
  211. *
  212. * Context: Any context.
  213. *
  214. * Returns: the value of the UART register
  215. */
  216. static u32 __ssp_readl(struct sifive_serial_port *ssp, u16 offs)
  217. {
  218. return __ssp_early_readl(&ssp->port, offs);
  219. }
  220. /**
  221. * sifive_serial_is_txfifo_full() - is the TXFIFO full?
  222. * @ssp: pointer to a struct sifive_serial_port
  223. *
  224. * Read the transmit FIFO "full" bit, returning a non-zero value if the
  225. * TX FIFO is full, or zero if space remains. Intended to be used to prevent
  226. * writes to the TX FIFO when it's full.
  227. *
  228. * Returns: SIFIVE_SERIAL_TXDATA_FULL_MASK (non-zero) if the transmit FIFO
  229. * is full, or 0 if space remains.
  230. */
  231. static int sifive_serial_is_txfifo_full(struct sifive_serial_port *ssp)
  232. {
  233. return __ssp_readl(ssp, SIFIVE_SERIAL_TXDATA_OFFS) &
  234. SIFIVE_SERIAL_TXDATA_FULL_MASK;
  235. }
  236. /**
  237. * __ssp_transmit_char() - enqueue a byte to transmit onto the TX FIFO
  238. * @ssp: pointer to a struct sifive_serial_port
  239. * @ch: character to transmit
  240. *
  241. * Enqueue a byte @ch onto the transmit FIFO, given a pointer @ssp to the
  242. * struct sifive_serial_port * to transmit on. Caller should first check to
  243. * ensure that the TXFIFO has space; see sifive_serial_is_txfifo_full().
  244. *
  245. * Context: Any context.
  246. */
  247. static void __ssp_transmit_char(struct sifive_serial_port *ssp, int ch)
  248. {
  249. __ssp_writel(ch, SIFIVE_SERIAL_TXDATA_OFFS, ssp);
  250. }
  251. /**
  252. * __ssp_transmit_chars() - enqueue multiple bytes onto the TX FIFO
  253. * @ssp: pointer to a struct sifive_serial_port
  254. *
  255. * Transfer up to a TX FIFO size's worth of characters from the Linux serial
  256. * transmit buffer to the SiFive UART TX FIFO.
  257. *
  258. * Context: Any context. Expects @ssp->port.lock to be held by caller.
  259. */
  260. static void __ssp_transmit_chars(struct sifive_serial_port *ssp)
  261. {
  262. u8 ch;
  263. uart_port_tx_limited(&ssp->port, ch, SIFIVE_TX_FIFO_DEPTH,
  264. true,
  265. __ssp_transmit_char(ssp, ch),
  266. ({}));
  267. }
  268. /**
  269. * __ssp_enable_txwm() - enable transmit watermark interrupts
  270. * @ssp: pointer to a struct sifive_serial_port
  271. *
  272. * Enable interrupt generation when the transmit FIFO watermark is reached
  273. * on the SiFive UART referred to by @ssp.
  274. */
  275. static void __ssp_enable_txwm(struct sifive_serial_port *ssp)
  276. {
  277. if (ssp->ier & SIFIVE_SERIAL_IE_TXWM_MASK)
  278. return;
  279. ssp->ier |= SIFIVE_SERIAL_IE_TXWM_MASK;
  280. __ssp_writel(ssp->ier, SIFIVE_SERIAL_IE_OFFS, ssp);
  281. }
  282. /**
  283. * __ssp_enable_rxwm() - enable receive watermark interrupts
  284. * @ssp: pointer to a struct sifive_serial_port
  285. *
  286. * Enable interrupt generation when the receive FIFO watermark is reached
  287. * on the SiFive UART referred to by @ssp.
  288. */
  289. static void __ssp_enable_rxwm(struct sifive_serial_port *ssp)
  290. {
  291. if (ssp->ier & SIFIVE_SERIAL_IE_RXWM_MASK)
  292. return;
  293. ssp->ier |= SIFIVE_SERIAL_IE_RXWM_MASK;
  294. __ssp_writel(ssp->ier, SIFIVE_SERIAL_IE_OFFS, ssp);
  295. }
  296. /**
  297. * __ssp_disable_txwm() - disable transmit watermark interrupts
  298. * @ssp: pointer to a struct sifive_serial_port
  299. *
  300. * Disable interrupt generation when the transmit FIFO watermark is reached
  301. * on the UART referred to by @ssp.
  302. */
  303. static void __ssp_disable_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_disable_rxwm() - disable receive watermark interrupts
  312. * @ssp: pointer to a struct sifive_serial_port
  313. *
  314. * Disable interrupt generation when the receive FIFO watermark is reached
  315. * on the UART referred to by @ssp.
  316. */
  317. static void __ssp_disable_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_receive_char() - receive a byte from the UART
  326. * @ssp: pointer to a struct sifive_serial_port
  327. * @is_empty: char pointer to return whether the RX FIFO is empty
  328. *
  329. * Try to read a byte from the SiFive UART RX FIFO, referenced by
  330. * @ssp, and to return it. Also returns the RX FIFO empty bit in
  331. * the char pointed to by @ch. The caller must pass the byte back to the
  332. * Linux serial layer if needed.
  333. *
  334. * Returns: the byte read from the UART RX FIFO.
  335. */
  336. static char __ssp_receive_char(struct sifive_serial_port *ssp, char *is_empty)
  337. {
  338. u32 v;
  339. u8 ch;
  340. v = __ssp_readl(ssp, SIFIVE_SERIAL_RXDATA_OFFS);
  341. if (!is_empty)
  342. WARN_ON(1);
  343. else
  344. *is_empty = (v & SIFIVE_SERIAL_RXDATA_EMPTY_MASK) >>
  345. SIFIVE_SERIAL_RXDATA_EMPTY_SHIFT;
  346. ch = (v & SIFIVE_SERIAL_RXDATA_DATA_MASK) >>
  347. SIFIVE_SERIAL_RXDATA_DATA_SHIFT;
  348. return ch;
  349. }
  350. /**
  351. * __ssp_receive_chars() - receive multiple bytes from the UART
  352. * @ssp: pointer to a struct sifive_serial_port
  353. *
  354. * Receive up to an RX FIFO's worth of bytes from the SiFive UART referred
  355. * to by @ssp and pass them up to the Linux serial layer.
  356. *
  357. * Context: Expects ssp->port.lock to be held by caller.
  358. */
  359. static void __ssp_receive_chars(struct sifive_serial_port *ssp)
  360. {
  361. char is_empty;
  362. int c;
  363. u8 ch;
  364. for (c = SIFIVE_RX_FIFO_DEPTH; c > 0; --c) {
  365. ch = __ssp_receive_char(ssp, &is_empty);
  366. if (is_empty)
  367. break;
  368. ssp->port.icount.rx++;
  369. if (!uart_prepare_sysrq_char(&ssp->port, ch))
  370. uart_insert_char(&ssp->port, 0, 0, ch, TTY_NORMAL);
  371. }
  372. tty_flip_buffer_push(&ssp->port.state->port);
  373. }
  374. /**
  375. * __ssp_update_div() - calculate the divisor setting by the line rate
  376. * @ssp: pointer to a struct sifive_serial_port
  377. *
  378. * Calculate the appropriate value of the clock divisor for the UART
  379. * and target line rate referred to by @ssp and write it into the
  380. * hardware.
  381. */
  382. static void __ssp_update_div(struct sifive_serial_port *ssp)
  383. {
  384. u16 div;
  385. div = DIV_ROUND_UP(ssp->port.uartclk, ssp->baud_rate) - 1;
  386. __ssp_writel(div, SIFIVE_SERIAL_DIV_OFFS, ssp);
  387. }
  388. /**
  389. * __ssp_update_baud_rate() - set the UART "baud rate"
  390. * @ssp: pointer to a struct sifive_serial_port
  391. * @rate: new target bit rate
  392. *
  393. * Calculate the UART divisor value for the target bit rate @rate for the
  394. * SiFive UART described by @ssp and program it into the UART. There may
  395. * be some error between the target bit rate and the actual bit rate implemented
  396. * by the UART due to clock ratio granularity.
  397. */
  398. static void __ssp_update_baud_rate(struct sifive_serial_port *ssp,
  399. unsigned int rate)
  400. {
  401. if (ssp->baud_rate == rate)
  402. return;
  403. ssp->baud_rate = rate;
  404. __ssp_update_div(ssp);
  405. }
  406. /**
  407. * __ssp_set_stop_bits() - set the number of stop bits
  408. * @ssp: pointer to a struct sifive_serial_port
  409. * @nstop: 1 or 2 (stop bits)
  410. *
  411. * Program the SiFive UART referred to by @ssp to use @nstop stop bits.
  412. */
  413. static void __ssp_set_stop_bits(struct sifive_serial_port *ssp, char nstop)
  414. {
  415. u32 v;
  416. if (nstop < 1 || nstop > 2) {
  417. WARN_ON(1);
  418. return;
  419. }
  420. v = __ssp_readl(ssp, SIFIVE_SERIAL_TXCTRL_OFFS);
  421. v &= ~SIFIVE_SERIAL_TXCTRL_NSTOP_MASK;
  422. v |= (nstop - 1) << SIFIVE_SERIAL_TXCTRL_NSTOP_SHIFT;
  423. __ssp_writel(v, SIFIVE_SERIAL_TXCTRL_OFFS, ssp);
  424. }
  425. /**
  426. * __ssp_wait_for_xmitr() - wait for an empty slot on the TX FIFO
  427. * @ssp: pointer to a struct sifive_serial_port
  428. *
  429. * Delay while the UART TX FIFO referred to by @ssp is marked as full.
  430. *
  431. * Context: Any context.
  432. */
  433. static void __maybe_unused __ssp_wait_for_xmitr(struct sifive_serial_port *ssp)
  434. {
  435. while (sifive_serial_is_txfifo_full(ssp))
  436. udelay(1); /* XXX Could probably be more intelligent here */
  437. }
  438. /*
  439. * Linux serial API functions
  440. */
  441. static void sifive_serial_stop_tx(struct uart_port *port)
  442. {
  443. struct sifive_serial_port *ssp = port_to_sifive_serial_port(port);
  444. __ssp_disable_txwm(ssp);
  445. }
  446. static void sifive_serial_stop_rx(struct uart_port *port)
  447. {
  448. struct sifive_serial_port *ssp = port_to_sifive_serial_port(port);
  449. __ssp_disable_rxwm(ssp);
  450. }
  451. static void sifive_serial_start_tx(struct uart_port *port)
  452. {
  453. struct sifive_serial_port *ssp = port_to_sifive_serial_port(port);
  454. __ssp_enable_txwm(ssp);
  455. }
  456. static irqreturn_t sifive_serial_irq(int irq, void *dev_id)
  457. {
  458. struct sifive_serial_port *ssp = dev_id;
  459. u32 ip;
  460. uart_port_lock(&ssp->port);
  461. ip = __ssp_readl(ssp, SIFIVE_SERIAL_IP_OFFS);
  462. if (!ip) {
  463. uart_port_unlock(&ssp->port);
  464. return IRQ_NONE;
  465. }
  466. if (ip & SIFIVE_SERIAL_IP_RXWM_MASK)
  467. __ssp_receive_chars(ssp);
  468. if (ip & SIFIVE_SERIAL_IP_TXWM_MASK)
  469. __ssp_transmit_chars(ssp);
  470. uart_unlock_and_check_sysrq(&ssp->port);
  471. return IRQ_HANDLED;
  472. }
  473. static unsigned int sifive_serial_tx_empty(struct uart_port *port)
  474. {
  475. return TIOCSER_TEMT;
  476. }
  477. static unsigned int sifive_serial_get_mctrl(struct uart_port *port)
  478. {
  479. return TIOCM_CAR | TIOCM_CTS | TIOCM_DSR;
  480. }
  481. static void sifive_serial_set_mctrl(struct uart_port *port, unsigned int mctrl)
  482. {
  483. /* IP block does not support these signals */
  484. }
  485. static void sifive_serial_break_ctl(struct uart_port *port, int break_state)
  486. {
  487. /* IP block does not support sending a break */
  488. }
  489. static int sifive_serial_startup(struct uart_port *port)
  490. {
  491. struct sifive_serial_port *ssp = port_to_sifive_serial_port(port);
  492. unsigned long flags;
  493. uart_port_lock_irqsave(&ssp->port, &flags);
  494. __ssp_enable_rxwm(ssp);
  495. uart_port_unlock_irqrestore(&ssp->port, flags);
  496. return 0;
  497. }
  498. static void sifive_serial_shutdown(struct uart_port *port)
  499. {
  500. struct sifive_serial_port *ssp = port_to_sifive_serial_port(port);
  501. unsigned long flags;
  502. uart_port_lock_irqsave(&ssp->port, &flags);
  503. __ssp_disable_rxwm(ssp);
  504. __ssp_disable_txwm(ssp);
  505. uart_port_unlock_irqrestore(&ssp->port, flags);
  506. }
  507. /**
  508. * sifive_serial_clk_notifier() - clock post-rate-change notifier
  509. * @nb: pointer to the struct notifier_block, from the notifier code
  510. * @event: event mask from the notifier code
  511. * @data: pointer to the struct clk_notifier_data from the notifier code
  512. *
  513. * On the V0 SoC, the UART IP block is derived from the CPU clock source
  514. * after a synchronous divide-by-two divider, so any CPU clock rate change
  515. * requires the UART baud rate to be updated. This presumably corrupts any
  516. * serial word currently being transmitted or received. In order to avoid
  517. * corrupting the output data stream, we drain the transmit queue before
  518. * allowing the clock's rate to be changed.
  519. */
  520. static int sifive_serial_clk_notifier(struct notifier_block *nb,
  521. unsigned long event, void *data)
  522. {
  523. struct clk_notifier_data *cnd = data;
  524. struct sifive_serial_port *ssp = notifier_to_sifive_serial_port(nb);
  525. if (event == PRE_RATE_CHANGE) {
  526. /*
  527. * The TX watermark is always set to 1 by this driver, which
  528. * means that the TX busy bit will lower when there are 0 bytes
  529. * left in the TX queue -- in other words, when the TX FIFO is
  530. * empty.
  531. */
  532. __ssp_wait_for_xmitr(ssp);
  533. /*
  534. * On the cycle the TX FIFO goes empty there is still a full
  535. * UART frame left to be transmitted in the shift register.
  536. * The UART provides no way for software to directly determine
  537. * when that last frame has been transmitted, so we just sleep
  538. * here instead. As we're not tracking the number of stop bits
  539. * they're just worst cased here. The rest of the serial
  540. * framing parameters aren't configurable by software.
  541. */
  542. udelay(DIV_ROUND_UP(12 * 1000 * 1000, ssp->baud_rate));
  543. }
  544. if (event == POST_RATE_CHANGE && ssp->port.uartclk != cnd->new_rate) {
  545. ssp->port.uartclk = cnd->new_rate;
  546. __ssp_update_div(ssp);
  547. }
  548. return NOTIFY_OK;
  549. }
  550. static void sifive_serial_set_termios(struct uart_port *port,
  551. struct ktermios *termios,
  552. const struct ktermios *old)
  553. {
  554. struct sifive_serial_port *ssp = port_to_sifive_serial_port(port);
  555. unsigned long flags;
  556. u32 v, old_v;
  557. int rate;
  558. char nstop;
  559. if ((termios->c_cflag & CSIZE) != CS8) {
  560. dev_err_once(ssp->port.dev, "only 8-bit words supported\n");
  561. termios->c_cflag &= ~CSIZE;
  562. termios->c_cflag |= CS8;
  563. }
  564. if (termios->c_iflag & (INPCK | PARMRK))
  565. dev_err_once(ssp->port.dev, "parity checking not supported\n");
  566. if (termios->c_iflag & BRKINT)
  567. dev_err_once(ssp->port.dev, "BREAK detection not supported\n");
  568. termios->c_iflag &= ~(INPCK|PARMRK|BRKINT);
  569. /* Set number of stop bits */
  570. nstop = (termios->c_cflag & CSTOPB) ? 2 : 1;
  571. __ssp_set_stop_bits(ssp, nstop);
  572. /* Set line rate */
  573. rate = uart_get_baud_rate(port, termios, old, 0,
  574. ssp->port.uartclk / 16);
  575. __ssp_update_baud_rate(ssp, rate);
  576. uart_port_lock_irqsave(&ssp->port, &flags);
  577. /* Update the per-port timeout */
  578. uart_update_timeout(port, termios->c_cflag, rate);
  579. ssp->port.read_status_mask = 0;
  580. /* Ignore all characters if CREAD is not set */
  581. v = __ssp_readl(ssp, SIFIVE_SERIAL_RXCTRL_OFFS);
  582. old_v = v;
  583. if ((termios->c_cflag & CREAD) == 0)
  584. v &= SIFIVE_SERIAL_RXCTRL_RXEN_MASK;
  585. else
  586. v |= SIFIVE_SERIAL_RXCTRL_RXEN_MASK;
  587. if (v != old_v)
  588. __ssp_writel(v, SIFIVE_SERIAL_RXCTRL_OFFS, ssp);
  589. uart_port_unlock_irqrestore(&ssp->port, flags);
  590. }
  591. static void sifive_serial_release_port(struct uart_port *port)
  592. {
  593. }
  594. static int sifive_serial_request_port(struct uart_port *port)
  595. {
  596. return 0;
  597. }
  598. static void sifive_serial_config_port(struct uart_port *port, int flags)
  599. {
  600. struct sifive_serial_port *ssp = port_to_sifive_serial_port(port);
  601. ssp->port.type = PORT_SIFIVE_V0;
  602. }
  603. static int sifive_serial_verify_port(struct uart_port *port,
  604. struct serial_struct *ser)
  605. {
  606. return -EINVAL;
  607. }
  608. static const char *sifive_serial_type(struct uart_port *port)
  609. {
  610. return port->type == PORT_SIFIVE_V0 ? "SiFive UART v0" : NULL;
  611. }
  612. #ifdef CONFIG_CONSOLE_POLL
  613. static int sifive_serial_poll_get_char(struct uart_port *port)
  614. {
  615. struct sifive_serial_port *ssp = port_to_sifive_serial_port(port);
  616. char is_empty, ch;
  617. ch = __ssp_receive_char(ssp, &is_empty);
  618. if (is_empty)
  619. return NO_POLL_CHAR;
  620. return ch;
  621. }
  622. static void sifive_serial_poll_put_char(struct uart_port *port,
  623. unsigned char c)
  624. {
  625. struct sifive_serial_port *ssp = port_to_sifive_serial_port(port);
  626. __ssp_wait_for_xmitr(ssp);
  627. __ssp_transmit_char(ssp, c);
  628. }
  629. #endif /* CONFIG_CONSOLE_POLL */
  630. /*
  631. * Early console support
  632. */
  633. #ifdef CONFIG_SERIAL_EARLYCON
  634. static void early_sifive_serial_putc(struct uart_port *port, unsigned char 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-uart",
  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, unsigned char 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. ssp->console_line_ended = (ch == '\n');
  672. }
  673. static void sifive_serial_device_lock(struct console *co, unsigned long *flags)
  674. {
  675. struct uart_port *up = &sifive_serial_console_ports[co->index]->port;
  676. __uart_port_lock_irqsave(up, flags);
  677. }
  678. static void sifive_serial_device_unlock(struct console *co, unsigned long flags)
  679. {
  680. struct uart_port *up = &sifive_serial_console_ports[co->index]->port;
  681. __uart_port_unlock_irqrestore(up, flags);
  682. }
  683. static void sifive_serial_console_write_atomic(struct console *co,
  684. struct nbcon_write_context *wctxt)
  685. {
  686. struct sifive_serial_port *ssp = sifive_serial_console_ports[co->index];
  687. struct uart_port *port = &ssp->port;
  688. unsigned int ier;
  689. if (!ssp)
  690. return;
  691. if (!nbcon_enter_unsafe(wctxt))
  692. return;
  693. ier = __ssp_readl(ssp, SIFIVE_SERIAL_IE_OFFS);
  694. __ssp_writel(0, SIFIVE_SERIAL_IE_OFFS, ssp);
  695. if (!ssp->console_line_ended)
  696. uart_console_write(port, "\n", 1, sifive_serial_console_putchar);
  697. uart_console_write(port, wctxt->outbuf, wctxt->len,
  698. sifive_serial_console_putchar);
  699. __ssp_writel(ier, SIFIVE_SERIAL_IE_OFFS, ssp);
  700. nbcon_exit_unsafe(wctxt);
  701. }
  702. static void sifive_serial_console_write_thread(struct console *co,
  703. struct nbcon_write_context *wctxt)
  704. {
  705. struct sifive_serial_port *ssp = sifive_serial_console_ports[co->index];
  706. struct uart_port *port = &ssp->port;
  707. unsigned int ier;
  708. if (!ssp)
  709. return;
  710. if (!nbcon_enter_unsafe(wctxt))
  711. return;
  712. ier = __ssp_readl(ssp, SIFIVE_SERIAL_IE_OFFS);
  713. __ssp_writel(0, SIFIVE_SERIAL_IE_OFFS, ssp);
  714. if (nbcon_exit_unsafe(wctxt)) {
  715. int len = READ_ONCE(wctxt->len);
  716. int i;
  717. for (i = 0; i < len; i++) {
  718. if (!nbcon_enter_unsafe(wctxt))
  719. break;
  720. uart_console_write(port, wctxt->outbuf + i, 1,
  721. sifive_serial_console_putchar);
  722. if (!nbcon_exit_unsafe(wctxt))
  723. break;
  724. }
  725. }
  726. while (!nbcon_enter_unsafe(wctxt))
  727. nbcon_reacquire_nobuf(wctxt);
  728. __ssp_writel(ier, SIFIVE_SERIAL_IE_OFFS, ssp);
  729. nbcon_exit_unsafe(wctxt);
  730. }
  731. static int sifive_serial_console_setup(struct console *co, char *options)
  732. {
  733. struct sifive_serial_port *ssp;
  734. int baud = SIFIVE_DEFAULT_BAUD_RATE;
  735. int bits = 8;
  736. int parity = 'n';
  737. int flow = 'n';
  738. if (co->index < 0 || co->index >= SIFIVE_SERIAL_MAX_PORTS)
  739. return -ENODEV;
  740. ssp = sifive_serial_console_ports[co->index];
  741. if (!ssp)
  742. return -ENODEV;
  743. ssp->console_line_ended = true;
  744. if (options)
  745. uart_parse_options(options, &baud, &parity, &bits, &flow);
  746. return uart_set_options(&ssp->port, co, baud, parity, bits, flow);
  747. }
  748. static struct uart_driver sifive_serial_uart_driver;
  749. static struct console sifive_serial_console = {
  750. .name = SIFIVE_TTY_PREFIX,
  751. .write_atomic = sifive_serial_console_write_atomic,
  752. .write_thread = sifive_serial_console_write_thread,
  753. .device_lock = sifive_serial_device_lock,
  754. .device_unlock = sifive_serial_device_unlock,
  755. .device = uart_console_device,
  756. .setup = sifive_serial_console_setup,
  757. .flags = CON_PRINTBUFFER | CON_NBCON,
  758. .index = -1,
  759. .data = &sifive_serial_uart_driver,
  760. };
  761. static int __init sifive_console_init(void)
  762. {
  763. register_console(&sifive_serial_console);
  764. return 0;
  765. }
  766. console_initcall(sifive_console_init);
  767. static void __ssp_add_console_port(struct sifive_serial_port *ssp)
  768. {
  769. sifive_serial_console_ports[ssp->port.line] = ssp;
  770. }
  771. static void __ssp_remove_console_port(struct sifive_serial_port *ssp)
  772. {
  773. sifive_serial_console_ports[ssp->port.line] = NULL;
  774. }
  775. #define SIFIVE_SERIAL_CONSOLE (&sifive_serial_console)
  776. #else
  777. #define SIFIVE_SERIAL_CONSOLE NULL
  778. static void __ssp_add_console_port(struct sifive_serial_port *ssp)
  779. {}
  780. static void __ssp_remove_console_port(struct sifive_serial_port *ssp)
  781. {}
  782. #endif
  783. static const struct uart_ops sifive_serial_uops = {
  784. .tx_empty = sifive_serial_tx_empty,
  785. .set_mctrl = sifive_serial_set_mctrl,
  786. .get_mctrl = sifive_serial_get_mctrl,
  787. .stop_tx = sifive_serial_stop_tx,
  788. .start_tx = sifive_serial_start_tx,
  789. .stop_rx = sifive_serial_stop_rx,
  790. .break_ctl = sifive_serial_break_ctl,
  791. .startup = sifive_serial_startup,
  792. .shutdown = sifive_serial_shutdown,
  793. .set_termios = sifive_serial_set_termios,
  794. .type = sifive_serial_type,
  795. .release_port = sifive_serial_release_port,
  796. .request_port = sifive_serial_request_port,
  797. .config_port = sifive_serial_config_port,
  798. .verify_port = sifive_serial_verify_port,
  799. #ifdef CONFIG_CONSOLE_POLL
  800. .poll_get_char = sifive_serial_poll_get_char,
  801. .poll_put_char = sifive_serial_poll_put_char,
  802. #endif
  803. };
  804. static struct uart_driver sifive_serial_uart_driver = {
  805. .owner = THIS_MODULE,
  806. .driver_name = SIFIVE_SERIAL_NAME,
  807. .dev_name = SIFIVE_TTY_PREFIX,
  808. .nr = SIFIVE_SERIAL_MAX_PORTS,
  809. .cons = SIFIVE_SERIAL_CONSOLE,
  810. };
  811. static int sifive_serial_probe(struct platform_device *pdev)
  812. {
  813. struct sifive_serial_port *ssp;
  814. struct resource *mem;
  815. struct clk *clk;
  816. void __iomem *base;
  817. int irq, id, r;
  818. irq = platform_get_irq(pdev, 0);
  819. if (irq < 0)
  820. return -EPROBE_DEFER;
  821. base = devm_platform_get_and_ioremap_resource(pdev, 0, &mem);
  822. if (IS_ERR(base))
  823. return PTR_ERR(base);
  824. clk = devm_clk_get_enabled(&pdev->dev, NULL);
  825. if (IS_ERR(clk)) {
  826. dev_err(&pdev->dev, "unable to find controller clock\n");
  827. return PTR_ERR(clk);
  828. }
  829. id = of_alias_get_id(pdev->dev.of_node, "serial");
  830. if (id < 0) {
  831. dev_err(&pdev->dev, "missing aliases entry\n");
  832. return id;
  833. }
  834. #ifdef CONFIG_SERIAL_SIFIVE_CONSOLE
  835. if (id > SIFIVE_SERIAL_MAX_PORTS) {
  836. dev_err(&pdev->dev, "too many UARTs (%d)\n", id);
  837. return -EINVAL;
  838. }
  839. #endif
  840. ssp = devm_kzalloc(&pdev->dev, sizeof(*ssp), GFP_KERNEL);
  841. if (!ssp)
  842. return -ENOMEM;
  843. ssp->port.dev = &pdev->dev;
  844. ssp->port.type = PORT_SIFIVE_V0;
  845. ssp->port.iotype = UPIO_MEM;
  846. ssp->port.irq = irq;
  847. ssp->port.fifosize = SIFIVE_TX_FIFO_DEPTH;
  848. ssp->port.ops = &sifive_serial_uops;
  849. ssp->port.line = id;
  850. ssp->port.mapbase = mem->start;
  851. ssp->port.membase = base;
  852. ssp->dev = &pdev->dev;
  853. ssp->clk = clk;
  854. ssp->clk_notifier.notifier_call = sifive_serial_clk_notifier;
  855. r = clk_notifier_register(ssp->clk, &ssp->clk_notifier);
  856. if (r) {
  857. dev_err(&pdev->dev, "could not register clock notifier: %d\n",
  858. r);
  859. goto probe_out1;
  860. }
  861. /* Set up clock divider */
  862. ssp->port.uartclk = clk_get_rate(ssp->clk);
  863. ssp->baud_rate = SIFIVE_DEFAULT_BAUD_RATE;
  864. __ssp_update_div(ssp);
  865. platform_set_drvdata(pdev, ssp);
  866. /* Enable transmits and set the watermark level to 1 */
  867. __ssp_writel((1 << SIFIVE_SERIAL_TXCTRL_TXCNT_SHIFT) |
  868. SIFIVE_SERIAL_TXCTRL_TXEN_MASK,
  869. SIFIVE_SERIAL_TXCTRL_OFFS, ssp);
  870. /* Enable receives and set the watermark level to 0 */
  871. __ssp_writel((0 << SIFIVE_SERIAL_RXCTRL_RXCNT_SHIFT) |
  872. SIFIVE_SERIAL_RXCTRL_RXEN_MASK,
  873. SIFIVE_SERIAL_RXCTRL_OFFS, ssp);
  874. r = request_irq(ssp->port.irq, sifive_serial_irq, ssp->port.irqflags,
  875. dev_name(&pdev->dev), ssp);
  876. if (r) {
  877. dev_err(&pdev->dev, "could not attach interrupt: %d\n", r);
  878. goto probe_out2;
  879. }
  880. __ssp_add_console_port(ssp);
  881. r = uart_add_one_port(&sifive_serial_uart_driver, &ssp->port);
  882. if (r != 0) {
  883. dev_err(&pdev->dev, "could not add uart: %d\n", r);
  884. goto probe_out3;
  885. }
  886. return 0;
  887. probe_out3:
  888. __ssp_remove_console_port(ssp);
  889. free_irq(ssp->port.irq, ssp);
  890. probe_out2:
  891. clk_notifier_unregister(ssp->clk, &ssp->clk_notifier);
  892. probe_out1:
  893. return r;
  894. }
  895. static void sifive_serial_remove(struct platform_device *dev)
  896. {
  897. struct sifive_serial_port *ssp = platform_get_drvdata(dev);
  898. __ssp_remove_console_port(ssp);
  899. uart_remove_one_port(&sifive_serial_uart_driver, &ssp->port);
  900. free_irq(ssp->port.irq, ssp);
  901. clk_notifier_unregister(ssp->clk, &ssp->clk_notifier);
  902. }
  903. static int sifive_serial_suspend(struct device *dev)
  904. {
  905. struct sifive_serial_port *ssp = dev_get_drvdata(dev);
  906. return uart_suspend_port(&sifive_serial_uart_driver, &ssp->port);
  907. }
  908. static int sifive_serial_resume(struct device *dev)
  909. {
  910. struct sifive_serial_port *ssp = dev_get_drvdata(dev);
  911. return uart_resume_port(&sifive_serial_uart_driver, &ssp->port);
  912. }
  913. static DEFINE_SIMPLE_DEV_PM_OPS(sifive_uart_pm_ops, sifive_serial_suspend,
  914. sifive_serial_resume);
  915. static const struct of_device_id sifive_serial_of_match[] = {
  916. { .compatible = "sifive,fu540-c000-uart" },
  917. { .compatible = "sifive,uart0" },
  918. {},
  919. };
  920. MODULE_DEVICE_TABLE(of, sifive_serial_of_match);
  921. static struct platform_driver sifive_serial_platform_driver = {
  922. .probe = sifive_serial_probe,
  923. .remove = sifive_serial_remove,
  924. .driver = {
  925. .name = SIFIVE_SERIAL_NAME,
  926. .pm = pm_sleep_ptr(&sifive_uart_pm_ops),
  927. .of_match_table = sifive_serial_of_match,
  928. },
  929. };
  930. static int __init sifive_serial_init(void)
  931. {
  932. int r;
  933. r = uart_register_driver(&sifive_serial_uart_driver);
  934. if (r)
  935. goto init_out1;
  936. r = platform_driver_register(&sifive_serial_platform_driver);
  937. if (r)
  938. goto init_out2;
  939. return 0;
  940. init_out2:
  941. uart_unregister_driver(&sifive_serial_uart_driver);
  942. init_out1:
  943. return r;
  944. }
  945. static void __exit sifive_serial_exit(void)
  946. {
  947. platform_driver_unregister(&sifive_serial_platform_driver);
  948. uart_unregister_driver(&sifive_serial_uart_driver);
  949. }
  950. module_init(sifive_serial_init);
  951. module_exit(sifive_serial_exit);
  952. MODULE_DESCRIPTION("SiFive UART serial driver");
  953. MODULE_LICENSE("GPL");
  954. MODULE_AUTHOR("Paul Walmsley <paul@pwsan.com>");