sccnxp.c 27 KB

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