sifive.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088
  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. *
  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. u8 ch;
  261. uart_port_tx_limited(&ssp->port, ch, SIFIVE_TX_FIFO_DEPTH,
  262. true,
  263. __ssp_transmit_char(ssp, ch),
  264. ({}));
  265. }
  266. /**
  267. * __ssp_enable_txwm() - enable transmit watermark interrupts
  268. * @ssp: pointer to a struct sifive_serial_port
  269. *
  270. * Enable interrupt generation when the transmit FIFO watermark is reached
  271. * on the SiFive UART referred to by @ssp.
  272. */
  273. static void __ssp_enable_txwm(struct sifive_serial_port *ssp)
  274. {
  275. if (ssp->ier & SIFIVE_SERIAL_IE_TXWM_MASK)
  276. return;
  277. ssp->ier |= SIFIVE_SERIAL_IE_TXWM_MASK;
  278. __ssp_writel(ssp->ier, SIFIVE_SERIAL_IE_OFFS, ssp);
  279. }
  280. /**
  281. * __ssp_enable_rxwm() - enable receive watermark interrupts
  282. * @ssp: pointer to a struct sifive_serial_port
  283. *
  284. * Enable interrupt generation when the receive FIFO watermark is reached
  285. * on the SiFive UART referred to by @ssp.
  286. */
  287. static void __ssp_enable_rxwm(struct sifive_serial_port *ssp)
  288. {
  289. if (ssp->ier & SIFIVE_SERIAL_IE_RXWM_MASK)
  290. return;
  291. ssp->ier |= SIFIVE_SERIAL_IE_RXWM_MASK;
  292. __ssp_writel(ssp->ier, SIFIVE_SERIAL_IE_OFFS, ssp);
  293. }
  294. /**
  295. * __ssp_disable_txwm() - disable transmit watermark interrupts
  296. * @ssp: pointer to a struct sifive_serial_port
  297. *
  298. * Disable interrupt generation when the transmit FIFO watermark is reached
  299. * on the UART referred to by @ssp.
  300. */
  301. static void __ssp_disable_txwm(struct sifive_serial_port *ssp)
  302. {
  303. if (!(ssp->ier & SIFIVE_SERIAL_IE_TXWM_MASK))
  304. return;
  305. ssp->ier &= ~SIFIVE_SERIAL_IE_TXWM_MASK;
  306. __ssp_writel(ssp->ier, SIFIVE_SERIAL_IE_OFFS, ssp);
  307. }
  308. /**
  309. * __ssp_disable_rxwm() - disable receive watermark interrupts
  310. * @ssp: pointer to a struct sifive_serial_port
  311. *
  312. * Disable interrupt generation when the receive FIFO watermark is reached
  313. * on the UART referred to by @ssp.
  314. */
  315. static void __ssp_disable_rxwm(struct sifive_serial_port *ssp)
  316. {
  317. if (!(ssp->ier & SIFIVE_SERIAL_IE_RXWM_MASK))
  318. return;
  319. ssp->ier &= ~SIFIVE_SERIAL_IE_RXWM_MASK;
  320. __ssp_writel(ssp->ier, SIFIVE_SERIAL_IE_OFFS, ssp);
  321. }
  322. /**
  323. * __ssp_receive_char() - receive a byte from the UART
  324. * @ssp: pointer to a struct sifive_serial_port
  325. * @is_empty: char pointer to return whether the RX FIFO is empty
  326. *
  327. * Try to read a byte from the SiFive UART RX FIFO, referenced by
  328. * @ssp, and to return it. Also returns the RX FIFO empty bit in
  329. * the char pointed to by @ch. The caller must pass the byte back to the
  330. * Linux serial layer if needed.
  331. *
  332. * Returns: the byte read from the UART RX FIFO.
  333. */
  334. static char __ssp_receive_char(struct sifive_serial_port *ssp, char *is_empty)
  335. {
  336. u32 v;
  337. u8 ch;
  338. v = __ssp_readl(ssp, SIFIVE_SERIAL_RXDATA_OFFS);
  339. if (!is_empty)
  340. WARN_ON(1);
  341. else
  342. *is_empty = (v & SIFIVE_SERIAL_RXDATA_EMPTY_MASK) >>
  343. SIFIVE_SERIAL_RXDATA_EMPTY_SHIFT;
  344. ch = (v & SIFIVE_SERIAL_RXDATA_DATA_MASK) >>
  345. SIFIVE_SERIAL_RXDATA_DATA_SHIFT;
  346. return ch;
  347. }
  348. /**
  349. * __ssp_receive_chars() - receive multiple bytes from the UART
  350. * @ssp: pointer to a struct sifive_serial_port
  351. *
  352. * Receive up to an RX FIFO's worth of bytes from the SiFive UART referred
  353. * to by @ssp and pass them up to the Linux serial layer.
  354. *
  355. * Context: Expects ssp->port.lock to be held by caller.
  356. */
  357. static void __ssp_receive_chars(struct sifive_serial_port *ssp)
  358. {
  359. char is_empty;
  360. int c;
  361. u8 ch;
  362. for (c = SIFIVE_RX_FIFO_DEPTH; c > 0; --c) {
  363. ch = __ssp_receive_char(ssp, &is_empty);
  364. if (is_empty)
  365. break;
  366. ssp->port.icount.rx++;
  367. if (!uart_prepare_sysrq_char(&ssp->port, ch))
  368. uart_insert_char(&ssp->port, 0, 0, ch, TTY_NORMAL);
  369. }
  370. tty_flip_buffer_push(&ssp->port.state->port);
  371. }
  372. /**
  373. * __ssp_update_div() - calculate the divisor setting by the line rate
  374. * @ssp: pointer to a struct sifive_serial_port
  375. *
  376. * Calculate the appropriate value of the clock divisor for the UART
  377. * and target line rate referred to by @ssp and write it into the
  378. * hardware.
  379. */
  380. static void __ssp_update_div(struct sifive_serial_port *ssp)
  381. {
  382. u16 div;
  383. div = DIV_ROUND_UP(ssp->port.uartclk, ssp->baud_rate) - 1;
  384. __ssp_writel(div, SIFIVE_SERIAL_DIV_OFFS, ssp);
  385. }
  386. /**
  387. * __ssp_update_baud_rate() - set the UART "baud rate"
  388. * @ssp: pointer to a struct sifive_serial_port
  389. * @rate: new target bit rate
  390. *
  391. * Calculate the UART divisor value for the target bit rate @rate for the
  392. * SiFive UART described by @ssp and program it into the UART. There may
  393. * be some error between the target bit rate and the actual bit rate implemented
  394. * by the UART due to clock ratio granularity.
  395. */
  396. static void __ssp_update_baud_rate(struct sifive_serial_port *ssp,
  397. unsigned int rate)
  398. {
  399. if (ssp->baud_rate == rate)
  400. return;
  401. ssp->baud_rate = rate;
  402. __ssp_update_div(ssp);
  403. }
  404. /**
  405. * __ssp_set_stop_bits() - set the number of stop bits
  406. * @ssp: pointer to a struct sifive_serial_port
  407. * @nstop: 1 or 2 (stop bits)
  408. *
  409. * Program the SiFive UART referred to by @ssp to use @nstop stop bits.
  410. */
  411. static void __ssp_set_stop_bits(struct sifive_serial_port *ssp, char nstop)
  412. {
  413. u32 v;
  414. if (nstop < 1 || nstop > 2) {
  415. WARN_ON(1);
  416. return;
  417. }
  418. v = __ssp_readl(ssp, SIFIVE_SERIAL_TXCTRL_OFFS);
  419. v &= ~SIFIVE_SERIAL_TXCTRL_NSTOP_MASK;
  420. v |= (nstop - 1) << SIFIVE_SERIAL_TXCTRL_NSTOP_SHIFT;
  421. __ssp_writel(v, SIFIVE_SERIAL_TXCTRL_OFFS, ssp);
  422. }
  423. /**
  424. * __ssp_wait_for_xmitr() - wait for an empty slot on the TX FIFO
  425. * @ssp: pointer to a struct sifive_serial_port
  426. *
  427. * Delay while the UART TX FIFO referred to by @ssp is marked as full.
  428. *
  429. * Context: Any context.
  430. */
  431. static void __maybe_unused __ssp_wait_for_xmitr(struct sifive_serial_port *ssp)
  432. {
  433. while (sifive_serial_is_txfifo_full(ssp))
  434. udelay(1); /* XXX Could probably be more intelligent here */
  435. }
  436. /*
  437. * Linux serial API functions
  438. */
  439. static void sifive_serial_stop_tx(struct uart_port *port)
  440. {
  441. struct sifive_serial_port *ssp = port_to_sifive_serial_port(port);
  442. __ssp_disable_txwm(ssp);
  443. }
  444. static void sifive_serial_stop_rx(struct uart_port *port)
  445. {
  446. struct sifive_serial_port *ssp = port_to_sifive_serial_port(port);
  447. __ssp_disable_rxwm(ssp);
  448. }
  449. static void sifive_serial_start_tx(struct uart_port *port)
  450. {
  451. struct sifive_serial_port *ssp = port_to_sifive_serial_port(port);
  452. __ssp_enable_txwm(ssp);
  453. }
  454. static irqreturn_t sifive_serial_irq(int irq, void *dev_id)
  455. {
  456. struct sifive_serial_port *ssp = dev_id;
  457. u32 ip;
  458. uart_port_lock(&ssp->port);
  459. ip = __ssp_readl(ssp, SIFIVE_SERIAL_IP_OFFS);
  460. if (!ip) {
  461. uart_port_unlock(&ssp->port);
  462. return IRQ_NONE;
  463. }
  464. if (ip & SIFIVE_SERIAL_IP_RXWM_MASK)
  465. __ssp_receive_chars(ssp);
  466. if (ip & SIFIVE_SERIAL_IP_TXWM_MASK)
  467. __ssp_transmit_chars(ssp);
  468. uart_unlock_and_check_sysrq(&ssp->port);
  469. return IRQ_HANDLED;
  470. }
  471. static unsigned int sifive_serial_tx_empty(struct uart_port *port)
  472. {
  473. return TIOCSER_TEMT;
  474. }
  475. static unsigned int sifive_serial_get_mctrl(struct uart_port *port)
  476. {
  477. return TIOCM_CAR | TIOCM_CTS | TIOCM_DSR;
  478. }
  479. static void sifive_serial_set_mctrl(struct uart_port *port, unsigned int mctrl)
  480. {
  481. /* IP block does not support these signals */
  482. }
  483. static void sifive_serial_break_ctl(struct uart_port *port, int break_state)
  484. {
  485. /* IP block does not support sending a break */
  486. }
  487. static int sifive_serial_startup(struct uart_port *port)
  488. {
  489. struct sifive_serial_port *ssp = port_to_sifive_serial_port(port);
  490. unsigned long flags;
  491. uart_port_lock_irqsave(&ssp->port, &flags);
  492. __ssp_enable_rxwm(ssp);
  493. uart_port_unlock_irqrestore(&ssp->port, flags);
  494. return 0;
  495. }
  496. static void sifive_serial_shutdown(struct uart_port *port)
  497. {
  498. struct sifive_serial_port *ssp = port_to_sifive_serial_port(port);
  499. unsigned long flags;
  500. uart_port_lock_irqsave(&ssp->port, &flags);
  501. __ssp_disable_rxwm(ssp);
  502. __ssp_disable_txwm(ssp);
  503. uart_port_unlock_irqrestore(&ssp->port, flags);
  504. }
  505. /**
  506. * sifive_serial_clk_notifier() - clock post-rate-change notifier
  507. * @nb: pointer to the struct notifier_block, from the notifier code
  508. * @event: event mask from the notifier code
  509. * @data: pointer to the struct clk_notifier_data from the notifier code
  510. *
  511. * On the V0 SoC, the UART IP block is derived from the CPU clock source
  512. * after a synchronous divide-by-two divider, so any CPU clock rate change
  513. * requires the UART baud rate to be updated. This presumably corrupts any
  514. * serial word currently being transmitted or received. In order to avoid
  515. * corrupting the output data stream, we drain the transmit queue before
  516. * allowing the clock's rate to be changed.
  517. */
  518. static int sifive_serial_clk_notifier(struct notifier_block *nb,
  519. unsigned long event, void *data)
  520. {
  521. struct clk_notifier_data *cnd = data;
  522. struct sifive_serial_port *ssp = notifier_to_sifive_serial_port(nb);
  523. if (event == PRE_RATE_CHANGE) {
  524. /*
  525. * The TX watermark is always set to 1 by this driver, which
  526. * means that the TX busy bit will lower when there are 0 bytes
  527. * left in the TX queue -- in other words, when the TX FIFO is
  528. * empty.
  529. */
  530. __ssp_wait_for_xmitr(ssp);
  531. /*
  532. * On the cycle the TX FIFO goes empty there is still a full
  533. * UART frame left to be transmitted in the shift register.
  534. * The UART provides no way for software to directly determine
  535. * when that last frame has been transmitted, so we just sleep
  536. * here instead. As we're not tracking the number of stop bits
  537. * they're just worst cased here. The rest of the serial
  538. * framing parameters aren't configurable by software.
  539. */
  540. udelay(DIV_ROUND_UP(12 * 1000 * 1000, ssp->baud_rate));
  541. }
  542. if (event == POST_RATE_CHANGE && ssp->port.uartclk != cnd->new_rate) {
  543. ssp->port.uartclk = cnd->new_rate;
  544. __ssp_update_div(ssp);
  545. }
  546. return NOTIFY_OK;
  547. }
  548. static void sifive_serial_set_termios(struct uart_port *port,
  549. struct ktermios *termios,
  550. const struct ktermios *old)
  551. {
  552. struct sifive_serial_port *ssp = port_to_sifive_serial_port(port);
  553. unsigned long flags;
  554. u32 v, old_v;
  555. int rate;
  556. char nstop;
  557. if ((termios->c_cflag & CSIZE) != CS8) {
  558. dev_err_once(ssp->port.dev, "only 8-bit words supported\n");
  559. termios->c_cflag &= ~CSIZE;
  560. termios->c_cflag |= CS8;
  561. }
  562. if (termios->c_iflag & (INPCK | PARMRK))
  563. dev_err_once(ssp->port.dev, "parity checking not supported\n");
  564. if (termios->c_iflag & BRKINT)
  565. dev_err_once(ssp->port.dev, "BREAK detection not supported\n");
  566. termios->c_iflag &= ~(INPCK|PARMRK|BRKINT);
  567. /* Set number of stop bits */
  568. nstop = (termios->c_cflag & CSTOPB) ? 2 : 1;
  569. __ssp_set_stop_bits(ssp, nstop);
  570. /* Set line rate */
  571. rate = uart_get_baud_rate(port, termios, old, 0,
  572. ssp->port.uartclk / 16);
  573. __ssp_update_baud_rate(ssp, rate);
  574. uart_port_lock_irqsave(&ssp->port, &flags);
  575. /* Update the per-port timeout */
  576. uart_update_timeout(port, termios->c_cflag, rate);
  577. ssp->port.read_status_mask = 0;
  578. /* Ignore all characters if CREAD is not set */
  579. v = __ssp_readl(ssp, SIFIVE_SERIAL_RXCTRL_OFFS);
  580. old_v = v;
  581. if ((termios->c_cflag & CREAD) == 0)
  582. v &= SIFIVE_SERIAL_RXCTRL_RXEN_MASK;
  583. else
  584. v |= SIFIVE_SERIAL_RXCTRL_RXEN_MASK;
  585. if (v != old_v)
  586. __ssp_writel(v, SIFIVE_SERIAL_RXCTRL_OFFS, ssp);
  587. uart_port_unlock_irqrestore(&ssp->port, flags);
  588. }
  589. static void sifive_serial_release_port(struct uart_port *port)
  590. {
  591. }
  592. static int sifive_serial_request_port(struct uart_port *port)
  593. {
  594. return 0;
  595. }
  596. static void sifive_serial_config_port(struct uart_port *port, int flags)
  597. {
  598. struct sifive_serial_port *ssp = port_to_sifive_serial_port(port);
  599. ssp->port.type = PORT_SIFIVE_V0;
  600. }
  601. static int sifive_serial_verify_port(struct uart_port *port,
  602. struct serial_struct *ser)
  603. {
  604. return -EINVAL;
  605. }
  606. static const char *sifive_serial_type(struct uart_port *port)
  607. {
  608. return port->type == PORT_SIFIVE_V0 ? "SiFive UART v0" : NULL;
  609. }
  610. #ifdef CONFIG_CONSOLE_POLL
  611. static int sifive_serial_poll_get_char(struct uart_port *port)
  612. {
  613. struct sifive_serial_port *ssp = port_to_sifive_serial_port(port);
  614. char is_empty, ch;
  615. ch = __ssp_receive_char(ssp, &is_empty);
  616. if (is_empty)
  617. return NO_POLL_CHAR;
  618. return ch;
  619. }
  620. static void sifive_serial_poll_put_char(struct uart_port *port,
  621. unsigned char c)
  622. {
  623. struct sifive_serial_port *ssp = port_to_sifive_serial_port(port);
  624. __ssp_wait_for_xmitr(ssp);
  625. __ssp_transmit_char(ssp, c);
  626. }
  627. #endif /* CONFIG_CONSOLE_POLL */
  628. /*
  629. * Early console support
  630. */
  631. #ifdef CONFIG_SERIAL_EARLYCON
  632. static void early_sifive_serial_putc(struct uart_port *port, unsigned char c)
  633. {
  634. while (__ssp_early_readl(port, SIFIVE_SERIAL_TXDATA_OFFS) &
  635. SIFIVE_SERIAL_TXDATA_FULL_MASK)
  636. cpu_relax();
  637. __ssp_early_writel(c, SIFIVE_SERIAL_TXDATA_OFFS, port);
  638. }
  639. static void early_sifive_serial_write(struct console *con, const char *s,
  640. unsigned int n)
  641. {
  642. struct earlycon_device *dev = con->data;
  643. struct uart_port *port = &dev->port;
  644. uart_console_write(port, s, n, early_sifive_serial_putc);
  645. }
  646. static int __init early_sifive_serial_setup(struct earlycon_device *dev,
  647. const char *options)
  648. {
  649. struct uart_port *port = &dev->port;
  650. if (!port->membase)
  651. return -ENODEV;
  652. dev->con->write = early_sifive_serial_write;
  653. return 0;
  654. }
  655. OF_EARLYCON_DECLARE(sifive, "sifive,uart0", early_sifive_serial_setup);
  656. OF_EARLYCON_DECLARE(sifive, "sifive,fu540-c000-uart",
  657. early_sifive_serial_setup);
  658. #endif /* CONFIG_SERIAL_EARLYCON */
  659. /*
  660. * Linux console interface
  661. */
  662. #ifdef CONFIG_SERIAL_SIFIVE_CONSOLE
  663. static struct sifive_serial_port *sifive_serial_console_ports[SIFIVE_SERIAL_MAX_PORTS];
  664. static void sifive_serial_console_putchar(struct uart_port *port, unsigned char ch)
  665. {
  666. struct sifive_serial_port *ssp = port_to_sifive_serial_port(port);
  667. __ssp_wait_for_xmitr(ssp);
  668. __ssp_transmit_char(ssp, ch);
  669. }
  670. static void sifive_serial_console_write(struct console *co, const char *s,
  671. unsigned int count)
  672. {
  673. struct sifive_serial_port *ssp = sifive_serial_console_ports[co->index];
  674. unsigned long flags;
  675. unsigned int ier;
  676. int locked = 1;
  677. if (!ssp)
  678. return;
  679. if (oops_in_progress)
  680. locked = uart_port_trylock_irqsave(&ssp->port, &flags);
  681. else
  682. uart_port_lock_irqsave(&ssp->port, &flags);
  683. ier = __ssp_readl(ssp, SIFIVE_SERIAL_IE_OFFS);
  684. __ssp_writel(0, SIFIVE_SERIAL_IE_OFFS, ssp);
  685. uart_console_write(&ssp->port, s, count, sifive_serial_console_putchar);
  686. __ssp_writel(ier, SIFIVE_SERIAL_IE_OFFS, ssp);
  687. if (locked)
  688. uart_port_unlock_irqrestore(&ssp->port, flags);
  689. }
  690. static int sifive_serial_console_setup(struct console *co, char *options)
  691. {
  692. struct sifive_serial_port *ssp;
  693. int baud = SIFIVE_DEFAULT_BAUD_RATE;
  694. int bits = 8;
  695. int parity = 'n';
  696. int flow = 'n';
  697. if (co->index < 0 || co->index >= SIFIVE_SERIAL_MAX_PORTS)
  698. return -ENODEV;
  699. ssp = sifive_serial_console_ports[co->index];
  700. if (!ssp)
  701. return -ENODEV;
  702. if (options)
  703. uart_parse_options(options, &baud, &parity, &bits, &flow);
  704. return uart_set_options(&ssp->port, co, baud, parity, bits, flow);
  705. }
  706. static struct uart_driver sifive_serial_uart_driver;
  707. static struct console sifive_serial_console = {
  708. .name = SIFIVE_TTY_PREFIX,
  709. .write = sifive_serial_console_write,
  710. .device = uart_console_device,
  711. .setup = sifive_serial_console_setup,
  712. .flags = CON_PRINTBUFFER,
  713. .index = -1,
  714. .data = &sifive_serial_uart_driver,
  715. };
  716. static int __init sifive_console_init(void)
  717. {
  718. register_console(&sifive_serial_console);
  719. return 0;
  720. }
  721. console_initcall(sifive_console_init);
  722. static void __ssp_add_console_port(struct sifive_serial_port *ssp)
  723. {
  724. sifive_serial_console_ports[ssp->port.line] = ssp;
  725. }
  726. static void __ssp_remove_console_port(struct sifive_serial_port *ssp)
  727. {
  728. sifive_serial_console_ports[ssp->port.line] = NULL;
  729. }
  730. #define SIFIVE_SERIAL_CONSOLE (&sifive_serial_console)
  731. #else
  732. #define SIFIVE_SERIAL_CONSOLE NULL
  733. static void __ssp_add_console_port(struct sifive_serial_port *ssp)
  734. {}
  735. static void __ssp_remove_console_port(struct sifive_serial_port *ssp)
  736. {}
  737. #endif
  738. static const struct uart_ops sifive_serial_uops = {
  739. .tx_empty = sifive_serial_tx_empty,
  740. .set_mctrl = sifive_serial_set_mctrl,
  741. .get_mctrl = sifive_serial_get_mctrl,
  742. .stop_tx = sifive_serial_stop_tx,
  743. .start_tx = sifive_serial_start_tx,
  744. .stop_rx = sifive_serial_stop_rx,
  745. .break_ctl = sifive_serial_break_ctl,
  746. .startup = sifive_serial_startup,
  747. .shutdown = sifive_serial_shutdown,
  748. .set_termios = sifive_serial_set_termios,
  749. .type = sifive_serial_type,
  750. .release_port = sifive_serial_release_port,
  751. .request_port = sifive_serial_request_port,
  752. .config_port = sifive_serial_config_port,
  753. .verify_port = sifive_serial_verify_port,
  754. #ifdef CONFIG_CONSOLE_POLL
  755. .poll_get_char = sifive_serial_poll_get_char,
  756. .poll_put_char = sifive_serial_poll_put_char,
  757. #endif
  758. };
  759. static struct uart_driver sifive_serial_uart_driver = {
  760. .owner = THIS_MODULE,
  761. .driver_name = SIFIVE_SERIAL_NAME,
  762. .dev_name = SIFIVE_TTY_PREFIX,
  763. .nr = SIFIVE_SERIAL_MAX_PORTS,
  764. .cons = SIFIVE_SERIAL_CONSOLE,
  765. };
  766. static int sifive_serial_probe(struct platform_device *pdev)
  767. {
  768. struct sifive_serial_port *ssp;
  769. struct resource *mem;
  770. struct clk *clk;
  771. void __iomem *base;
  772. int irq, id, r;
  773. irq = platform_get_irq(pdev, 0);
  774. if (irq < 0)
  775. return -EPROBE_DEFER;
  776. base = devm_platform_get_and_ioremap_resource(pdev, 0, &mem);
  777. if (IS_ERR(base))
  778. return PTR_ERR(base);
  779. clk = devm_clk_get_enabled(&pdev->dev, NULL);
  780. if (IS_ERR(clk)) {
  781. dev_err(&pdev->dev, "unable to find controller clock\n");
  782. return PTR_ERR(clk);
  783. }
  784. id = of_alias_get_id(pdev->dev.of_node, "serial");
  785. if (id < 0) {
  786. dev_err(&pdev->dev, "missing aliases entry\n");
  787. return id;
  788. }
  789. #ifdef CONFIG_SERIAL_SIFIVE_CONSOLE
  790. if (id > SIFIVE_SERIAL_MAX_PORTS) {
  791. dev_err(&pdev->dev, "too many UARTs (%d)\n", id);
  792. return -EINVAL;
  793. }
  794. #endif
  795. ssp = devm_kzalloc(&pdev->dev, sizeof(*ssp), GFP_KERNEL);
  796. if (!ssp)
  797. return -ENOMEM;
  798. ssp->port.dev = &pdev->dev;
  799. ssp->port.type = PORT_SIFIVE_V0;
  800. ssp->port.iotype = UPIO_MEM;
  801. ssp->port.irq = irq;
  802. ssp->port.fifosize = SIFIVE_TX_FIFO_DEPTH;
  803. ssp->port.ops = &sifive_serial_uops;
  804. ssp->port.line = id;
  805. ssp->port.mapbase = mem->start;
  806. ssp->port.membase = base;
  807. ssp->dev = &pdev->dev;
  808. ssp->clk = clk;
  809. ssp->clk_notifier.notifier_call = sifive_serial_clk_notifier;
  810. r = clk_notifier_register(ssp->clk, &ssp->clk_notifier);
  811. if (r) {
  812. dev_err(&pdev->dev, "could not register clock notifier: %d\n",
  813. r);
  814. goto probe_out1;
  815. }
  816. /* Set up clock divider */
  817. ssp->port.uartclk = clk_get_rate(ssp->clk);
  818. ssp->baud_rate = SIFIVE_DEFAULT_BAUD_RATE;
  819. __ssp_update_div(ssp);
  820. platform_set_drvdata(pdev, ssp);
  821. /* Enable transmits and set the watermark level to 1 */
  822. __ssp_writel((1 << SIFIVE_SERIAL_TXCTRL_TXCNT_SHIFT) |
  823. SIFIVE_SERIAL_TXCTRL_TXEN_MASK,
  824. SIFIVE_SERIAL_TXCTRL_OFFS, ssp);
  825. /* Enable receives and set the watermark level to 0 */
  826. __ssp_writel((0 << SIFIVE_SERIAL_RXCTRL_RXCNT_SHIFT) |
  827. SIFIVE_SERIAL_RXCTRL_RXEN_MASK,
  828. SIFIVE_SERIAL_RXCTRL_OFFS, ssp);
  829. r = request_irq(ssp->port.irq, sifive_serial_irq, ssp->port.irqflags,
  830. dev_name(&pdev->dev), ssp);
  831. if (r) {
  832. dev_err(&pdev->dev, "could not attach interrupt: %d\n", r);
  833. goto probe_out2;
  834. }
  835. __ssp_add_console_port(ssp);
  836. r = uart_add_one_port(&sifive_serial_uart_driver, &ssp->port);
  837. if (r != 0) {
  838. dev_err(&pdev->dev, "could not add uart: %d\n", r);
  839. goto probe_out3;
  840. }
  841. return 0;
  842. probe_out3:
  843. __ssp_remove_console_port(ssp);
  844. free_irq(ssp->port.irq, ssp);
  845. probe_out2:
  846. clk_notifier_unregister(ssp->clk, &ssp->clk_notifier);
  847. probe_out1:
  848. return r;
  849. }
  850. static void sifive_serial_remove(struct platform_device *dev)
  851. {
  852. struct sifive_serial_port *ssp = platform_get_drvdata(dev);
  853. __ssp_remove_console_port(ssp);
  854. uart_remove_one_port(&sifive_serial_uart_driver, &ssp->port);
  855. free_irq(ssp->port.irq, ssp);
  856. clk_notifier_unregister(ssp->clk, &ssp->clk_notifier);
  857. }
  858. static int sifive_serial_suspend(struct device *dev)
  859. {
  860. struct sifive_serial_port *ssp = dev_get_drvdata(dev);
  861. return uart_suspend_port(&sifive_serial_uart_driver, &ssp->port);
  862. }
  863. static int sifive_serial_resume(struct device *dev)
  864. {
  865. struct sifive_serial_port *ssp = dev_get_drvdata(dev);
  866. return uart_resume_port(&sifive_serial_uart_driver, &ssp->port);
  867. }
  868. static DEFINE_SIMPLE_DEV_PM_OPS(sifive_uart_pm_ops, sifive_serial_suspend,
  869. sifive_serial_resume);
  870. static const struct of_device_id sifive_serial_of_match[] = {
  871. { .compatible = "sifive,fu540-c000-uart" },
  872. { .compatible = "sifive,uart0" },
  873. {},
  874. };
  875. MODULE_DEVICE_TABLE(of, sifive_serial_of_match);
  876. static struct platform_driver sifive_serial_platform_driver = {
  877. .probe = sifive_serial_probe,
  878. .remove_new = sifive_serial_remove,
  879. .driver = {
  880. .name = SIFIVE_SERIAL_NAME,
  881. .pm = pm_sleep_ptr(&sifive_uart_pm_ops),
  882. .of_match_table = sifive_serial_of_match,
  883. },
  884. };
  885. static int __init sifive_serial_init(void)
  886. {
  887. int r;
  888. r = uart_register_driver(&sifive_serial_uart_driver);
  889. if (r)
  890. goto init_out1;
  891. r = platform_driver_register(&sifive_serial_platform_driver);
  892. if (r)
  893. goto init_out2;
  894. return 0;
  895. init_out2:
  896. uart_unregister_driver(&sifive_serial_uart_driver);
  897. init_out1:
  898. return r;
  899. }
  900. static void __exit sifive_serial_exit(void)
  901. {
  902. platform_driver_unregister(&sifive_serial_platform_driver);
  903. uart_unregister_driver(&sifive_serial_uart_driver);
  904. }
  905. module_init(sifive_serial_init);
  906. module_exit(sifive_serial_exit);
  907. MODULE_DESCRIPTION("SiFive UART serial driver");
  908. MODULE_LICENSE("GPL");
  909. MODULE_AUTHOR("Paul Walmsley <paul@pwsan.com>");