sccnxp.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029
  1. /*
  2. * NXP (Philips) SCC+++(SCN+++) serial driver
  3. *
  4. * Copyright (C) 2012 Alexander Shiyan <shc_work@mail.ru>
  5. *
  6. * Based on sc26xx.c, by Thomas Bogendörfer (tsbogend@alpha.franken.de)
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. */
  13. #if defined(CONFIG_SERIAL_SCCNXP_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
  14. #define SUPPORT_SYSRQ
  15. #endif
  16. #include <linux/clk.h>
  17. #include <linux/err.h>
  18. #include <linux/module.h>
  19. #include <linux/device.h>
  20. #include <linux/console.h>
  21. #include <linux/serial_core.h>
  22. #include <linux/serial.h>
  23. #include <linux/io.h>
  24. #include <linux/tty.h>
  25. #include <linux/tty_flip.h>
  26. #include <linux/spinlock.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/platform_data/serial-sccnxp.h>
  29. #include <linux/regulator/consumer.h>
  30. #define SCCNXP_NAME "uart-sccnxp"
  31. #define SCCNXP_MAJOR 204
  32. #define SCCNXP_MINOR 205
  33. #define SCCNXP_MR_REG (0x00)
  34. # define MR0_BAUD_NORMAL (0 << 0)
  35. # define MR0_BAUD_EXT1 (1 << 0)
  36. # define MR0_BAUD_EXT2 (5 << 0)
  37. # define MR0_FIFO (1 << 3)
  38. # define MR0_TXLVL (1 << 4)
  39. # define MR1_BITS_5 (0 << 0)
  40. # define MR1_BITS_6 (1 << 0)
  41. # define MR1_BITS_7 (2 << 0)
  42. # define MR1_BITS_8 (3 << 0)
  43. # define MR1_PAR_EVN (0 << 2)
  44. # define MR1_PAR_ODD (1 << 2)
  45. # define MR1_PAR_NO (4 << 2)
  46. # define MR2_STOP1 (7 << 0)
  47. # define MR2_STOP2 (0xf << 0)
  48. #define SCCNXP_SR_REG (0x01)
  49. #define SCCNXP_CSR_REG SCCNXP_SR_REG
  50. # define SR_RXRDY (1 << 0)
  51. # define SR_FULL (1 << 1)
  52. # define SR_TXRDY (1 << 2)
  53. # define SR_TXEMT (1 << 3)
  54. # define SR_OVR (1 << 4)
  55. # define SR_PE (1 << 5)
  56. # define SR_FE (1 << 6)
  57. # define SR_BRK (1 << 7)
  58. #define SCCNXP_CR_REG (0x02)
  59. # define CR_RX_ENABLE (1 << 0)
  60. # define CR_RX_DISABLE (1 << 1)
  61. # define CR_TX_ENABLE (1 << 2)
  62. # define CR_TX_DISABLE (1 << 3)
  63. # define CR_CMD_MRPTR1 (0x01 << 4)
  64. # define CR_CMD_RX_RESET (0x02 << 4)
  65. # define CR_CMD_TX_RESET (0x03 << 4)
  66. # define CR_CMD_STATUS_RESET (0x04 << 4)
  67. # define CR_CMD_BREAK_RESET (0x05 << 4)
  68. # define CR_CMD_START_BREAK (0x06 << 4)
  69. # define CR_CMD_STOP_BREAK (0x07 << 4)
  70. # define CR_CMD_MRPTR0 (0x0b << 4)
  71. #define SCCNXP_RHR_REG (0x03)
  72. #define SCCNXP_THR_REG SCCNXP_RHR_REG
  73. #define SCCNXP_IPCR_REG (0x04)
  74. #define SCCNXP_ACR_REG SCCNXP_IPCR_REG
  75. # define ACR_BAUD0 (0 << 7)
  76. # define ACR_BAUD1 (1 << 7)
  77. # define ACR_TIMER_MODE (6 << 4)
  78. #define SCCNXP_ISR_REG (0x05)
  79. #define SCCNXP_IMR_REG SCCNXP_ISR_REG
  80. # define IMR_TXRDY (1 << 0)
  81. # define IMR_RXRDY (1 << 1)
  82. # define ISR_TXRDY(x) (1 << ((x * 4) + 0))
  83. # define ISR_RXRDY(x) (1 << ((x * 4) + 1))
  84. #define SCCNXP_IPR_REG (0x0d)
  85. #define SCCNXP_OPCR_REG SCCNXP_IPR_REG
  86. #define SCCNXP_SOP_REG (0x0e)
  87. #define SCCNXP_ROP_REG (0x0f)
  88. /* Route helpers */
  89. #define MCTRL_MASK(sig) (0xf << (sig))
  90. #define MCTRL_IBIT(cfg, sig) ((((cfg) >> (sig)) & 0xf) - LINE_IP0)
  91. #define MCTRL_OBIT(cfg, sig) ((((cfg) >> (sig)) & 0xf) - LINE_OP0)
  92. #define SCCNXP_HAVE_IO 0x00000001
  93. #define SCCNXP_HAVE_MR0 0x00000002
  94. struct sccnxp_chip {
  95. const char *name;
  96. unsigned int nr;
  97. unsigned long freq_min;
  98. unsigned long freq_std;
  99. unsigned long freq_max;
  100. unsigned int flags;
  101. unsigned int fifosize;
  102. };
  103. struct sccnxp_port {
  104. struct uart_driver uart;
  105. struct uart_port port[SCCNXP_MAX_UARTS];
  106. bool opened[SCCNXP_MAX_UARTS];
  107. int irq;
  108. u8 imr;
  109. struct sccnxp_chip *chip;
  110. #ifdef CONFIG_SERIAL_SCCNXP_CONSOLE
  111. struct console console;
  112. #endif
  113. spinlock_t lock;
  114. bool poll;
  115. struct timer_list timer;
  116. struct sccnxp_pdata pdata;
  117. struct regulator *regulator;
  118. };
  119. static const struct sccnxp_chip sc2681 = {
  120. .name = "SC2681",
  121. .nr = 2,
  122. .freq_min = 1000000,
  123. .freq_std = 3686400,
  124. .freq_max = 4000000,
  125. .flags = SCCNXP_HAVE_IO,
  126. .fifosize = 3,
  127. };
  128. static const struct sccnxp_chip sc2691 = {
  129. .name = "SC2691",
  130. .nr = 1,
  131. .freq_min = 1000000,
  132. .freq_std = 3686400,
  133. .freq_max = 4000000,
  134. .flags = 0,
  135. .fifosize = 3,
  136. };
  137. static const struct sccnxp_chip sc2692 = {
  138. .name = "SC2692",
  139. .nr = 2,
  140. .freq_min = 1000000,
  141. .freq_std = 3686400,
  142. .freq_max = 4000000,
  143. .flags = SCCNXP_HAVE_IO,
  144. .fifosize = 3,
  145. };
  146. static const struct sccnxp_chip sc2891 = {
  147. .name = "SC2891",
  148. .nr = 1,
  149. .freq_min = 100000,
  150. .freq_std = 3686400,
  151. .freq_max = 8000000,
  152. .flags = SCCNXP_HAVE_IO | SCCNXP_HAVE_MR0,
  153. .fifosize = 16,
  154. };
  155. static const struct sccnxp_chip sc2892 = {
  156. .name = "SC2892",
  157. .nr = 2,
  158. .freq_min = 100000,
  159. .freq_std = 3686400,
  160. .freq_max = 8000000,
  161. .flags = SCCNXP_HAVE_IO | SCCNXP_HAVE_MR0,
  162. .fifosize = 16,
  163. };
  164. static const struct sccnxp_chip sc28202 = {
  165. .name = "SC28202",
  166. .nr = 2,
  167. .freq_min = 1000000,
  168. .freq_std = 14745600,
  169. .freq_max = 50000000,
  170. .flags = SCCNXP_HAVE_IO | SCCNXP_HAVE_MR0,
  171. .fifosize = 256,
  172. };
  173. static const struct sccnxp_chip sc68681 = {
  174. .name = "SC68681",
  175. .nr = 2,
  176. .freq_min = 1000000,
  177. .freq_std = 3686400,
  178. .freq_max = 4000000,
  179. .flags = SCCNXP_HAVE_IO,
  180. .fifosize = 3,
  181. };
  182. static const struct sccnxp_chip sc68692 = {
  183. .name = "SC68692",
  184. .nr = 2,
  185. .freq_min = 1000000,
  186. .freq_std = 3686400,
  187. .freq_max = 4000000,
  188. .flags = SCCNXP_HAVE_IO,
  189. .fifosize = 3,
  190. };
  191. static inline u8 sccnxp_read(struct uart_port *port, u8 reg)
  192. {
  193. return readb(port->membase + (reg << port->regshift));
  194. }
  195. static inline void sccnxp_write(struct uart_port *port, u8 reg, u8 v)
  196. {
  197. writeb(v, port->membase + (reg << port->regshift));
  198. }
  199. static inline u8 sccnxp_port_read(struct uart_port *port, u8 reg)
  200. {
  201. return sccnxp_read(port, (port->line << 3) + reg);
  202. }
  203. static inline void sccnxp_port_write(struct uart_port *port, u8 reg, u8 v)
  204. {
  205. sccnxp_write(port, (port->line << 3) + reg, v);
  206. }
  207. static int sccnxp_update_best_err(int a, int b, int *besterr)
  208. {
  209. int err = abs(a - b);
  210. if ((*besterr < 0) || (*besterr > err)) {
  211. *besterr = err;
  212. return 0;
  213. }
  214. return 1;
  215. }
  216. static const struct {
  217. u8 csr;
  218. u8 acr;
  219. u8 mr0;
  220. int baud;
  221. } baud_std[] = {
  222. { 0, ACR_BAUD0, MR0_BAUD_NORMAL, 50, },
  223. { 0, ACR_BAUD1, MR0_BAUD_NORMAL, 75, },
  224. { 1, ACR_BAUD0, MR0_BAUD_NORMAL, 110, },
  225. { 2, ACR_BAUD0, MR0_BAUD_NORMAL, 134, },
  226. { 3, ACR_BAUD1, MR0_BAUD_NORMAL, 150, },
  227. { 3, ACR_BAUD0, MR0_BAUD_NORMAL, 200, },
  228. { 4, ACR_BAUD0, MR0_BAUD_NORMAL, 300, },
  229. { 0, ACR_BAUD1, MR0_BAUD_EXT1, 450, },
  230. { 1, ACR_BAUD0, MR0_BAUD_EXT2, 880, },
  231. { 3, ACR_BAUD1, MR0_BAUD_EXT1, 900, },
  232. { 5, ACR_BAUD0, MR0_BAUD_NORMAL, 600, },
  233. { 7, ACR_BAUD0, MR0_BAUD_NORMAL, 1050, },
  234. { 2, ACR_BAUD0, MR0_BAUD_EXT2, 1076, },
  235. { 6, ACR_BAUD0, MR0_BAUD_NORMAL, 1200, },
  236. { 10, ACR_BAUD1, MR0_BAUD_NORMAL, 1800, },
  237. { 7, ACR_BAUD1, MR0_BAUD_NORMAL, 2000, },
  238. { 8, ACR_BAUD0, MR0_BAUD_NORMAL, 2400, },
  239. { 5, ACR_BAUD1, MR0_BAUD_EXT1, 3600, },
  240. { 9, ACR_BAUD0, MR0_BAUD_NORMAL, 4800, },
  241. { 10, ACR_BAUD0, MR0_BAUD_NORMAL, 7200, },
  242. { 11, ACR_BAUD0, MR0_BAUD_NORMAL, 9600, },
  243. { 8, ACR_BAUD0, MR0_BAUD_EXT1, 14400, },
  244. { 12, ACR_BAUD1, MR0_BAUD_NORMAL, 19200, },
  245. { 9, ACR_BAUD0, MR0_BAUD_EXT1, 28800, },
  246. { 12, ACR_BAUD0, MR0_BAUD_NORMAL, 38400, },
  247. { 11, ACR_BAUD0, MR0_BAUD_EXT1, 57600, },
  248. { 12, ACR_BAUD1, MR0_BAUD_EXT1, 115200, },
  249. { 12, ACR_BAUD0, MR0_BAUD_EXT1, 230400, },
  250. { 0, 0, 0, 0 }
  251. };
  252. static int sccnxp_set_baud(struct uart_port *port, int baud)
  253. {
  254. struct sccnxp_port *s = dev_get_drvdata(port->dev);
  255. int div_std, tmp_baud, bestbaud = baud, besterr = -1;
  256. struct sccnxp_chip *chip = s->chip;
  257. u8 i, acr = 0, csr = 0, mr0 = 0;
  258. /* Find best baud from table */
  259. for (i = 0; baud_std[i].baud && besterr; i++) {
  260. if (baud_std[i].mr0 && !(chip->flags & SCCNXP_HAVE_MR0))
  261. continue;
  262. div_std = DIV_ROUND_CLOSEST(chip->freq_std, baud_std[i].baud);
  263. tmp_baud = DIV_ROUND_CLOSEST(port->uartclk, div_std);
  264. if (!sccnxp_update_best_err(baud, tmp_baud, &besterr)) {
  265. acr = baud_std[i].acr;
  266. csr = baud_std[i].csr;
  267. mr0 = baud_std[i].mr0;
  268. bestbaud = tmp_baud;
  269. }
  270. }
  271. if (chip->flags & SCCNXP_HAVE_MR0) {
  272. /* Enable FIFO, set half level for TX */
  273. mr0 |= MR0_FIFO | MR0_TXLVL;
  274. /* Update MR0 */
  275. sccnxp_port_write(port, SCCNXP_CR_REG, CR_CMD_MRPTR0);
  276. sccnxp_port_write(port, SCCNXP_MR_REG, mr0);
  277. }
  278. sccnxp_port_write(port, SCCNXP_ACR_REG, acr | ACR_TIMER_MODE);
  279. sccnxp_port_write(port, SCCNXP_CSR_REG, (csr << 4) | csr);
  280. if (baud != bestbaud)
  281. dev_dbg(port->dev, "Baudrate desired: %i, calculated: %i\n",
  282. baud, bestbaud);
  283. return bestbaud;
  284. }
  285. static void sccnxp_enable_irq(struct uart_port *port, int mask)
  286. {
  287. struct sccnxp_port *s = dev_get_drvdata(port->dev);
  288. s->imr |= mask << (port->line * 4);
  289. sccnxp_write(port, SCCNXP_IMR_REG, s->imr);
  290. }
  291. static void sccnxp_disable_irq(struct uart_port *port, int mask)
  292. {
  293. struct sccnxp_port *s = dev_get_drvdata(port->dev);
  294. s->imr &= ~(mask << (port->line * 4));
  295. sccnxp_write(port, SCCNXP_IMR_REG, s->imr);
  296. }
  297. static void sccnxp_set_bit(struct uart_port *port, int sig, int state)
  298. {
  299. u8 bitmask;
  300. struct sccnxp_port *s = dev_get_drvdata(port->dev);
  301. if (s->pdata.mctrl_cfg[port->line] & MCTRL_MASK(sig)) {
  302. bitmask = 1 << MCTRL_OBIT(s->pdata.mctrl_cfg[port->line], sig);
  303. if (state)
  304. sccnxp_write(port, SCCNXP_SOP_REG, bitmask);
  305. else
  306. sccnxp_write(port, SCCNXP_ROP_REG, bitmask);
  307. }
  308. }
  309. static void sccnxp_handle_rx(struct uart_port *port)
  310. {
  311. u8 sr;
  312. unsigned int ch, flag;
  313. for (;;) {
  314. sr = sccnxp_port_read(port, SCCNXP_SR_REG);
  315. if (!(sr & SR_RXRDY))
  316. break;
  317. sr &= SR_PE | SR_FE | SR_OVR | SR_BRK;
  318. ch = sccnxp_port_read(port, SCCNXP_RHR_REG);
  319. port->icount.rx++;
  320. flag = TTY_NORMAL;
  321. if (unlikely(sr)) {
  322. if (sr & SR_BRK) {
  323. port->icount.brk++;
  324. sccnxp_port_write(port, SCCNXP_CR_REG,
  325. CR_CMD_BREAK_RESET);
  326. if (uart_handle_break(port))
  327. continue;
  328. } else if (sr & SR_PE)
  329. port->icount.parity++;
  330. else if (sr & SR_FE)
  331. port->icount.frame++;
  332. else if (sr & SR_OVR) {
  333. port->icount.overrun++;
  334. sccnxp_port_write(port, SCCNXP_CR_REG,
  335. CR_CMD_STATUS_RESET);
  336. }
  337. sr &= port->read_status_mask;
  338. if (sr & SR_BRK)
  339. flag = TTY_BREAK;
  340. else if (sr & SR_PE)
  341. flag = TTY_PARITY;
  342. else if (sr & SR_FE)
  343. flag = TTY_FRAME;
  344. else if (sr & SR_OVR)
  345. flag = TTY_OVERRUN;
  346. }
  347. if (uart_handle_sysrq_char(port, ch))
  348. continue;
  349. if (sr & port->ignore_status_mask)
  350. continue;
  351. uart_insert_char(port, sr, SR_OVR, ch, flag);
  352. }
  353. tty_flip_buffer_push(&port->state->port);
  354. }
  355. static void sccnxp_handle_tx(struct uart_port *port)
  356. {
  357. u8 sr;
  358. struct circ_buf *xmit = &port->state->xmit;
  359. struct sccnxp_port *s = dev_get_drvdata(port->dev);
  360. if (unlikely(port->x_char)) {
  361. sccnxp_port_write(port, SCCNXP_THR_REG, port->x_char);
  362. port->icount.tx++;
  363. port->x_char = 0;
  364. return;
  365. }
  366. if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
  367. /* Disable TX if FIFO is empty */
  368. if (sccnxp_port_read(port, SCCNXP_SR_REG) & SR_TXEMT) {
  369. sccnxp_disable_irq(port, IMR_TXRDY);
  370. /* Set direction to input */
  371. if (s->chip->flags & SCCNXP_HAVE_IO)
  372. sccnxp_set_bit(port, DIR_OP, 0);
  373. }
  374. return;
  375. }
  376. while (!uart_circ_empty(xmit)) {
  377. sr = sccnxp_port_read(port, SCCNXP_SR_REG);
  378. if (!(sr & SR_TXRDY))
  379. break;
  380. sccnxp_port_write(port, SCCNXP_THR_REG, xmit->buf[xmit->tail]);
  381. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  382. port->icount.tx++;
  383. }
  384. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  385. uart_write_wakeup(port);
  386. }
  387. static void sccnxp_handle_events(struct sccnxp_port *s)
  388. {
  389. int i;
  390. u8 isr;
  391. do {
  392. isr = sccnxp_read(&s->port[0], SCCNXP_ISR_REG);
  393. isr &= s->imr;
  394. if (!isr)
  395. break;
  396. for (i = 0; i < s->uart.nr; i++) {
  397. if (s->opened[i] && (isr & ISR_RXRDY(i)))
  398. sccnxp_handle_rx(&s->port[i]);
  399. if (s->opened[i] && (isr & ISR_TXRDY(i)))
  400. sccnxp_handle_tx(&s->port[i]);
  401. }
  402. } while (1);
  403. }
  404. static void sccnxp_timer(unsigned long data)
  405. {
  406. struct sccnxp_port *s = (struct sccnxp_port *)data;
  407. unsigned long flags;
  408. spin_lock_irqsave(&s->lock, flags);
  409. sccnxp_handle_events(s);
  410. spin_unlock_irqrestore(&s->lock, flags);
  411. mod_timer(&s->timer, jiffies + usecs_to_jiffies(s->pdata.poll_time_us));
  412. }
  413. static irqreturn_t sccnxp_ist(int irq, void *dev_id)
  414. {
  415. struct sccnxp_port *s = (struct sccnxp_port *)dev_id;
  416. unsigned long flags;
  417. spin_lock_irqsave(&s->lock, flags);
  418. sccnxp_handle_events(s);
  419. spin_unlock_irqrestore(&s->lock, flags);
  420. return IRQ_HANDLED;
  421. }
  422. static void sccnxp_start_tx(struct uart_port *port)
  423. {
  424. struct sccnxp_port *s = dev_get_drvdata(port->dev);
  425. unsigned long flags;
  426. spin_lock_irqsave(&s->lock, flags);
  427. /* Set direction to output */
  428. if (s->chip->flags & SCCNXP_HAVE_IO)
  429. sccnxp_set_bit(port, DIR_OP, 1);
  430. sccnxp_enable_irq(port, IMR_TXRDY);
  431. spin_unlock_irqrestore(&s->lock, flags);
  432. }
  433. static void sccnxp_stop_tx(struct uart_port *port)
  434. {
  435. /* Do nothing */
  436. }
  437. static void sccnxp_stop_rx(struct uart_port *port)
  438. {
  439. struct sccnxp_port *s = dev_get_drvdata(port->dev);
  440. unsigned long flags;
  441. spin_lock_irqsave(&s->lock, flags);
  442. sccnxp_port_write(port, SCCNXP_CR_REG, CR_RX_DISABLE);
  443. spin_unlock_irqrestore(&s->lock, flags);
  444. }
  445. static unsigned int sccnxp_tx_empty(struct uart_port *port)
  446. {
  447. u8 val;
  448. unsigned long flags;
  449. struct sccnxp_port *s = dev_get_drvdata(port->dev);
  450. spin_lock_irqsave(&s->lock, flags);
  451. val = sccnxp_port_read(port, SCCNXP_SR_REG);
  452. spin_unlock_irqrestore(&s->lock, flags);
  453. return (val & SR_TXEMT) ? TIOCSER_TEMT : 0;
  454. }
  455. static void sccnxp_set_mctrl(struct uart_port *port, unsigned int mctrl)
  456. {
  457. struct sccnxp_port *s = dev_get_drvdata(port->dev);
  458. unsigned long flags;
  459. if (!(s->chip->flags & SCCNXP_HAVE_IO))
  460. return;
  461. spin_lock_irqsave(&s->lock, flags);
  462. sccnxp_set_bit(port, DTR_OP, mctrl & TIOCM_DTR);
  463. sccnxp_set_bit(port, RTS_OP, mctrl & TIOCM_RTS);
  464. spin_unlock_irqrestore(&s->lock, flags);
  465. }
  466. static unsigned int sccnxp_get_mctrl(struct uart_port *port)
  467. {
  468. u8 bitmask, ipr;
  469. unsigned long flags;
  470. struct sccnxp_port *s = dev_get_drvdata(port->dev);
  471. unsigned int mctrl = TIOCM_DSR | TIOCM_CTS | TIOCM_CAR;
  472. if (!(s->chip->flags & SCCNXP_HAVE_IO))
  473. return mctrl;
  474. spin_lock_irqsave(&s->lock, flags);
  475. ipr = ~sccnxp_read(port, SCCNXP_IPCR_REG);
  476. if (s->pdata.mctrl_cfg[port->line] & MCTRL_MASK(DSR_IP)) {
  477. bitmask = 1 << MCTRL_IBIT(s->pdata.mctrl_cfg[port->line],
  478. DSR_IP);
  479. mctrl &= ~TIOCM_DSR;
  480. mctrl |= (ipr & bitmask) ? TIOCM_DSR : 0;
  481. }
  482. if (s->pdata.mctrl_cfg[port->line] & MCTRL_MASK(CTS_IP)) {
  483. bitmask = 1 << MCTRL_IBIT(s->pdata.mctrl_cfg[port->line],
  484. CTS_IP);
  485. mctrl &= ~TIOCM_CTS;
  486. mctrl |= (ipr & bitmask) ? TIOCM_CTS : 0;
  487. }
  488. if (s->pdata.mctrl_cfg[port->line] & MCTRL_MASK(DCD_IP)) {
  489. bitmask = 1 << MCTRL_IBIT(s->pdata.mctrl_cfg[port->line],
  490. DCD_IP);
  491. mctrl &= ~TIOCM_CAR;
  492. mctrl |= (ipr & bitmask) ? TIOCM_CAR : 0;
  493. }
  494. if (s->pdata.mctrl_cfg[port->line] & MCTRL_MASK(RNG_IP)) {
  495. bitmask = 1 << MCTRL_IBIT(s->pdata.mctrl_cfg[port->line],
  496. RNG_IP);
  497. mctrl &= ~TIOCM_RNG;
  498. mctrl |= (ipr & bitmask) ? TIOCM_RNG : 0;
  499. }
  500. spin_unlock_irqrestore(&s->lock, flags);
  501. return mctrl;
  502. }
  503. static void sccnxp_break_ctl(struct uart_port *port, int break_state)
  504. {
  505. struct sccnxp_port *s = dev_get_drvdata(port->dev);
  506. unsigned long flags;
  507. spin_lock_irqsave(&s->lock, flags);
  508. sccnxp_port_write(port, SCCNXP_CR_REG, break_state ?
  509. CR_CMD_START_BREAK : CR_CMD_STOP_BREAK);
  510. spin_unlock_irqrestore(&s->lock, flags);
  511. }
  512. static void sccnxp_set_termios(struct uart_port *port,
  513. struct ktermios *termios, struct ktermios *old)
  514. {
  515. struct sccnxp_port *s = dev_get_drvdata(port->dev);
  516. unsigned long flags;
  517. u8 mr1, mr2;
  518. int baud;
  519. spin_lock_irqsave(&s->lock, flags);
  520. /* Mask termios capabilities we don't support */
  521. termios->c_cflag &= ~CMSPAR;
  522. /* Disable RX & TX, reset break condition, status and FIFOs */
  523. sccnxp_port_write(port, SCCNXP_CR_REG, CR_CMD_RX_RESET |
  524. CR_RX_DISABLE | CR_TX_DISABLE);
  525. sccnxp_port_write(port, SCCNXP_CR_REG, CR_CMD_TX_RESET);
  526. sccnxp_port_write(port, SCCNXP_CR_REG, CR_CMD_STATUS_RESET);
  527. sccnxp_port_write(port, SCCNXP_CR_REG, CR_CMD_BREAK_RESET);
  528. /* Word size */
  529. switch (termios->c_cflag & CSIZE) {
  530. case CS5:
  531. mr1 = MR1_BITS_5;
  532. break;
  533. case CS6:
  534. mr1 = MR1_BITS_6;
  535. break;
  536. case CS7:
  537. mr1 = MR1_BITS_7;
  538. break;
  539. case CS8:
  540. default:
  541. mr1 = MR1_BITS_8;
  542. break;
  543. }
  544. /* Parity */
  545. if (termios->c_cflag & PARENB) {
  546. if (termios->c_cflag & PARODD)
  547. mr1 |= MR1_PAR_ODD;
  548. } else
  549. mr1 |= MR1_PAR_NO;
  550. /* Stop bits */
  551. mr2 = (termios->c_cflag & CSTOPB) ? MR2_STOP2 : MR2_STOP1;
  552. /* Update desired format */
  553. sccnxp_port_write(port, SCCNXP_CR_REG, CR_CMD_MRPTR1);
  554. sccnxp_port_write(port, SCCNXP_MR_REG, mr1);
  555. sccnxp_port_write(port, SCCNXP_MR_REG, mr2);
  556. /* Set read status mask */
  557. port->read_status_mask = SR_OVR;
  558. if (termios->c_iflag & INPCK)
  559. port->read_status_mask |= SR_PE | SR_FE;
  560. if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
  561. port->read_status_mask |= SR_BRK;
  562. /* Set status ignore mask */
  563. port->ignore_status_mask = 0;
  564. if (termios->c_iflag & IGNBRK)
  565. port->ignore_status_mask |= SR_BRK;
  566. if (termios->c_iflag & IGNPAR)
  567. port->ignore_status_mask |= SR_PE;
  568. if (!(termios->c_cflag & CREAD))
  569. port->ignore_status_mask |= SR_PE | SR_OVR | SR_FE | SR_BRK;
  570. /* Setup baudrate */
  571. baud = uart_get_baud_rate(port, termios, old, 50,
  572. (s->chip->flags & SCCNXP_HAVE_MR0) ?
  573. 230400 : 38400);
  574. baud = sccnxp_set_baud(port, baud);
  575. /* Update timeout according to new baud rate */
  576. uart_update_timeout(port, termios->c_cflag, baud);
  577. /* Report actual baudrate back to core */
  578. if (tty_termios_baud_rate(termios))
  579. tty_termios_encode_baud_rate(termios, baud, baud);
  580. /* Enable RX & TX */
  581. sccnxp_port_write(port, SCCNXP_CR_REG, CR_RX_ENABLE | CR_TX_ENABLE);
  582. spin_unlock_irqrestore(&s->lock, flags);
  583. }
  584. static int sccnxp_startup(struct uart_port *port)
  585. {
  586. struct sccnxp_port *s = dev_get_drvdata(port->dev);
  587. unsigned long flags;
  588. spin_lock_irqsave(&s->lock, flags);
  589. if (s->chip->flags & SCCNXP_HAVE_IO) {
  590. /* Outputs are controlled manually */
  591. sccnxp_write(port, SCCNXP_OPCR_REG, 0);
  592. }
  593. /* Reset break condition, status and FIFOs */
  594. sccnxp_port_write(port, SCCNXP_CR_REG, CR_CMD_RX_RESET);
  595. sccnxp_port_write(port, SCCNXP_CR_REG, CR_CMD_TX_RESET);
  596. sccnxp_port_write(port, SCCNXP_CR_REG, CR_CMD_STATUS_RESET);
  597. sccnxp_port_write(port, SCCNXP_CR_REG, CR_CMD_BREAK_RESET);
  598. /* Enable RX & TX */
  599. sccnxp_port_write(port, SCCNXP_CR_REG, CR_RX_ENABLE | CR_TX_ENABLE);
  600. /* Enable RX interrupt */
  601. sccnxp_enable_irq(port, IMR_RXRDY);
  602. s->opened[port->line] = 1;
  603. spin_unlock_irqrestore(&s->lock, flags);
  604. return 0;
  605. }
  606. static void sccnxp_shutdown(struct uart_port *port)
  607. {
  608. struct sccnxp_port *s = dev_get_drvdata(port->dev);
  609. unsigned long flags;
  610. spin_lock_irqsave(&s->lock, flags);
  611. s->opened[port->line] = 0;
  612. /* Disable interrupts */
  613. sccnxp_disable_irq(port, IMR_TXRDY | IMR_RXRDY);
  614. /* Disable TX & RX */
  615. sccnxp_port_write(port, SCCNXP_CR_REG, CR_RX_DISABLE | CR_TX_DISABLE);
  616. /* Leave direction to input */
  617. if (s->chip->flags & SCCNXP_HAVE_IO)
  618. sccnxp_set_bit(port, DIR_OP, 0);
  619. spin_unlock_irqrestore(&s->lock, flags);
  620. }
  621. static const char *sccnxp_type(struct uart_port *port)
  622. {
  623. struct sccnxp_port *s = dev_get_drvdata(port->dev);
  624. return (port->type == PORT_SC26XX) ? s->chip->name : NULL;
  625. }
  626. static void sccnxp_release_port(struct uart_port *port)
  627. {
  628. /* Do nothing */
  629. }
  630. static int sccnxp_request_port(struct uart_port *port)
  631. {
  632. /* Do nothing */
  633. return 0;
  634. }
  635. static void sccnxp_config_port(struct uart_port *port, int flags)
  636. {
  637. if (flags & UART_CONFIG_TYPE)
  638. port->type = PORT_SC26XX;
  639. }
  640. static int sccnxp_verify_port(struct uart_port *port, struct serial_struct *s)
  641. {
  642. if ((s->type == PORT_UNKNOWN) || (s->type == PORT_SC26XX))
  643. return 0;
  644. if (s->irq == port->irq)
  645. return 0;
  646. return -EINVAL;
  647. }
  648. static const struct uart_ops sccnxp_ops = {
  649. .tx_empty = sccnxp_tx_empty,
  650. .set_mctrl = sccnxp_set_mctrl,
  651. .get_mctrl = sccnxp_get_mctrl,
  652. .stop_tx = sccnxp_stop_tx,
  653. .start_tx = sccnxp_start_tx,
  654. .stop_rx = sccnxp_stop_rx,
  655. .break_ctl = sccnxp_break_ctl,
  656. .startup = sccnxp_startup,
  657. .shutdown = sccnxp_shutdown,
  658. .set_termios = sccnxp_set_termios,
  659. .type = sccnxp_type,
  660. .release_port = sccnxp_release_port,
  661. .request_port = sccnxp_request_port,
  662. .config_port = sccnxp_config_port,
  663. .verify_port = sccnxp_verify_port,
  664. };
  665. #ifdef CONFIG_SERIAL_SCCNXP_CONSOLE
  666. static void sccnxp_console_putchar(struct uart_port *port, int c)
  667. {
  668. int tryes = 100000;
  669. while (tryes--) {
  670. if (sccnxp_port_read(port, SCCNXP_SR_REG) & SR_TXRDY) {
  671. sccnxp_port_write(port, SCCNXP_THR_REG, c);
  672. break;
  673. }
  674. barrier();
  675. }
  676. }
  677. static void sccnxp_console_write(struct console *co, const char *c, unsigned n)
  678. {
  679. struct sccnxp_port *s = (struct sccnxp_port *)co->data;
  680. struct uart_port *port = &s->port[co->index];
  681. unsigned long flags;
  682. spin_lock_irqsave(&s->lock, flags);
  683. uart_console_write(port, c, n, sccnxp_console_putchar);
  684. spin_unlock_irqrestore(&s->lock, flags);
  685. }
  686. static int sccnxp_console_setup(struct console *co, char *options)
  687. {
  688. struct sccnxp_port *s = (struct sccnxp_port *)co->data;
  689. struct uart_port *port = &s->port[(co->index > 0) ? co->index : 0];
  690. int baud = 9600, bits = 8, parity = 'n', flow = 'n';
  691. if (options)
  692. uart_parse_options(options, &baud, &parity, &bits, &flow);
  693. return uart_set_options(port, co, baud, parity, bits, flow);
  694. }
  695. #endif
  696. static const struct platform_device_id sccnxp_id_table[] = {
  697. { .name = "sc2681", .driver_data = (kernel_ulong_t)&sc2681, },
  698. { .name = "sc2691", .driver_data = (kernel_ulong_t)&sc2691, },
  699. { .name = "sc2692", .driver_data = (kernel_ulong_t)&sc2692, },
  700. { .name = "sc2891", .driver_data = (kernel_ulong_t)&sc2891, },
  701. { .name = "sc2892", .driver_data = (kernel_ulong_t)&sc2892, },
  702. { .name = "sc28202", .driver_data = (kernel_ulong_t)&sc28202, },
  703. { .name = "sc68681", .driver_data = (kernel_ulong_t)&sc68681, },
  704. { .name = "sc68692", .driver_data = (kernel_ulong_t)&sc68692, },
  705. { }
  706. };
  707. MODULE_DEVICE_TABLE(platform, sccnxp_id_table);
  708. static int sccnxp_probe(struct platform_device *pdev)
  709. {
  710. struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  711. struct sccnxp_pdata *pdata = dev_get_platdata(&pdev->dev);
  712. int i, ret, uartclk;
  713. struct sccnxp_port *s;
  714. void __iomem *membase;
  715. struct clk *clk;
  716. membase = devm_ioremap_resource(&pdev->dev, res);
  717. if (IS_ERR(membase))
  718. return PTR_ERR(membase);
  719. s = devm_kzalloc(&pdev->dev, sizeof(struct sccnxp_port), GFP_KERNEL);
  720. if (!s) {
  721. dev_err(&pdev->dev, "Error allocating port structure\n");
  722. return -ENOMEM;
  723. }
  724. platform_set_drvdata(pdev, s);
  725. spin_lock_init(&s->lock);
  726. s->chip = (struct sccnxp_chip *)pdev->id_entry->driver_data;
  727. s->regulator = devm_regulator_get(&pdev->dev, "vcc");
  728. if (!IS_ERR(s->regulator)) {
  729. ret = regulator_enable(s->regulator);
  730. if (ret) {
  731. dev_err(&pdev->dev,
  732. "Failed to enable regulator: %i\n", ret);
  733. return ret;
  734. }
  735. } else if (PTR_ERR(s->regulator) == -EPROBE_DEFER)
  736. return -EPROBE_DEFER;
  737. clk = devm_clk_get(&pdev->dev, NULL);
  738. if (IS_ERR(clk)) {
  739. ret = PTR_ERR(clk);
  740. if (ret == -EPROBE_DEFER)
  741. goto err_out;
  742. uartclk = 0;
  743. } else {
  744. clk_prepare_enable(clk);
  745. uartclk = clk_get_rate(clk);
  746. }
  747. if (!uartclk) {
  748. dev_notice(&pdev->dev, "Using default clock frequency\n");
  749. uartclk = s->chip->freq_std;
  750. }
  751. /* Check input frequency */
  752. if ((uartclk < s->chip->freq_min) || (uartclk > s->chip->freq_max)) {
  753. dev_err(&pdev->dev, "Frequency out of bounds\n");
  754. ret = -EINVAL;
  755. goto err_out;
  756. }
  757. if (pdata)
  758. memcpy(&s->pdata, pdata, sizeof(struct sccnxp_pdata));
  759. if (s->pdata.poll_time_us) {
  760. dev_info(&pdev->dev, "Using poll mode, resolution %u usecs\n",
  761. s->pdata.poll_time_us);
  762. s->poll = 1;
  763. }
  764. if (!s->poll) {
  765. s->irq = platform_get_irq(pdev, 0);
  766. if (s->irq < 0) {
  767. dev_err(&pdev->dev, "Missing irq resource data\n");
  768. ret = -ENXIO;
  769. goto err_out;
  770. }
  771. }
  772. s->uart.owner = THIS_MODULE;
  773. s->uart.dev_name = "ttySC";
  774. s->uart.major = SCCNXP_MAJOR;
  775. s->uart.minor = SCCNXP_MINOR;
  776. s->uart.nr = s->chip->nr;
  777. #ifdef CONFIG_SERIAL_SCCNXP_CONSOLE
  778. s->uart.cons = &s->console;
  779. s->uart.cons->device = uart_console_device;
  780. s->uart.cons->write = sccnxp_console_write;
  781. s->uart.cons->setup = sccnxp_console_setup;
  782. s->uart.cons->flags = CON_PRINTBUFFER;
  783. s->uart.cons->index = -1;
  784. s->uart.cons->data = s;
  785. strcpy(s->uart.cons->name, "ttySC");
  786. #endif
  787. ret = uart_register_driver(&s->uart);
  788. if (ret) {
  789. dev_err(&pdev->dev, "Registering UART driver failed\n");
  790. goto err_out;
  791. }
  792. for (i = 0; i < s->uart.nr; i++) {
  793. s->port[i].line = i;
  794. s->port[i].dev = &pdev->dev;
  795. s->port[i].irq = s->irq;
  796. s->port[i].type = PORT_SC26XX;
  797. s->port[i].fifosize = s->chip->fifosize;
  798. s->port[i].flags = UPF_SKIP_TEST | UPF_FIXED_TYPE;
  799. s->port[i].iotype = UPIO_MEM;
  800. s->port[i].mapbase = res->start;
  801. s->port[i].membase = membase;
  802. s->port[i].regshift = s->pdata.reg_shift;
  803. s->port[i].uartclk = uartclk;
  804. s->port[i].ops = &sccnxp_ops;
  805. uart_add_one_port(&s->uart, &s->port[i]);
  806. /* Set direction to input */
  807. if (s->chip->flags & SCCNXP_HAVE_IO)
  808. sccnxp_set_bit(&s->port[i], DIR_OP, 0);
  809. }
  810. /* Disable interrupts */
  811. s->imr = 0;
  812. sccnxp_write(&s->port[0], SCCNXP_IMR_REG, 0);
  813. if (!s->poll) {
  814. ret = devm_request_threaded_irq(&pdev->dev, s->irq, NULL,
  815. sccnxp_ist,
  816. IRQF_TRIGGER_FALLING |
  817. IRQF_ONESHOT,
  818. dev_name(&pdev->dev), s);
  819. if (!ret)
  820. return 0;
  821. dev_err(&pdev->dev, "Unable to reguest IRQ %i\n", s->irq);
  822. } else {
  823. init_timer(&s->timer);
  824. setup_timer(&s->timer, sccnxp_timer, (unsigned long)s);
  825. mod_timer(&s->timer, jiffies +
  826. usecs_to_jiffies(s->pdata.poll_time_us));
  827. return 0;
  828. }
  829. uart_unregister_driver(&s->uart);
  830. err_out:
  831. if (!IS_ERR(s->regulator))
  832. return regulator_disable(s->regulator);
  833. return ret;
  834. }
  835. static int sccnxp_remove(struct platform_device *pdev)
  836. {
  837. int i;
  838. struct sccnxp_port *s = platform_get_drvdata(pdev);
  839. if (!s->poll)
  840. devm_free_irq(&pdev->dev, s->irq, s);
  841. else
  842. del_timer_sync(&s->timer);
  843. for (i = 0; i < s->uart.nr; i++)
  844. uart_remove_one_port(&s->uart, &s->port[i]);
  845. uart_unregister_driver(&s->uart);
  846. if (!IS_ERR(s->regulator))
  847. return regulator_disable(s->regulator);
  848. return 0;
  849. }
  850. static struct platform_driver sccnxp_uart_driver = {
  851. .driver = {
  852. .name = SCCNXP_NAME,
  853. },
  854. .probe = sccnxp_probe,
  855. .remove = sccnxp_remove,
  856. .id_table = sccnxp_id_table,
  857. };
  858. module_platform_driver(sccnxp_uart_driver);
  859. MODULE_LICENSE("GPL v2");
  860. MODULE_AUTHOR("Alexander Shiyan <shc_work@mail.ru>");
  861. MODULE_DESCRIPTION("SCCNXP serial driver");