etraxfs-uart.c 27 KB

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