sccnxp.c 27 KB

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