pic32_uart.c 24 KB

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