pic32_uart.c 26 KB

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