pic32_uart.c 24 KB

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