pic32_uart.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * PIC32 Integrated Serial Driver.
  4. *
  5. * Copyright (C) 2015 Microchip Technology, Inc.
  6. *
  7. * Authors:
  8. * Sorin-Andrei Pistirica <[email protected]>
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/of.h>
  13. #include <linux/of_device.h>
  14. #include <linux/of_irq.h>
  15. #include <linux/of_gpio.h>
  16. #include <linux/init.h>
  17. #include <linux/module.h>
  18. #include <linux/slab.h>
  19. #include <linux/console.h>
  20. #include <linux/clk.h>
  21. #include <linux/tty.h>
  22. #include <linux/tty_flip.h>
  23. #include <linux/serial_core.h>
  24. #include <linux/delay.h>
  25. #include <asm/mach-pic32/pic32.h>
  26. /* UART name and device definitions */
  27. #define PIC32_DEV_NAME "pic32-uart"
  28. #define PIC32_MAX_UARTS 6
  29. #define PIC32_SDEV_NAME "ttyPIC"
  30. #define PIC32_UART_DFLT_BRATE 9600
  31. #define PIC32_UART_TX_FIFO_DEPTH 8
  32. #define PIC32_UART_RX_FIFO_DEPTH 8
  33. #define PIC32_UART_MODE 0x00
  34. #define PIC32_UART_STA 0x10
  35. #define PIC32_UART_TX 0x20
  36. #define PIC32_UART_RX 0x30
  37. #define PIC32_UART_BRG 0x40
  38. /* struct pic32_sport - pic32 serial port descriptor
  39. * @port: uart port descriptor
  40. * @idx: port index
  41. * @irq_fault: virtual fault interrupt number
  42. * @irq_fault_name: irq fault name
  43. * @irq_rx: virtual rx interrupt number
  44. * @irq_rx_name: irq rx name
  45. * @irq_tx: virtual tx interrupt number
  46. * @irq_tx_name: irq tx name
  47. * @cts_gpiod: clear to send GPIO
  48. * @dev: device descriptor
  49. **/
  50. struct pic32_sport {
  51. struct uart_port port;
  52. int idx;
  53. int irq_fault;
  54. const char *irq_fault_name;
  55. int irq_rx;
  56. const char *irq_rx_name;
  57. int irq_tx;
  58. const char *irq_tx_name;
  59. bool enable_tx_irq;
  60. struct gpio_desc *cts_gpiod;
  61. struct clk *clk;
  62. struct device *dev;
  63. };
  64. static inline struct pic32_sport *to_pic32_sport(struct uart_port *port)
  65. {
  66. return container_of(port, struct pic32_sport, port);
  67. }
  68. static inline void pic32_uart_writel(struct pic32_sport *sport,
  69. u32 reg, u32 val)
  70. {
  71. __raw_writel(val, sport->port.membase + reg);
  72. }
  73. static inline u32 pic32_uart_readl(struct pic32_sport *sport, u32 reg)
  74. {
  75. return __raw_readl(sport->port.membase + reg);
  76. }
  77. /* pic32 uart mode register bits */
  78. #define PIC32_UART_MODE_ON BIT(15)
  79. #define PIC32_UART_MODE_FRZ BIT(14)
  80. #define PIC32_UART_MODE_SIDL BIT(13)
  81. #define PIC32_UART_MODE_IREN BIT(12)
  82. #define PIC32_UART_MODE_RTSMD BIT(11)
  83. #define PIC32_UART_MODE_RESV1 BIT(10)
  84. #define PIC32_UART_MODE_UEN1 BIT(9)
  85. #define PIC32_UART_MODE_UEN0 BIT(8)
  86. #define PIC32_UART_MODE_WAKE BIT(7)
  87. #define PIC32_UART_MODE_LPBK BIT(6)
  88. #define PIC32_UART_MODE_ABAUD BIT(5)
  89. #define PIC32_UART_MODE_RXINV BIT(4)
  90. #define PIC32_UART_MODE_BRGH BIT(3)
  91. #define PIC32_UART_MODE_PDSEL1 BIT(2)
  92. #define PIC32_UART_MODE_PDSEL0 BIT(1)
  93. #define PIC32_UART_MODE_STSEL BIT(0)
  94. /* pic32 uart status register bits */
  95. #define PIC32_UART_STA_UTXISEL1 BIT(15)
  96. #define PIC32_UART_STA_UTXISEL0 BIT(14)
  97. #define PIC32_UART_STA_UTXINV BIT(13)
  98. #define PIC32_UART_STA_URXEN BIT(12)
  99. #define PIC32_UART_STA_UTXBRK BIT(11)
  100. #define PIC32_UART_STA_UTXEN BIT(10)
  101. #define PIC32_UART_STA_UTXBF BIT(9)
  102. #define PIC32_UART_STA_TRMT BIT(8)
  103. #define PIC32_UART_STA_URXISEL1 BIT(7)
  104. #define PIC32_UART_STA_URXISEL0 BIT(6)
  105. #define PIC32_UART_STA_ADDEN BIT(5)
  106. #define PIC32_UART_STA_RIDLE BIT(4)
  107. #define PIC32_UART_STA_PERR BIT(3)
  108. #define PIC32_UART_STA_FERR BIT(2)
  109. #define PIC32_UART_STA_OERR BIT(1)
  110. #define PIC32_UART_STA_URXDA BIT(0)
  111. /* pic32_sport pointer for console use */
  112. static struct pic32_sport *pic32_sports[PIC32_MAX_UARTS];
  113. static inline void pic32_wait_deplete_txbuf(struct pic32_sport *sport)
  114. {
  115. /* wait for tx empty, otherwise chars will be lost or corrupted */
  116. while (!(pic32_uart_readl(sport, PIC32_UART_STA) & PIC32_UART_STA_TRMT))
  117. udelay(1);
  118. }
  119. /* serial core request to check if uart tx buffer is empty */
  120. static unsigned int pic32_uart_tx_empty(struct uart_port *port)
  121. {
  122. struct pic32_sport *sport = to_pic32_sport(port);
  123. u32 val = pic32_uart_readl(sport, PIC32_UART_STA);
  124. return (val & PIC32_UART_STA_TRMT) ? 1 : 0;
  125. }
  126. /* serial core request to set UART outputs */
  127. static void pic32_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
  128. {
  129. struct pic32_sport *sport = to_pic32_sport(port);
  130. /* set loopback mode */
  131. if (mctrl & TIOCM_LOOP)
  132. pic32_uart_writel(sport, PIC32_SET(PIC32_UART_MODE),
  133. PIC32_UART_MODE_LPBK);
  134. else
  135. pic32_uart_writel(sport, PIC32_CLR(PIC32_UART_MODE),
  136. PIC32_UART_MODE_LPBK);
  137. }
  138. /* serial core request to return the state of misc UART input pins */
  139. static unsigned int pic32_uart_get_mctrl(struct uart_port *port)
  140. {
  141. struct pic32_sport *sport = to_pic32_sport(port);
  142. unsigned int mctrl = 0;
  143. /* get the state of CTS input pin for this port */
  144. if (!sport->cts_gpiod)
  145. mctrl |= TIOCM_CTS;
  146. else if (gpiod_get_value(sport->cts_gpiod))
  147. mctrl |= TIOCM_CTS;
  148. /* DSR and CD are not supported in PIC32, so return 1
  149. * RI is not supported in PIC32, so return 0
  150. */
  151. mctrl |= TIOCM_CD;
  152. mctrl |= TIOCM_DSR;
  153. return mctrl;
  154. }
  155. /* stop tx and start tx are not called in pairs, therefore a flag indicates
  156. * the status of irq to control the irq-depth.
  157. */
  158. static inline void pic32_uart_irqtxen(struct pic32_sport *sport, u8 en)
  159. {
  160. if (en && !sport->enable_tx_irq) {
  161. enable_irq(sport->irq_tx);
  162. sport->enable_tx_irq = true;
  163. } else if (!en && sport->enable_tx_irq) {
  164. /* use disable_irq_nosync() and not disable_irq() to avoid self
  165. * imposed deadlock by not waiting for irq handler to end,
  166. * since this callback is called from interrupt context.
  167. */
  168. disable_irq_nosync(sport->irq_tx);
  169. sport->enable_tx_irq = false;
  170. }
  171. }
  172. /* serial core request to disable tx ASAP (used for flow control) */
  173. static void pic32_uart_stop_tx(struct uart_port *port)
  174. {
  175. struct pic32_sport *sport = to_pic32_sport(port);
  176. if (!(pic32_uart_readl(sport, PIC32_UART_MODE) & PIC32_UART_MODE_ON))
  177. return;
  178. if (!(pic32_uart_readl(sport, PIC32_UART_STA) & PIC32_UART_STA_UTXEN))
  179. return;
  180. /* wait for tx empty */
  181. pic32_wait_deplete_txbuf(sport);
  182. pic32_uart_writel(sport, PIC32_CLR(PIC32_UART_STA),
  183. PIC32_UART_STA_UTXEN);
  184. pic32_uart_irqtxen(sport, 0);
  185. }
  186. /* serial core request to (re)enable tx */
  187. static void pic32_uart_start_tx(struct uart_port *port)
  188. {
  189. struct pic32_sport *sport = to_pic32_sport(port);
  190. pic32_uart_irqtxen(sport, 1);
  191. pic32_uart_writel(sport, PIC32_SET(PIC32_UART_STA),
  192. PIC32_UART_STA_UTXEN);
  193. }
  194. /* serial core request to stop rx, called before port shutdown */
  195. static void pic32_uart_stop_rx(struct uart_port *port)
  196. {
  197. struct pic32_sport *sport = to_pic32_sport(port);
  198. /* disable rx interrupts */
  199. disable_irq(sport->irq_rx);
  200. /* receiver Enable bit OFF */
  201. pic32_uart_writel(sport, PIC32_CLR(PIC32_UART_STA),
  202. PIC32_UART_STA_URXEN);
  203. }
  204. /* serial core request to start/stop emitting break char */
  205. static void pic32_uart_break_ctl(struct uart_port *port, int ctl)
  206. {
  207. struct pic32_sport *sport = to_pic32_sport(port);
  208. unsigned long flags;
  209. spin_lock_irqsave(&port->lock, flags);
  210. if (ctl)
  211. pic32_uart_writel(sport, PIC32_SET(PIC32_UART_STA),
  212. PIC32_UART_STA_UTXBRK);
  213. else
  214. pic32_uart_writel(sport, PIC32_CLR(PIC32_UART_STA),
  215. PIC32_UART_STA_UTXBRK);
  216. spin_unlock_irqrestore(&port->lock, flags);
  217. }
  218. /* get port type in string format */
  219. static const char *pic32_uart_type(struct uart_port *port)
  220. {
  221. return (port->type == PORT_PIC32) ? PIC32_DEV_NAME : NULL;
  222. }
  223. /* read all chars in rx fifo and send them to core */
  224. static void pic32_uart_do_rx(struct uart_port *port)
  225. {
  226. struct pic32_sport *sport = to_pic32_sport(port);
  227. struct tty_port *tty;
  228. unsigned int max_count;
  229. /* limit number of char read in interrupt, should not be
  230. * higher than fifo size anyway since we're much faster than
  231. * serial port
  232. */
  233. max_count = PIC32_UART_RX_FIFO_DEPTH;
  234. spin_lock(&port->lock);
  235. tty = &port->state->port;
  236. do {
  237. u32 sta_reg, c;
  238. char flag;
  239. /* get overrun/fifo empty information from status register */
  240. sta_reg = pic32_uart_readl(sport, PIC32_UART_STA);
  241. if (unlikely(sta_reg & PIC32_UART_STA_OERR)) {
  242. /* fifo reset is required to clear interrupt */
  243. pic32_uart_writel(sport, PIC32_CLR(PIC32_UART_STA),
  244. PIC32_UART_STA_OERR);
  245. port->icount.overrun++;
  246. tty_insert_flip_char(tty, 0, TTY_OVERRUN);
  247. }
  248. /* Can at least one more character can be read? */
  249. if (!(sta_reg & PIC32_UART_STA_URXDA))
  250. break;
  251. /* read the character and increment the rx counter */
  252. c = pic32_uart_readl(sport, PIC32_UART_RX);
  253. port->icount.rx++;
  254. flag = TTY_NORMAL;
  255. c &= 0xff;
  256. if (unlikely((sta_reg & PIC32_UART_STA_PERR) ||
  257. (sta_reg & PIC32_UART_STA_FERR))) {
  258. /* do stats first */
  259. if (sta_reg & PIC32_UART_STA_PERR)
  260. port->icount.parity++;
  261. if (sta_reg & PIC32_UART_STA_FERR)
  262. port->icount.frame++;
  263. /* update flag wrt read_status_mask */
  264. sta_reg &= port->read_status_mask;
  265. if (sta_reg & PIC32_UART_STA_FERR)
  266. flag = TTY_FRAME;
  267. if (sta_reg & PIC32_UART_STA_PERR)
  268. flag = TTY_PARITY;
  269. }
  270. if (uart_handle_sysrq_char(port, c))
  271. continue;
  272. if ((sta_reg & port->ignore_status_mask) == 0)
  273. tty_insert_flip_char(tty, c, flag);
  274. } while (--max_count);
  275. spin_unlock(&port->lock);
  276. tty_flip_buffer_push(tty);
  277. }
  278. /* fill tx fifo with chars to send, stop when fifo is about to be full
  279. * or when all chars have been sent.
  280. */
  281. static void pic32_uart_do_tx(struct uart_port *port)
  282. {
  283. struct pic32_sport *sport = to_pic32_sport(port);
  284. struct circ_buf *xmit = &port->state->xmit;
  285. unsigned int max_count = PIC32_UART_TX_FIFO_DEPTH;
  286. if (port->x_char) {
  287. pic32_uart_writel(sport, PIC32_UART_TX, port->x_char);
  288. port->icount.tx++;
  289. port->x_char = 0;
  290. return;
  291. }
  292. if (uart_tx_stopped(port)) {
  293. pic32_uart_stop_tx(port);
  294. return;
  295. }
  296. if (uart_circ_empty(xmit))
  297. goto txq_empty;
  298. /* keep stuffing chars into uart tx buffer
  299. * 1) until uart fifo is full
  300. * or
  301. * 2) until the circ buffer is empty
  302. * (all chars have been sent)
  303. * or
  304. * 3) until the max count is reached
  305. * (prevents lingering here for too long in certain cases)
  306. */
  307. while (!(PIC32_UART_STA_UTXBF &
  308. pic32_uart_readl(sport, PIC32_UART_STA))) {
  309. unsigned int c = xmit->buf[xmit->tail];
  310. pic32_uart_writel(sport, PIC32_UART_TX, c);
  311. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  312. port->icount.tx++;
  313. if (uart_circ_empty(xmit))
  314. break;
  315. if (--max_count == 0)
  316. break;
  317. }
  318. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  319. uart_write_wakeup(port);
  320. if (uart_circ_empty(xmit))
  321. goto txq_empty;
  322. return;
  323. txq_empty:
  324. pic32_uart_irqtxen(sport, 0);
  325. }
  326. /* RX interrupt handler */
  327. static irqreturn_t pic32_uart_rx_interrupt(int irq, void *dev_id)
  328. {
  329. struct uart_port *port = dev_id;
  330. pic32_uart_do_rx(port);
  331. return IRQ_HANDLED;
  332. }
  333. /* TX interrupt handler */
  334. static irqreturn_t pic32_uart_tx_interrupt(int irq, void *dev_id)
  335. {
  336. struct uart_port *port = dev_id;
  337. unsigned long flags;
  338. spin_lock_irqsave(&port->lock, flags);
  339. pic32_uart_do_tx(port);
  340. spin_unlock_irqrestore(&port->lock, flags);
  341. return IRQ_HANDLED;
  342. }
  343. /* FAULT interrupt handler */
  344. static irqreturn_t pic32_uart_fault_interrupt(int irq, void *dev_id)
  345. {
  346. /* do nothing: pic32_uart_do_rx() handles faults. */
  347. return IRQ_HANDLED;
  348. }
  349. /* enable rx & tx operation on uart */
  350. static void pic32_uart_en_and_unmask(struct uart_port *port)
  351. {
  352. struct pic32_sport *sport = to_pic32_sport(port);
  353. pic32_uart_writel(sport, PIC32_SET(PIC32_UART_STA),
  354. PIC32_UART_STA_UTXEN | PIC32_UART_STA_URXEN);
  355. pic32_uart_writel(sport, PIC32_SET(PIC32_UART_MODE),
  356. PIC32_UART_MODE_ON);
  357. }
  358. /* disable rx & tx operation on uart */
  359. static void pic32_uart_dsbl_and_mask(struct uart_port *port)
  360. {
  361. struct pic32_sport *sport = to_pic32_sport(port);
  362. /* wait for tx empty, otherwise chars will be lost or corrupted */
  363. pic32_wait_deplete_txbuf(sport);
  364. pic32_uart_writel(sport, PIC32_CLR(PIC32_UART_STA),
  365. PIC32_UART_STA_UTXEN | PIC32_UART_STA_URXEN);
  366. pic32_uart_writel(sport, PIC32_CLR(PIC32_UART_MODE),
  367. PIC32_UART_MODE_ON);
  368. }
  369. /* serial core request to initialize uart and start rx operation */
  370. static int pic32_uart_startup(struct uart_port *port)
  371. {
  372. struct pic32_sport *sport = to_pic32_sport(port);
  373. u32 dflt_baud = (port->uartclk / PIC32_UART_DFLT_BRATE / 16) - 1;
  374. unsigned long flags;
  375. int ret;
  376. local_irq_save(flags);
  377. ret = clk_prepare_enable(sport->clk);
  378. if (ret) {
  379. local_irq_restore(flags);
  380. goto out_done;
  381. }
  382. /* clear status and mode registers */
  383. pic32_uart_writel(sport, PIC32_UART_MODE, 0);
  384. pic32_uart_writel(sport, PIC32_UART_STA, 0);
  385. /* disable uart and mask all interrupts */
  386. pic32_uart_dsbl_and_mask(port);
  387. /* set default baud */
  388. pic32_uart_writel(sport, PIC32_UART_BRG, dflt_baud);
  389. local_irq_restore(flags);
  390. /* Each UART of a PIC32 has three interrupts therefore,
  391. * we setup driver to register the 3 irqs for the device.
  392. *
  393. * For each irq request_irq() is called with interrupt disabled.
  394. * And the irq is enabled as soon as we are ready to handle them.
  395. */
  396. sport->enable_tx_irq = false;
  397. sport->irq_fault_name = kasprintf(GFP_KERNEL, "%s%d-fault",
  398. pic32_uart_type(port),
  399. sport->idx);
  400. if (!sport->irq_fault_name) {
  401. dev_err(port->dev, "%s: kasprintf err!", __func__);
  402. ret = -ENOMEM;
  403. goto out_disable_clk;
  404. }
  405. irq_set_status_flags(sport->irq_fault, IRQ_NOAUTOEN);
  406. ret = request_irq(sport->irq_fault, pic32_uart_fault_interrupt,
  407. IRQF_NO_THREAD, sport->irq_fault_name, port);
  408. if (ret) {
  409. dev_err(port->dev, "%s: request irq(%d) err! ret:%d name:%s\n",
  410. __func__, sport->irq_fault, ret,
  411. pic32_uart_type(port));
  412. goto out_f;
  413. }
  414. sport->irq_rx_name = kasprintf(GFP_KERNEL, "%s%d-rx",
  415. pic32_uart_type(port),
  416. sport->idx);
  417. if (!sport->irq_rx_name) {
  418. dev_err(port->dev, "%s: kasprintf err!", __func__);
  419. ret = -ENOMEM;
  420. goto out_f;
  421. }
  422. irq_set_status_flags(sport->irq_rx, IRQ_NOAUTOEN);
  423. ret = request_irq(sport->irq_rx, pic32_uart_rx_interrupt,
  424. IRQF_NO_THREAD, sport->irq_rx_name, port);
  425. if (ret) {
  426. dev_err(port->dev, "%s: request irq(%d) err! ret:%d name:%s\n",
  427. __func__, sport->irq_rx, ret,
  428. pic32_uart_type(port));
  429. goto out_r;
  430. }
  431. sport->irq_tx_name = kasprintf(GFP_KERNEL, "%s%d-tx",
  432. pic32_uart_type(port),
  433. sport->idx);
  434. if (!sport->irq_tx_name) {
  435. dev_err(port->dev, "%s: kasprintf err!", __func__);
  436. ret = -ENOMEM;
  437. goto out_r;
  438. }
  439. irq_set_status_flags(sport->irq_tx, IRQ_NOAUTOEN);
  440. ret = request_irq(sport->irq_tx, pic32_uart_tx_interrupt,
  441. IRQF_NO_THREAD, sport->irq_tx_name, port);
  442. if (ret) {
  443. dev_err(port->dev, "%s: request irq(%d) err! ret:%d name:%s\n",
  444. __func__, sport->irq_tx, ret,
  445. pic32_uart_type(port));
  446. goto out_t;
  447. }
  448. local_irq_save(flags);
  449. /* set rx interrupt on first receive */
  450. pic32_uart_writel(sport, PIC32_CLR(PIC32_UART_STA),
  451. PIC32_UART_STA_URXISEL1 | PIC32_UART_STA_URXISEL0);
  452. /* set interrupt on empty */
  453. pic32_uart_writel(sport, PIC32_CLR(PIC32_UART_STA),
  454. PIC32_UART_STA_UTXISEL1);
  455. /* enable all interrupts and eanable uart */
  456. pic32_uart_en_and_unmask(port);
  457. local_irq_restore(flags);
  458. enable_irq(sport->irq_rx);
  459. return 0;
  460. out_t:
  461. free_irq(sport->irq_tx, port);
  462. kfree(sport->irq_tx_name);
  463. out_r:
  464. free_irq(sport->irq_rx, port);
  465. kfree(sport->irq_rx_name);
  466. out_f:
  467. free_irq(sport->irq_fault, port);
  468. kfree(sport->irq_fault_name);
  469. out_disable_clk:
  470. clk_disable_unprepare(sport->clk);
  471. out_done:
  472. return ret;
  473. }
  474. /* serial core request to flush & disable uart */
  475. static void pic32_uart_shutdown(struct uart_port *port)
  476. {
  477. struct pic32_sport *sport = to_pic32_sport(port);
  478. unsigned long flags;
  479. /* disable uart */
  480. spin_lock_irqsave(&port->lock, flags);
  481. pic32_uart_dsbl_and_mask(port);
  482. spin_unlock_irqrestore(&port->lock, flags);
  483. clk_disable_unprepare(sport->clk);
  484. /* free all 3 interrupts for this UART */
  485. free_irq(sport->irq_fault, port);
  486. kfree(sport->irq_fault_name);
  487. free_irq(sport->irq_tx, port);
  488. kfree(sport->irq_tx_name);
  489. free_irq(sport->irq_rx, port);
  490. kfree(sport->irq_rx_name);
  491. }
  492. /* serial core request to change current uart setting */
  493. static void pic32_uart_set_termios(struct uart_port *port,
  494. struct ktermios *new,
  495. const struct ktermios *old)
  496. {
  497. struct pic32_sport *sport = to_pic32_sport(port);
  498. unsigned int baud;
  499. unsigned int quot;
  500. unsigned long flags;
  501. spin_lock_irqsave(&port->lock, flags);
  502. /* disable uart and mask all interrupts while changing speed */
  503. pic32_uart_dsbl_and_mask(port);
  504. /* stop bit options */
  505. if (new->c_cflag & CSTOPB)
  506. pic32_uart_writel(sport, PIC32_SET(PIC32_UART_MODE),
  507. PIC32_UART_MODE_STSEL);
  508. else
  509. pic32_uart_writel(sport, PIC32_CLR(PIC32_UART_MODE),
  510. PIC32_UART_MODE_STSEL);
  511. /* parity options */
  512. if (new->c_cflag & PARENB) {
  513. if (new->c_cflag & PARODD) {
  514. pic32_uart_writel(sport, PIC32_SET(PIC32_UART_MODE),
  515. PIC32_UART_MODE_PDSEL1);
  516. pic32_uart_writel(sport, PIC32_CLR(PIC32_UART_MODE),
  517. PIC32_UART_MODE_PDSEL0);
  518. } else {
  519. pic32_uart_writel(sport, PIC32_SET(PIC32_UART_MODE),
  520. PIC32_UART_MODE_PDSEL0);
  521. pic32_uart_writel(sport, PIC32_CLR(PIC32_UART_MODE),
  522. PIC32_UART_MODE_PDSEL1);
  523. }
  524. } else {
  525. pic32_uart_writel(sport, PIC32_CLR(PIC32_UART_MODE),
  526. PIC32_UART_MODE_PDSEL1 |
  527. PIC32_UART_MODE_PDSEL0);
  528. }
  529. /* if hw flow ctrl, then the pins must be specified in device tree */
  530. if ((new->c_cflag & CRTSCTS) && sport->cts_gpiod) {
  531. /* enable hardware flow control */
  532. pic32_uart_writel(sport, PIC32_SET(PIC32_UART_MODE),
  533. PIC32_UART_MODE_UEN1);
  534. pic32_uart_writel(sport, PIC32_CLR(PIC32_UART_MODE),
  535. PIC32_UART_MODE_UEN0);
  536. pic32_uart_writel(sport, PIC32_CLR(PIC32_UART_MODE),
  537. PIC32_UART_MODE_RTSMD);
  538. } else {
  539. /* disable hardware flow control */
  540. pic32_uart_writel(sport, PIC32_CLR(PIC32_UART_MODE),
  541. PIC32_UART_MODE_UEN1);
  542. pic32_uart_writel(sport, PIC32_CLR(PIC32_UART_MODE),
  543. PIC32_UART_MODE_UEN0);
  544. pic32_uart_writel(sport, PIC32_CLR(PIC32_UART_MODE),
  545. PIC32_UART_MODE_RTSMD);
  546. }
  547. /* Always 8-bit */
  548. new->c_cflag |= CS8;
  549. /* Mark/Space parity is not supported */
  550. new->c_cflag &= ~CMSPAR;
  551. /* update baud */
  552. baud = uart_get_baud_rate(port, new, old, 0, port->uartclk / 16);
  553. quot = uart_get_divisor(port, baud) - 1;
  554. pic32_uart_writel(sport, PIC32_UART_BRG, quot);
  555. uart_update_timeout(port, new->c_cflag, baud);
  556. if (tty_termios_baud_rate(new))
  557. tty_termios_encode_baud_rate(new, baud, baud);
  558. /* enable uart */
  559. pic32_uart_en_and_unmask(port);
  560. spin_unlock_irqrestore(&port->lock, flags);
  561. }
  562. /* serial core request to claim uart iomem */
  563. static int pic32_uart_request_port(struct uart_port *port)
  564. {
  565. struct platform_device *pdev = to_platform_device(port->dev);
  566. struct resource *res_mem;
  567. res_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  568. if (unlikely(!res_mem))
  569. return -EINVAL;
  570. if (!request_mem_region(port->mapbase, resource_size(res_mem),
  571. "pic32_uart_mem"))
  572. return -EBUSY;
  573. port->membase = devm_ioremap(port->dev, port->mapbase,
  574. resource_size(res_mem));
  575. if (!port->membase) {
  576. dev_err(port->dev, "Unable to map registers\n");
  577. release_mem_region(port->mapbase, resource_size(res_mem));
  578. return -ENOMEM;
  579. }
  580. return 0;
  581. }
  582. /* serial core request to release uart iomem */
  583. static void pic32_uart_release_port(struct uart_port *port)
  584. {
  585. struct platform_device *pdev = to_platform_device(port->dev);
  586. struct resource *res_mem;
  587. unsigned int res_size;
  588. res_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  589. if (unlikely(!res_mem))
  590. return;
  591. res_size = resource_size(res_mem);
  592. release_mem_region(port->mapbase, res_size);
  593. }
  594. /* serial core request to do any port required auto-configuration */
  595. static void pic32_uart_config_port(struct uart_port *port, int flags)
  596. {
  597. if (flags & UART_CONFIG_TYPE) {
  598. if (pic32_uart_request_port(port))
  599. return;
  600. port->type = PORT_PIC32;
  601. }
  602. }
  603. /* serial core request to check that port information in serinfo are suitable */
  604. static int pic32_uart_verify_port(struct uart_port *port,
  605. struct serial_struct *serinfo)
  606. {
  607. if (port->type != PORT_PIC32)
  608. return -EINVAL;
  609. if (port->irq != serinfo->irq)
  610. return -EINVAL;
  611. if (port->iotype != serinfo->io_type)
  612. return -EINVAL;
  613. if (port->mapbase != (unsigned long)serinfo->iomem_base)
  614. return -EINVAL;
  615. return 0;
  616. }
  617. /* serial core callbacks */
  618. static const struct uart_ops pic32_uart_ops = {
  619. .tx_empty = pic32_uart_tx_empty,
  620. .get_mctrl = pic32_uart_get_mctrl,
  621. .set_mctrl = pic32_uart_set_mctrl,
  622. .start_tx = pic32_uart_start_tx,
  623. .stop_tx = pic32_uart_stop_tx,
  624. .stop_rx = pic32_uart_stop_rx,
  625. .break_ctl = pic32_uart_break_ctl,
  626. .startup = pic32_uart_startup,
  627. .shutdown = pic32_uart_shutdown,
  628. .set_termios = pic32_uart_set_termios,
  629. .type = pic32_uart_type,
  630. .release_port = pic32_uart_release_port,
  631. .request_port = pic32_uart_request_port,
  632. .config_port = pic32_uart_config_port,
  633. .verify_port = pic32_uart_verify_port,
  634. };
  635. #ifdef CONFIG_SERIAL_PIC32_CONSOLE
  636. /* output given char */
  637. static void pic32_console_putchar(struct uart_port *port, unsigned char ch)
  638. {
  639. struct pic32_sport *sport = to_pic32_sport(port);
  640. if (!(pic32_uart_readl(sport, PIC32_UART_MODE) & PIC32_UART_MODE_ON))
  641. return;
  642. if (!(pic32_uart_readl(sport, PIC32_UART_STA) & PIC32_UART_STA_UTXEN))
  643. return;
  644. /* wait for tx empty */
  645. pic32_wait_deplete_txbuf(sport);
  646. pic32_uart_writel(sport, PIC32_UART_TX, ch & 0xff);
  647. }
  648. /* console core request to output given string */
  649. static void pic32_console_write(struct console *co, const char *s,
  650. unsigned int count)
  651. {
  652. struct pic32_sport *sport = pic32_sports[co->index];
  653. /* call uart helper to deal with \r\n */
  654. uart_console_write(&sport->port, s, count, pic32_console_putchar);
  655. }
  656. /* console core request to setup given console, find matching uart
  657. * port and setup it.
  658. */
  659. static int pic32_console_setup(struct console *co, char *options)
  660. {
  661. struct pic32_sport *sport;
  662. int baud = 115200;
  663. int bits = 8;
  664. int parity = 'n';
  665. int flow = 'n';
  666. int ret = 0;
  667. if (unlikely(co->index < 0 || co->index >= PIC32_MAX_UARTS))
  668. return -ENODEV;
  669. sport = pic32_sports[co->index];
  670. if (!sport)
  671. return -ENODEV;
  672. ret = clk_prepare_enable(sport->clk);
  673. if (ret)
  674. return ret;
  675. if (options)
  676. uart_parse_options(options, &baud, &parity, &bits, &flow);
  677. return uart_set_options(&sport->port, co, baud, parity, bits, flow);
  678. }
  679. static struct uart_driver pic32_uart_driver;
  680. static struct console pic32_console = {
  681. .name = PIC32_SDEV_NAME,
  682. .write = pic32_console_write,
  683. .device = uart_console_device,
  684. .setup = pic32_console_setup,
  685. .flags = CON_PRINTBUFFER,
  686. .index = -1,
  687. .data = &pic32_uart_driver,
  688. };
  689. #define PIC32_SCONSOLE (&pic32_console)
  690. static int __init pic32_console_init(void)
  691. {
  692. register_console(&pic32_console);
  693. return 0;
  694. }
  695. console_initcall(pic32_console_init);
  696. /*
  697. * Late console initialization.
  698. */
  699. static int __init pic32_late_console_init(void)
  700. {
  701. if (!(pic32_console.flags & CON_ENABLED))
  702. register_console(&pic32_console);
  703. return 0;
  704. }
  705. core_initcall(pic32_late_console_init);
  706. #else
  707. #define PIC32_SCONSOLE NULL
  708. #endif
  709. static struct uart_driver pic32_uart_driver = {
  710. .owner = THIS_MODULE,
  711. .driver_name = PIC32_DEV_NAME,
  712. .dev_name = PIC32_SDEV_NAME,
  713. .nr = PIC32_MAX_UARTS,
  714. .cons = PIC32_SCONSOLE,
  715. };
  716. static int pic32_uart_probe(struct platform_device *pdev)
  717. {
  718. struct device *dev = &pdev->dev;
  719. struct device_node *np = dev->of_node;
  720. struct pic32_sport *sport;
  721. int uart_idx = 0;
  722. struct resource *res_mem;
  723. struct uart_port *port;
  724. int ret;
  725. uart_idx = of_alias_get_id(np, "serial");
  726. if (uart_idx < 0 || uart_idx >= PIC32_MAX_UARTS)
  727. return -EINVAL;
  728. res_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  729. if (!res_mem)
  730. return -EINVAL;
  731. sport = devm_kzalloc(&pdev->dev, sizeof(*sport), GFP_KERNEL);
  732. if (!sport)
  733. return -ENOMEM;
  734. sport->idx = uart_idx;
  735. sport->irq_fault = irq_of_parse_and_map(np, 0);
  736. sport->irq_rx = irq_of_parse_and_map(np, 1);
  737. sport->irq_tx = irq_of_parse_and_map(np, 2);
  738. sport->clk = devm_clk_get(&pdev->dev, NULL);
  739. sport->dev = &pdev->dev;
  740. /* Hardware flow control: gpios
  741. * !Note: Basically, CTS is needed for reading the status.
  742. */
  743. sport->cts_gpiod = devm_gpiod_get_optional(dev, "cts", GPIOD_IN);
  744. if (IS_ERR(sport->cts_gpiod))
  745. return dev_err_probe(dev, PTR_ERR(sport->cts_gpiod), "error requesting CTS GPIO\n");
  746. gpiod_set_consumer_name(sport->cts_gpiod, "CTS");
  747. pic32_sports[uart_idx] = sport;
  748. port = &sport->port;
  749. port->iotype = UPIO_MEM;
  750. port->mapbase = res_mem->start;
  751. port->ops = &pic32_uart_ops;
  752. port->flags = UPF_BOOT_AUTOCONF;
  753. port->dev = &pdev->dev;
  754. port->fifosize = PIC32_UART_TX_FIFO_DEPTH;
  755. port->uartclk = clk_get_rate(sport->clk);
  756. port->line = uart_idx;
  757. ret = uart_add_one_port(&pic32_uart_driver, port);
  758. if (ret) {
  759. port->membase = NULL;
  760. dev_err(port->dev, "%s: uart add port error!\n", __func__);
  761. goto err;
  762. }
  763. #ifdef CONFIG_SERIAL_PIC32_CONSOLE
  764. if (uart_console_enabled(port)) {
  765. /* The peripheral clock has been enabled by console_setup,
  766. * so disable it till the port is used.
  767. */
  768. clk_disable_unprepare(sport->clk);
  769. }
  770. #endif
  771. platform_set_drvdata(pdev, port);
  772. dev_info(&pdev->dev, "%s: uart(%d) driver initialized.\n",
  773. __func__, uart_idx);
  774. return 0;
  775. err:
  776. /* automatic unroll of sport and gpios */
  777. return ret;
  778. }
  779. static int pic32_uart_remove(struct platform_device *pdev)
  780. {
  781. struct uart_port *port = platform_get_drvdata(pdev);
  782. struct pic32_sport *sport = to_pic32_sport(port);
  783. uart_remove_one_port(&pic32_uart_driver, port);
  784. clk_disable_unprepare(sport->clk);
  785. platform_set_drvdata(pdev, NULL);
  786. pic32_sports[sport->idx] = NULL;
  787. /* automatic unroll of sport and gpios */
  788. return 0;
  789. }
  790. static const struct of_device_id pic32_serial_dt_ids[] = {
  791. { .compatible = "microchip,pic32mzda-uart" },
  792. { /* sentinel */ }
  793. };
  794. MODULE_DEVICE_TABLE(of, pic32_serial_dt_ids);
  795. static struct platform_driver pic32_uart_platform_driver = {
  796. .probe = pic32_uart_probe,
  797. .remove = pic32_uart_remove,
  798. .driver = {
  799. .name = PIC32_DEV_NAME,
  800. .of_match_table = of_match_ptr(pic32_serial_dt_ids),
  801. .suppress_bind_attrs = IS_BUILTIN(CONFIG_SERIAL_PIC32),
  802. },
  803. };
  804. static int __init pic32_uart_init(void)
  805. {
  806. int ret;
  807. ret = uart_register_driver(&pic32_uart_driver);
  808. if (ret) {
  809. pr_err("failed to register %s:%d\n",
  810. pic32_uart_driver.driver_name, ret);
  811. return ret;
  812. }
  813. ret = platform_driver_register(&pic32_uart_platform_driver);
  814. if (ret) {
  815. pr_err("fail to register pic32 uart\n");
  816. uart_unregister_driver(&pic32_uart_driver);
  817. }
  818. return ret;
  819. }
  820. arch_initcall(pic32_uart_init);
  821. static void __exit pic32_uart_exit(void)
  822. {
  823. #ifdef CONFIG_SERIAL_PIC32_CONSOLE
  824. unregister_console(&pic32_console);
  825. #endif
  826. platform_driver_unregister(&pic32_uart_platform_driver);
  827. uart_unregister_driver(&pic32_uart_driver);
  828. }
  829. module_exit(pic32_uart_exit);
  830. MODULE_AUTHOR("Sorin-Andrei Pistirica <[email protected]>");
  831. MODULE_DESCRIPTION("Microchip PIC32 integrated serial port driver");
  832. MODULE_LICENSE("GPL v2");