etraxfs-uart.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959
  1. #include <linux/module.h>
  2. #include <linux/init.h>
  3. #include <linux/console.h>
  4. #include <linux/platform_device.h>
  5. #include <linux/serial_core.h>
  6. #include <linux/tty_flip.h>
  7. #include <linux/of.h>
  8. #include <linux/gpio.h>
  9. #include <linux/of_irq.h>
  10. #include <linux/of_address.h>
  11. #include <hwregs/ser_defs.h>
  12. #include "serial_mctrl_gpio.h"
  13. #define DRV_NAME "etraxfs-uart"
  14. #define UART_NR CONFIG_ETRAX_SERIAL_PORTS
  15. #define MODIFY_REG(instance, reg, var) \
  16. do { \
  17. if (REG_RD_INT(ser, instance, reg) != \
  18. REG_TYPE_CONV(int, reg_ser_##reg, var)) \
  19. REG_WR(ser, instance, reg, var); \
  20. } while (0)
  21. struct uart_cris_port {
  22. struct uart_port port;
  23. int initialized;
  24. int irq;
  25. void __iomem *regi_ser;
  26. struct mctrl_gpios *gpios;
  27. int write_ongoing;
  28. };
  29. static struct uart_driver etraxfs_uart_driver;
  30. static struct uart_port *console_port;
  31. static int console_baud = 115200;
  32. static struct uart_cris_port *etraxfs_uart_ports[UART_NR];
  33. static void cris_serial_port_init(struct uart_port *port, int line);
  34. static void etraxfs_uart_stop_rx(struct uart_port *port);
  35. static inline void etraxfs_uart_start_tx_bottom(struct uart_port *port);
  36. #ifdef CONFIG_SERIAL_ETRAXFS_CONSOLE
  37. static void
  38. cris_console_write(struct console *co, const char *s, unsigned int count)
  39. {
  40. struct uart_cris_port *up;
  41. int i;
  42. reg_ser_r_stat_din stat;
  43. reg_ser_rw_tr_dma_en tr_dma_en, old;
  44. up = etraxfs_uart_ports[co->index];
  45. if (!up)
  46. return;
  47. /* Switch to manual mode. */
  48. tr_dma_en = old = REG_RD(ser, up->regi_ser, rw_tr_dma_en);
  49. if (tr_dma_en.en == regk_ser_yes) {
  50. tr_dma_en.en = regk_ser_no;
  51. REG_WR(ser, up->regi_ser, rw_tr_dma_en, tr_dma_en);
  52. }
  53. /* Send data. */
  54. for (i = 0; i < count; i++) {
  55. /* LF -> CRLF */
  56. if (s[i] == '\n') {
  57. do {
  58. stat = REG_RD(ser, up->regi_ser, r_stat_din);
  59. } while (!stat.tr_rdy);
  60. REG_WR_INT(ser, up->regi_ser, rw_dout, '\r');
  61. }
  62. /* Wait until transmitter is ready and send. */
  63. do {
  64. stat = REG_RD(ser, up->regi_ser, r_stat_din);
  65. } while (!stat.tr_rdy);
  66. REG_WR_INT(ser, up->regi_ser, rw_dout, s[i]);
  67. }
  68. /* Restore mode. */
  69. if (tr_dma_en.en != old.en)
  70. REG_WR(ser, up->regi_ser, rw_tr_dma_en, old);
  71. }
  72. static int __init
  73. cris_console_setup(struct console *co, char *options)
  74. {
  75. struct uart_port *port;
  76. int baud = 115200;
  77. int bits = 8;
  78. int parity = 'n';
  79. int flow = 'n';
  80. if (co->index < 0 || co->index >= UART_NR)
  81. co->index = 0;
  82. port = &etraxfs_uart_ports[co->index]->port;
  83. console_port = port;
  84. co->flags |= CON_CONSDEV;
  85. if (options)
  86. uart_parse_options(options, &baud, &parity, &bits, &flow);
  87. console_baud = baud;
  88. cris_serial_port_init(port, co->index);
  89. uart_set_options(port, co, baud, parity, bits, flow);
  90. return 0;
  91. }
  92. static struct console cris_console = {
  93. .name = "ttyS",
  94. .write = cris_console_write,
  95. .device = uart_console_device,
  96. .setup = cris_console_setup,
  97. .flags = CON_PRINTBUFFER,
  98. .index = -1,
  99. .data = &etraxfs_uart_driver,
  100. };
  101. #endif /* CONFIG_SERIAL_ETRAXFS_CONSOLE */
  102. static struct uart_driver etraxfs_uart_driver = {
  103. .owner = THIS_MODULE,
  104. .driver_name = "serial",
  105. .dev_name = "ttyS",
  106. .major = TTY_MAJOR,
  107. .minor = 64,
  108. .nr = UART_NR,
  109. #ifdef CONFIG_SERIAL_ETRAXFS_CONSOLE
  110. .cons = &cris_console,
  111. #endif /* CONFIG_SERIAL_ETRAXFS_CONSOLE */
  112. };
  113. static inline int crisv32_serial_get_rts(struct uart_cris_port *up)
  114. {
  115. void __iomem *regi_ser = up->regi_ser;
  116. /*
  117. * Return what the user has controlled rts to or
  118. * what the pin is? (if auto_rts is used it differs during tx)
  119. */
  120. reg_ser_r_stat_din rstat = REG_RD(ser, regi_ser, r_stat_din);
  121. return !(rstat.rts_n == regk_ser_active);
  122. }
  123. /*
  124. * A set = 0 means 3.3V on the pin, bitvalue: 0=active, 1=inactive
  125. * 0=0V , 1=3.3V
  126. */
  127. static inline void crisv32_serial_set_rts(struct uart_cris_port *up,
  128. int set, int force)
  129. {
  130. void __iomem *regi_ser = up->regi_ser;
  131. unsigned long flags;
  132. reg_ser_rw_rec_ctrl rec_ctrl;
  133. local_irq_save(flags);
  134. rec_ctrl = REG_RD(ser, regi_ser, rw_rec_ctrl);
  135. if (set)
  136. rec_ctrl.rts_n = regk_ser_active;
  137. else
  138. rec_ctrl.rts_n = regk_ser_inactive;
  139. REG_WR(ser, regi_ser, rw_rec_ctrl, rec_ctrl);
  140. local_irq_restore(flags);
  141. }
  142. static inline int crisv32_serial_get_cts(struct uart_cris_port *up)
  143. {
  144. void __iomem *regi_ser = up->regi_ser;
  145. reg_ser_r_stat_din rstat = REG_RD(ser, regi_ser, r_stat_din);
  146. return (rstat.cts_n == regk_ser_active);
  147. }
  148. /*
  149. * Send a single character for XON/XOFF purposes. We do it in this separate
  150. * function instead of the alternative support port.x_char, in the ...start_tx
  151. * function, so we don't mix up this case with possibly enabling transmission
  152. * of queued-up data (in case that's disabled after *receiving* an XOFF or
  153. * negative CTS). This function is used for both DMA and non-DMA case; see HW
  154. * docs specifically blessing sending characters manually when DMA for
  155. * transmission is enabled and running. We may be asked to transmit despite
  156. * the transmitter being disabled by a ..._stop_tx call so we need to enable
  157. * it temporarily but restore the state afterwards.
  158. */
  159. static void etraxfs_uart_send_xchar(struct uart_port *port, char ch)
  160. {
  161. struct uart_cris_port *up = (struct uart_cris_port *)port;
  162. reg_ser_rw_dout dout = { .data = ch };
  163. reg_ser_rw_ack_intr ack_intr = { .tr_rdy = regk_ser_yes };
  164. reg_ser_r_stat_din rstat;
  165. reg_ser_rw_tr_ctrl prev_tr_ctrl, tr_ctrl;
  166. void __iomem *regi_ser = up->regi_ser;
  167. unsigned long flags;
  168. /*
  169. * Wait for tr_rdy in case a character is already being output. Make
  170. * sure we have integrity between the register reads and the writes
  171. * below, but don't busy-wait with interrupts off and the port lock
  172. * taken.
  173. */
  174. spin_lock_irqsave(&port->lock, flags);
  175. do {
  176. spin_unlock_irqrestore(&port->lock, flags);
  177. spin_lock_irqsave(&port->lock, flags);
  178. prev_tr_ctrl = tr_ctrl = REG_RD(ser, regi_ser, rw_tr_ctrl);
  179. rstat = REG_RD(ser, regi_ser, r_stat_din);
  180. } while (!rstat.tr_rdy);
  181. /*
  182. * Ack an interrupt if one was just issued for the previous character
  183. * that was output. This is required for non-DMA as the interrupt is
  184. * used as the only indicator that the transmitter is ready and it
  185. * isn't while this x_char is being transmitted.
  186. */
  187. REG_WR(ser, regi_ser, rw_ack_intr, ack_intr);
  188. /* Enable the transmitter in case it was disabled. */
  189. tr_ctrl.stop = 0;
  190. REG_WR(ser, regi_ser, rw_tr_ctrl, tr_ctrl);
  191. /*
  192. * Finally, send the blessed character; nothing should stop it now,
  193. * except for an xoff-detected state, which we'll handle below.
  194. */
  195. REG_WR(ser, regi_ser, rw_dout, dout);
  196. up->port.icount.tx++;
  197. /* There might be an xoff state to clear. */
  198. rstat = REG_RD(ser, up->regi_ser, r_stat_din);
  199. /*
  200. * Clear any xoff state that *may* have been there to
  201. * inhibit transmission of the character.
  202. */
  203. if (rstat.xoff_detect) {
  204. reg_ser_rw_xoff_clr xoff_clr = { .clr = 1 };
  205. reg_ser_rw_tr_dma_en tr_dma_en;
  206. REG_WR(ser, regi_ser, rw_xoff_clr, xoff_clr);
  207. tr_dma_en = REG_RD(ser, regi_ser, rw_tr_dma_en);
  208. /*
  209. * If we had an xoff state but cleared it, instead sneak in a
  210. * disabled state for the transmitter, after the character we
  211. * sent. Thus we keep the port disabled, just as if the xoff
  212. * state was still in effect (or actually, as if stop_tx had
  213. * been called, as we stop DMA too).
  214. */
  215. prev_tr_ctrl.stop = 1;
  216. tr_dma_en.en = 0;
  217. REG_WR(ser, regi_ser, rw_tr_dma_en, tr_dma_en);
  218. }
  219. /* Restore "previous" enabled/disabled state of the transmitter. */
  220. REG_WR(ser, regi_ser, rw_tr_ctrl, prev_tr_ctrl);
  221. spin_unlock_irqrestore(&port->lock, flags);
  222. }
  223. /*
  224. * Do not spin_lock_irqsave or disable interrupts by other means here; it's
  225. * already done by the caller.
  226. */
  227. static void etraxfs_uart_start_tx(struct uart_port *port)
  228. {
  229. struct uart_cris_port *up = (struct uart_cris_port *)port;
  230. /* we have already done below if a write is ongoing */
  231. if (up->write_ongoing)
  232. return;
  233. /* Signal that write is ongoing */
  234. up->write_ongoing = 1;
  235. etraxfs_uart_start_tx_bottom(port);
  236. }
  237. static inline void etraxfs_uart_start_tx_bottom(struct uart_port *port)
  238. {
  239. struct uart_cris_port *up = (struct uart_cris_port *)port;
  240. void __iomem *regi_ser = up->regi_ser;
  241. reg_ser_rw_tr_ctrl tr_ctrl;
  242. reg_ser_rw_intr_mask intr_mask;
  243. tr_ctrl = REG_RD(ser, regi_ser, rw_tr_ctrl);
  244. tr_ctrl.stop = regk_ser_no;
  245. REG_WR(ser, regi_ser, rw_tr_ctrl, tr_ctrl);
  246. intr_mask = REG_RD(ser, regi_ser, rw_intr_mask);
  247. intr_mask.tr_rdy = regk_ser_yes;
  248. REG_WR(ser, regi_ser, rw_intr_mask, intr_mask);
  249. }
  250. /*
  251. * This function handles both the DMA and non-DMA case by ordering the
  252. * transmitter to stop of after the current character. We don't need to wait
  253. * for any such character to be completely transmitted; we do that where it
  254. * matters, like in etraxfs_uart_set_termios. Don't busy-wait here; see
  255. * Documentation/serial/driver: this function is called within
  256. * spin_lock_irq{,save} and thus separate ones would be disastrous (when SMP).
  257. * There's no documented need to set the txd pin to any particular value;
  258. * break setting is controlled solely by etraxfs_uart_break_ctl.
  259. */
  260. static void etraxfs_uart_stop_tx(struct uart_port *port)
  261. {
  262. struct uart_cris_port *up = (struct uart_cris_port *)port;
  263. void __iomem *regi_ser = up->regi_ser;
  264. reg_ser_rw_tr_ctrl tr_ctrl;
  265. reg_ser_rw_intr_mask intr_mask;
  266. reg_ser_rw_tr_dma_en tr_dma_en = {0};
  267. reg_ser_rw_xoff_clr xoff_clr = {0};
  268. /*
  269. * For the non-DMA case, we'd get a tr_rdy interrupt that we're not
  270. * interested in as we're not transmitting any characters. For the
  271. * DMA case, that interrupt is already turned off, but no reason to
  272. * waste code on conditionals here.
  273. */
  274. intr_mask = REG_RD(ser, regi_ser, rw_intr_mask);
  275. intr_mask.tr_rdy = regk_ser_no;
  276. REG_WR(ser, regi_ser, rw_intr_mask, intr_mask);
  277. tr_ctrl = REG_RD(ser, regi_ser, rw_tr_ctrl);
  278. tr_ctrl.stop = 1;
  279. REG_WR(ser, regi_ser, rw_tr_ctrl, tr_ctrl);
  280. /*
  281. * Always clear possible hardware xoff-detected state here, no need to
  282. * unnecessary consider mctrl settings and when they change. We clear
  283. * it here rather than in start_tx: both functions are called as the
  284. * effect of XOFF processing, but start_tx is also called when upper
  285. * levels tell the driver that there are more characters to send, so
  286. * avoid adding code there.
  287. */
  288. xoff_clr.clr = 1;
  289. REG_WR(ser, regi_ser, rw_xoff_clr, xoff_clr);
  290. /*
  291. * Disable transmitter DMA, so that if we're in XON/XOFF, we can send
  292. * those single characters without also giving go-ahead for queued up
  293. * DMA data.
  294. */
  295. tr_dma_en.en = 0;
  296. REG_WR(ser, regi_ser, rw_tr_dma_en, tr_dma_en);
  297. /*
  298. * Make sure that write_ongoing is reset when stopping tx.
  299. */
  300. up->write_ongoing = 0;
  301. }
  302. static void etraxfs_uart_stop_rx(struct uart_port *port)
  303. {
  304. struct uart_cris_port *up = (struct uart_cris_port *)port;
  305. void __iomem *regi_ser = up->regi_ser;
  306. reg_ser_rw_rec_ctrl rec_ctrl = REG_RD(ser, regi_ser, rw_rec_ctrl);
  307. rec_ctrl.en = regk_ser_no;
  308. REG_WR(ser, regi_ser, rw_rec_ctrl, rec_ctrl);
  309. }
  310. static unsigned int etraxfs_uart_tx_empty(struct uart_port *port)
  311. {
  312. struct uart_cris_port *up = (struct uart_cris_port *)port;
  313. unsigned long flags;
  314. unsigned int ret;
  315. reg_ser_r_stat_din rstat = {0};
  316. spin_lock_irqsave(&up->port.lock, flags);
  317. rstat = REG_RD(ser, up->regi_ser, r_stat_din);
  318. ret = rstat.tr_empty ? TIOCSER_TEMT : 0;
  319. spin_unlock_irqrestore(&up->port.lock, flags);
  320. return ret;
  321. }
  322. static unsigned int etraxfs_uart_get_mctrl(struct uart_port *port)
  323. {
  324. struct uart_cris_port *up = (struct uart_cris_port *)port;
  325. unsigned int ret;
  326. ret = 0;
  327. if (crisv32_serial_get_rts(up))
  328. ret |= TIOCM_RTS;
  329. if (crisv32_serial_get_cts(up))
  330. ret |= TIOCM_CTS;
  331. return mctrl_gpio_get(up->gpios, &ret);
  332. }
  333. static void etraxfs_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
  334. {
  335. struct uart_cris_port *up = (struct uart_cris_port *)port;
  336. crisv32_serial_set_rts(up, mctrl & TIOCM_RTS ? 1 : 0, 0);
  337. mctrl_gpio_set(up->gpios, mctrl);
  338. }
  339. static void etraxfs_uart_break_ctl(struct uart_port *port, int break_state)
  340. {
  341. struct uart_cris_port *up = (struct uart_cris_port *)port;
  342. unsigned long flags;
  343. reg_ser_rw_tr_ctrl tr_ctrl;
  344. reg_ser_rw_tr_dma_en tr_dma_en;
  345. reg_ser_rw_intr_mask intr_mask;
  346. spin_lock_irqsave(&up->port.lock, flags);
  347. tr_ctrl = REG_RD(ser, up->regi_ser, rw_tr_ctrl);
  348. tr_dma_en = REG_RD(ser, up->regi_ser, rw_tr_dma_en);
  349. intr_mask = REG_RD(ser, up->regi_ser, rw_intr_mask);
  350. if (break_state != 0) { /* Send break */
  351. /*
  352. * We need to disable DMA (if used) or tr_rdy interrupts if no
  353. * DMA. No need to make this conditional on use of DMA;
  354. * disabling will be a no-op for the other mode.
  355. */
  356. intr_mask.tr_rdy = regk_ser_no;
  357. tr_dma_en.en = 0;
  358. /*
  359. * Stop transmission and set the txd pin to 0 after the
  360. * current character. The txd setting will take effect after
  361. * any current transmission has completed.
  362. */
  363. tr_ctrl.stop = 1;
  364. tr_ctrl.txd = 0;
  365. } else {
  366. /* Re-enable the serial interrupt. */
  367. intr_mask.tr_rdy = regk_ser_yes;
  368. tr_ctrl.stop = 0;
  369. tr_ctrl.txd = 1;
  370. }
  371. REG_WR(ser, up->regi_ser, rw_tr_ctrl, tr_ctrl);
  372. REG_WR(ser, up->regi_ser, rw_tr_dma_en, tr_dma_en);
  373. REG_WR(ser, up->regi_ser, rw_intr_mask, intr_mask);
  374. spin_unlock_irqrestore(&up->port.lock, flags);
  375. }
  376. static void
  377. transmit_chars_no_dma(struct uart_cris_port *up)
  378. {
  379. int max_count;
  380. struct circ_buf *xmit = &up->port.state->xmit;
  381. void __iomem *regi_ser = up->regi_ser;
  382. reg_ser_r_stat_din rstat;
  383. reg_ser_rw_ack_intr ack_intr = { .tr_rdy = regk_ser_yes };
  384. if (uart_circ_empty(xmit) || uart_tx_stopped(&up->port)) {
  385. /* No more to send, so disable the interrupt. */
  386. reg_ser_rw_intr_mask intr_mask;
  387. intr_mask = REG_RD(ser, regi_ser, rw_intr_mask);
  388. intr_mask.tr_rdy = 0;
  389. intr_mask.tr_empty = 0;
  390. REG_WR(ser, regi_ser, rw_intr_mask, intr_mask);
  391. up->write_ongoing = 0;
  392. return;
  393. }
  394. /* If the serport is fast, we send up to max_count bytes before
  395. exiting the loop. */
  396. max_count = 64;
  397. do {
  398. reg_ser_rw_dout dout = { .data = xmit->buf[xmit->tail] };
  399. REG_WR(ser, regi_ser, rw_dout, dout);
  400. REG_WR(ser, regi_ser, rw_ack_intr, ack_intr);
  401. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE-1);
  402. up->port.icount.tx++;
  403. if (xmit->head == xmit->tail)
  404. break;
  405. rstat = REG_RD(ser, regi_ser, r_stat_din);
  406. } while ((--max_count > 0) && rstat.tr_rdy);
  407. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  408. uart_write_wakeup(&up->port);
  409. }
  410. static void receive_chars_no_dma(struct uart_cris_port *up)
  411. {
  412. reg_ser_rs_stat_din stat_din;
  413. reg_ser_r_stat_din rstat;
  414. struct tty_port *port;
  415. struct uart_icount *icount;
  416. int max_count = 16;
  417. char flag;
  418. reg_ser_rw_ack_intr ack_intr = { 0 };
  419. rstat = REG_RD(ser, up->regi_ser, r_stat_din);
  420. icount = &up->port.icount;
  421. port = &up->port.state->port;
  422. do {
  423. stat_din = REG_RD(ser, up->regi_ser, rs_stat_din);
  424. flag = TTY_NORMAL;
  425. ack_intr.dav = 1;
  426. REG_WR(ser, up->regi_ser, rw_ack_intr, ack_intr);
  427. icount->rx++;
  428. if (stat_din.framing_err | stat_din.par_err | stat_din.orun) {
  429. if (stat_din.data == 0x00 &&
  430. stat_din.framing_err) {
  431. /* Most likely a break. */
  432. flag = TTY_BREAK;
  433. icount->brk++;
  434. } else if (stat_din.par_err) {
  435. flag = TTY_PARITY;
  436. icount->parity++;
  437. } else if (stat_din.orun) {
  438. flag = TTY_OVERRUN;
  439. icount->overrun++;
  440. } else if (stat_din.framing_err) {
  441. flag = TTY_FRAME;
  442. icount->frame++;
  443. }
  444. }
  445. /*
  446. * If this becomes important, we probably *could* handle this
  447. * gracefully by keeping track of the unhandled character.
  448. */
  449. if (!tty_insert_flip_char(port, stat_din.data, flag))
  450. panic("%s: No tty buffer space", __func__);
  451. rstat = REG_RD(ser, up->regi_ser, r_stat_din);
  452. } while (rstat.dav && (max_count-- > 0));
  453. spin_unlock(&up->port.lock);
  454. tty_flip_buffer_push(port);
  455. spin_lock(&up->port.lock);
  456. }
  457. static irqreturn_t
  458. ser_interrupt(int irq, void *dev_id)
  459. {
  460. struct uart_cris_port *up = (struct uart_cris_port *)dev_id;
  461. void __iomem *regi_ser;
  462. int handled = 0;
  463. spin_lock(&up->port.lock);
  464. regi_ser = up->regi_ser;
  465. if (regi_ser) {
  466. reg_ser_r_masked_intr masked_intr;
  467. masked_intr = REG_RD(ser, regi_ser, r_masked_intr);
  468. /*
  469. * Check what interrupts are active before taking
  470. * actions. If DMA is used the interrupt shouldn't
  471. * be enabled.
  472. */
  473. if (masked_intr.dav) {
  474. receive_chars_no_dma(up);
  475. handled = 1;
  476. }
  477. if (masked_intr.tr_rdy) {
  478. transmit_chars_no_dma(up);
  479. handled = 1;
  480. }
  481. }
  482. spin_unlock(&up->port.lock);
  483. return IRQ_RETVAL(handled);
  484. }
  485. #ifdef CONFIG_CONSOLE_POLL
  486. static int etraxfs_uart_get_poll_char(struct uart_port *port)
  487. {
  488. reg_ser_rs_stat_din stat;
  489. reg_ser_rw_ack_intr ack_intr = { 0 };
  490. struct uart_cris_port *up = (struct uart_cris_port *)port;
  491. do {
  492. stat = REG_RD(ser, up->regi_ser, rs_stat_din);
  493. } while (!stat.dav);
  494. /* Ack the data_avail interrupt. */
  495. ack_intr.dav = 1;
  496. REG_WR(ser, up->regi_ser, rw_ack_intr, ack_intr);
  497. return stat.data;
  498. }
  499. static void etraxfs_uart_put_poll_char(struct uart_port *port,
  500. unsigned char c)
  501. {
  502. reg_ser_r_stat_din stat;
  503. struct uart_cris_port *up = (struct uart_cris_port *)port;
  504. do {
  505. stat = REG_RD(ser, up->regi_ser, r_stat_din);
  506. } while (!stat.tr_rdy);
  507. REG_WR_INT(ser, up->regi_ser, rw_dout, c);
  508. }
  509. #endif /* CONFIG_CONSOLE_POLL */
  510. static int etraxfs_uart_startup(struct uart_port *port)
  511. {
  512. struct uart_cris_port *up = (struct uart_cris_port *)port;
  513. unsigned long flags;
  514. reg_ser_rw_intr_mask ser_intr_mask = {0};
  515. ser_intr_mask.dav = regk_ser_yes;
  516. if (request_irq(etraxfs_uart_ports[port->line]->irq, ser_interrupt,
  517. 0, DRV_NAME, etraxfs_uart_ports[port->line]))
  518. panic("irq ser%d", port->line);
  519. spin_lock_irqsave(&up->port.lock, flags);
  520. REG_WR(ser, up->regi_ser, rw_intr_mask, ser_intr_mask);
  521. etraxfs_uart_set_mctrl(&up->port, up->port.mctrl);
  522. spin_unlock_irqrestore(&up->port.lock, flags);
  523. return 0;
  524. }
  525. static void etraxfs_uart_shutdown(struct uart_port *port)
  526. {
  527. struct uart_cris_port *up = (struct uart_cris_port *)port;
  528. unsigned long flags;
  529. spin_lock_irqsave(&up->port.lock, flags);
  530. etraxfs_uart_stop_tx(port);
  531. etraxfs_uart_stop_rx(port);
  532. free_irq(etraxfs_uart_ports[port->line]->irq,
  533. etraxfs_uart_ports[port->line]);
  534. etraxfs_uart_set_mctrl(&up->port, up->port.mctrl);
  535. spin_unlock_irqrestore(&up->port.lock, flags);
  536. }
  537. static void
  538. etraxfs_uart_set_termios(struct uart_port *port, struct ktermios *termios,
  539. struct ktermios *old)
  540. {
  541. struct uart_cris_port *up = (struct uart_cris_port *)port;
  542. unsigned long flags;
  543. reg_ser_rw_xoff xoff;
  544. reg_ser_rw_xoff_clr xoff_clr = {0};
  545. reg_ser_rw_tr_ctrl tx_ctrl = {0};
  546. reg_ser_rw_tr_dma_en tx_dma_en = {0};
  547. reg_ser_rw_rec_ctrl rx_ctrl = {0};
  548. reg_ser_rw_tr_baud_div tx_baud_div = {0};
  549. reg_ser_rw_rec_baud_div rx_baud_div = {0};
  550. int baud;
  551. if (old &&
  552. termios->c_cflag == old->c_cflag &&
  553. termios->c_iflag == old->c_iflag)
  554. return;
  555. /* Tx: 8 bit, no/even parity, 1 stop bit, no cts. */
  556. tx_ctrl.base_freq = regk_ser_f29_493;
  557. tx_ctrl.en = 0;
  558. tx_ctrl.stop = 0;
  559. tx_ctrl.auto_rts = regk_ser_no;
  560. tx_ctrl.txd = 1;
  561. tx_ctrl.auto_cts = 0;
  562. /* Rx: 8 bit, no/even parity. */
  563. rx_ctrl.dma_err = regk_ser_stop;
  564. rx_ctrl.sampling = regk_ser_majority;
  565. rx_ctrl.timeout = 1;
  566. rx_ctrl.rts_n = regk_ser_inactive;
  567. /* Common for tx and rx: 8N1. */
  568. tx_ctrl.data_bits = regk_ser_bits8;
  569. rx_ctrl.data_bits = regk_ser_bits8;
  570. tx_ctrl.par = regk_ser_even;
  571. rx_ctrl.par = regk_ser_even;
  572. tx_ctrl.par_en = regk_ser_no;
  573. rx_ctrl.par_en = regk_ser_no;
  574. tx_ctrl.stop_bits = regk_ser_bits1;
  575. /*
  576. * Change baud-rate and write it to the hardware.
  577. *
  578. * baud_clock = base_freq / (divisor*8)
  579. * divisor = base_freq / (baud_clock * 8)
  580. * base_freq is either:
  581. * off, ext, 29.493MHz, 32.000 MHz, 32.768 MHz or 100 MHz
  582. * 20.493MHz is used for standard baudrates
  583. */
  584. /*
  585. * For the console port we keep the original baudrate here. Not very
  586. * beautiful.
  587. */
  588. if ((port != console_port) || old)
  589. baud = uart_get_baud_rate(port, termios, old, 0,
  590. port->uartclk / 8);
  591. else
  592. baud = console_baud;
  593. tx_baud_div.div = 29493000 / (8 * baud);
  594. /* Rx uses same as tx. */
  595. rx_baud_div.div = tx_baud_div.div;
  596. rx_ctrl.base_freq = tx_ctrl.base_freq;
  597. if ((termios->c_cflag & CSIZE) == CS7) {
  598. /* Set 7 bit mode. */
  599. tx_ctrl.data_bits = regk_ser_bits7;
  600. rx_ctrl.data_bits = regk_ser_bits7;
  601. }
  602. if (termios->c_cflag & CSTOPB) {
  603. /* Set 2 stop bit mode. */
  604. tx_ctrl.stop_bits = regk_ser_bits2;
  605. }
  606. if (termios->c_cflag & PARENB) {
  607. /* Enable parity. */
  608. tx_ctrl.par_en = regk_ser_yes;
  609. rx_ctrl.par_en = regk_ser_yes;
  610. }
  611. if (termios->c_cflag & CMSPAR) {
  612. if (termios->c_cflag & PARODD) {
  613. /* Set mark parity if PARODD and CMSPAR. */
  614. tx_ctrl.par = regk_ser_mark;
  615. rx_ctrl.par = regk_ser_mark;
  616. } else {
  617. tx_ctrl.par = regk_ser_space;
  618. rx_ctrl.par = regk_ser_space;
  619. }
  620. } else {
  621. if (termios->c_cflag & PARODD) {
  622. /* Set odd parity. */
  623. tx_ctrl.par = regk_ser_odd;
  624. rx_ctrl.par = regk_ser_odd;
  625. }
  626. }
  627. if (termios->c_cflag & CRTSCTS) {
  628. /* Enable automatic CTS handling. */
  629. tx_ctrl.auto_cts = regk_ser_yes;
  630. }
  631. /* Make sure the tx and rx are enabled. */
  632. tx_ctrl.en = regk_ser_yes;
  633. rx_ctrl.en = regk_ser_yes;
  634. spin_lock_irqsave(&port->lock, flags);
  635. tx_dma_en.en = 0;
  636. REG_WR(ser, up->regi_ser, rw_tr_dma_en, tx_dma_en);
  637. /* Actually write the control regs (if modified) to the hardware. */
  638. uart_update_timeout(port, termios->c_cflag, port->uartclk/8);
  639. MODIFY_REG(up->regi_ser, rw_rec_baud_div, rx_baud_div);
  640. MODIFY_REG(up->regi_ser, rw_rec_ctrl, rx_ctrl);
  641. MODIFY_REG(up->regi_ser, rw_tr_baud_div, tx_baud_div);
  642. MODIFY_REG(up->regi_ser, rw_tr_ctrl, tx_ctrl);
  643. tx_dma_en.en = 0;
  644. REG_WR(ser, up->regi_ser, rw_tr_dma_en, tx_dma_en);
  645. xoff = REG_RD(ser, up->regi_ser, rw_xoff);
  646. if (up->port.state && up->port.state->port.tty &&
  647. (up->port.state->port.tty->termios.c_iflag & IXON)) {
  648. xoff.chr = STOP_CHAR(up->port.state->port.tty);
  649. xoff.automatic = regk_ser_yes;
  650. } else
  651. xoff.automatic = regk_ser_no;
  652. MODIFY_REG(up->regi_ser, rw_xoff, xoff);
  653. /*
  654. * Make sure we don't start in an automatically shut-off state due to
  655. * a previous early exit.
  656. */
  657. xoff_clr.clr = 1;
  658. REG_WR(ser, up->regi_ser, rw_xoff_clr, xoff_clr);
  659. etraxfs_uart_set_mctrl(&up->port, up->port.mctrl);
  660. spin_unlock_irqrestore(&up->port.lock, flags);
  661. }
  662. static const char *
  663. etraxfs_uart_type(struct uart_port *port)
  664. {
  665. return "CRISv32";
  666. }
  667. static void etraxfs_uart_release_port(struct uart_port *port)
  668. {
  669. }
  670. static int etraxfs_uart_request_port(struct uart_port *port)
  671. {
  672. return 0;
  673. }
  674. static void etraxfs_uart_config_port(struct uart_port *port, int flags)
  675. {
  676. struct uart_cris_port *up = (struct uart_cris_port *)port;
  677. up->port.type = PORT_CRIS;
  678. }
  679. static const struct uart_ops etraxfs_uart_pops = {
  680. .tx_empty = etraxfs_uart_tx_empty,
  681. .set_mctrl = etraxfs_uart_set_mctrl,
  682. .get_mctrl = etraxfs_uart_get_mctrl,
  683. .stop_tx = etraxfs_uart_stop_tx,
  684. .start_tx = etraxfs_uart_start_tx,
  685. .send_xchar = etraxfs_uart_send_xchar,
  686. .stop_rx = etraxfs_uart_stop_rx,
  687. .break_ctl = etraxfs_uart_break_ctl,
  688. .startup = etraxfs_uart_startup,
  689. .shutdown = etraxfs_uart_shutdown,
  690. .set_termios = etraxfs_uart_set_termios,
  691. .type = etraxfs_uart_type,
  692. .release_port = etraxfs_uart_release_port,
  693. .request_port = etraxfs_uart_request_port,
  694. .config_port = etraxfs_uart_config_port,
  695. #ifdef CONFIG_CONSOLE_POLL
  696. .poll_get_char = etraxfs_uart_get_poll_char,
  697. .poll_put_char = etraxfs_uart_put_poll_char,
  698. #endif
  699. };
  700. static void cris_serial_port_init(struct uart_port *port, int line)
  701. {
  702. struct uart_cris_port *up = (struct uart_cris_port *)port;
  703. if (up->initialized)
  704. return;
  705. up->initialized = 1;
  706. port->line = line;
  707. spin_lock_init(&port->lock);
  708. port->ops = &etraxfs_uart_pops;
  709. port->irq = up->irq;
  710. port->iobase = (unsigned long) up->regi_ser;
  711. port->uartclk = 29493000;
  712. /*
  713. * We can't fit any more than 255 here (unsigned char), though
  714. * actually UART_XMIT_SIZE characters could be pending output.
  715. * At time of this writing, the definition of "fifosize" is here the
  716. * amount of characters that can be pending output after a start_tx call
  717. * until tx_empty returns 1: see serial_core.c:uart_wait_until_sent.
  718. * This matters for timeout calculations unfortunately, but keeping
  719. * larger amounts at the DMA wouldn't win much so let's just play nice.
  720. */
  721. port->fifosize = 255;
  722. port->flags = UPF_BOOT_AUTOCONF;
  723. }
  724. static int etraxfs_uart_probe(struct platform_device *pdev)
  725. {
  726. struct device_node *np = pdev->dev.of_node;
  727. struct uart_cris_port *up;
  728. int dev_id;
  729. if (!np)
  730. return -ENODEV;
  731. dev_id = of_alias_get_id(np, "serial");
  732. if (dev_id < 0)
  733. dev_id = 0;
  734. if (dev_id >= UART_NR)
  735. return -EINVAL;
  736. if (etraxfs_uart_ports[dev_id])
  737. return -EBUSY;
  738. up = devm_kzalloc(&pdev->dev, sizeof(struct uart_cris_port),
  739. GFP_KERNEL);
  740. if (!up)
  741. return -ENOMEM;
  742. up->irq = irq_of_parse_and_map(np, 0);
  743. up->regi_ser = of_iomap(np, 0);
  744. up->port.dev = &pdev->dev;
  745. up->gpios = mctrl_gpio_init_noauto(&pdev->dev, 0);
  746. if (IS_ERR(up->gpios))
  747. return PTR_ERR(up->gpios);
  748. cris_serial_port_init(&up->port, dev_id);
  749. etraxfs_uart_ports[dev_id] = up;
  750. platform_set_drvdata(pdev, &up->port);
  751. uart_add_one_port(&etraxfs_uart_driver, &up->port);
  752. return 0;
  753. }
  754. static int etraxfs_uart_remove(struct platform_device *pdev)
  755. {
  756. struct uart_port *port;
  757. port = platform_get_drvdata(pdev);
  758. uart_remove_one_port(&etraxfs_uart_driver, port);
  759. etraxfs_uart_ports[port->line] = NULL;
  760. return 0;
  761. }
  762. static const struct of_device_id etraxfs_uart_dt_ids[] = {
  763. { .compatible = "axis,etraxfs-uart" },
  764. { /* sentinel */ }
  765. };
  766. MODULE_DEVICE_TABLE(of, etraxfs_uart_dt_ids);
  767. static struct platform_driver etraxfs_uart_platform_driver = {
  768. .driver = {
  769. .name = DRV_NAME,
  770. .of_match_table = of_match_ptr(etraxfs_uart_dt_ids),
  771. },
  772. .probe = etraxfs_uart_probe,
  773. .remove = etraxfs_uart_remove,
  774. };
  775. static int __init etraxfs_uart_init(void)
  776. {
  777. int ret;
  778. ret = uart_register_driver(&etraxfs_uart_driver);
  779. if (ret)
  780. return ret;
  781. ret = platform_driver_register(&etraxfs_uart_platform_driver);
  782. if (ret)
  783. uart_unregister_driver(&etraxfs_uart_driver);
  784. return ret;
  785. }
  786. static void __exit etraxfs_uart_exit(void)
  787. {
  788. platform_driver_unregister(&etraxfs_uart_platform_driver);
  789. uart_unregister_driver(&etraxfs_uart_driver);
  790. }
  791. module_init(etraxfs_uart_init);
  792. module_exit(etraxfs_uart_exit);