sc16is7xx.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470
  1. /*
  2. * SC16IS7xx tty serial driver - Copyright (C) 2014 GridPoint
  3. * Author: Jon Ringle <jringle@gridpoint.com>
  4. *
  5. * Based on max310x.c, by Alexander Shiyan <shc_work@mail.ru>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. */
  13. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  14. #include <linux/bitops.h>
  15. #include <linux/clk.h>
  16. #include <linux/delay.h>
  17. #include <linux/device.h>
  18. #include <linux/gpio.h>
  19. #include <linux/i2c.h>
  20. #include <linux/module.h>
  21. #include <linux/of.h>
  22. #include <linux/of_device.h>
  23. #include <linux/regmap.h>
  24. #include <linux/serial_core.h>
  25. #include <linux/serial.h>
  26. #include <linux/tty.h>
  27. #include <linux/tty_flip.h>
  28. #include <linux/spi/spi.h>
  29. #include <linux/uaccess.h>
  30. #define SC16IS7XX_NAME "sc16is7xx"
  31. #define SC16IS7XX_MAX_DEVS 8
  32. /* SC16IS7XX register definitions */
  33. #define SC16IS7XX_RHR_REG (0x00) /* RX FIFO */
  34. #define SC16IS7XX_THR_REG (0x00) /* TX FIFO */
  35. #define SC16IS7XX_IER_REG (0x01) /* Interrupt enable */
  36. #define SC16IS7XX_IIR_REG (0x02) /* Interrupt Identification */
  37. #define SC16IS7XX_FCR_REG (0x02) /* FIFO control */
  38. #define SC16IS7XX_LCR_REG (0x03) /* Line Control */
  39. #define SC16IS7XX_MCR_REG (0x04) /* Modem Control */
  40. #define SC16IS7XX_LSR_REG (0x05) /* Line Status */
  41. #define SC16IS7XX_MSR_REG (0x06) /* Modem Status */
  42. #define SC16IS7XX_SPR_REG (0x07) /* Scratch Pad */
  43. #define SC16IS7XX_TXLVL_REG (0x08) /* TX FIFO level */
  44. #define SC16IS7XX_RXLVL_REG (0x09) /* RX FIFO level */
  45. #define SC16IS7XX_IODIR_REG (0x0a) /* I/O Direction
  46. * - only on 75x/76x
  47. */
  48. #define SC16IS7XX_IOSTATE_REG (0x0b) /* I/O State
  49. * - only on 75x/76x
  50. */
  51. #define SC16IS7XX_IOINTENA_REG (0x0c) /* I/O Interrupt Enable
  52. * - only on 75x/76x
  53. */
  54. #define SC16IS7XX_IOCONTROL_REG (0x0e) /* I/O Control
  55. * - only on 75x/76x
  56. */
  57. #define SC16IS7XX_EFCR_REG (0x0f) /* Extra Features Control */
  58. /* TCR/TLR Register set: Only if ((MCR[2] == 1) && (EFR[4] == 1)) */
  59. #define SC16IS7XX_TCR_REG (0x06) /* Transmit control */
  60. #define SC16IS7XX_TLR_REG (0x07) /* Trigger level */
  61. /* Special Register set: Only if ((LCR[7] == 1) && (LCR != 0xBF)) */
  62. #define SC16IS7XX_DLL_REG (0x00) /* Divisor Latch Low */
  63. #define SC16IS7XX_DLH_REG (0x01) /* Divisor Latch High */
  64. /* Enhanced Register set: Only if (LCR == 0xBF) */
  65. #define SC16IS7XX_EFR_REG (0x02) /* Enhanced Features */
  66. #define SC16IS7XX_XON1_REG (0x04) /* Xon1 word */
  67. #define SC16IS7XX_XON2_REG (0x05) /* Xon2 word */
  68. #define SC16IS7XX_XOFF1_REG (0x06) /* Xoff1 word */
  69. #define SC16IS7XX_XOFF2_REG (0x07) /* Xoff2 word */
  70. /* IER register bits */
  71. #define SC16IS7XX_IER_RDI_BIT (1 << 0) /* Enable RX data interrupt */
  72. #define SC16IS7XX_IER_THRI_BIT (1 << 1) /* Enable TX holding register
  73. * interrupt */
  74. #define SC16IS7XX_IER_RLSI_BIT (1 << 2) /* Enable RX line status
  75. * interrupt */
  76. #define SC16IS7XX_IER_MSI_BIT (1 << 3) /* Enable Modem status
  77. * interrupt */
  78. /* IER register bits - write only if (EFR[4] == 1) */
  79. #define SC16IS7XX_IER_SLEEP_BIT (1 << 4) /* Enable Sleep mode */
  80. #define SC16IS7XX_IER_XOFFI_BIT (1 << 5) /* Enable Xoff interrupt */
  81. #define SC16IS7XX_IER_RTSI_BIT (1 << 6) /* Enable nRTS interrupt */
  82. #define SC16IS7XX_IER_CTSI_BIT (1 << 7) /* Enable nCTS interrupt */
  83. /* FCR register bits */
  84. #define SC16IS7XX_FCR_FIFO_BIT (1 << 0) /* Enable FIFO */
  85. #define SC16IS7XX_FCR_RXRESET_BIT (1 << 1) /* Reset RX FIFO */
  86. #define SC16IS7XX_FCR_TXRESET_BIT (1 << 2) /* Reset TX FIFO */
  87. #define SC16IS7XX_FCR_RXLVLL_BIT (1 << 6) /* RX Trigger level LSB */
  88. #define SC16IS7XX_FCR_RXLVLH_BIT (1 << 7) /* RX Trigger level MSB */
  89. /* FCR register bits - write only if (EFR[4] == 1) */
  90. #define SC16IS7XX_FCR_TXLVLL_BIT (1 << 4) /* TX Trigger level LSB */
  91. #define SC16IS7XX_FCR_TXLVLH_BIT (1 << 5) /* TX Trigger level MSB */
  92. /* IIR register bits */
  93. #define SC16IS7XX_IIR_NO_INT_BIT (1 << 0) /* No interrupts pending */
  94. #define SC16IS7XX_IIR_ID_MASK 0x3e /* Mask for the interrupt ID */
  95. #define SC16IS7XX_IIR_THRI_SRC 0x02 /* TX holding register empty */
  96. #define SC16IS7XX_IIR_RDI_SRC 0x04 /* RX data interrupt */
  97. #define SC16IS7XX_IIR_RLSE_SRC 0x06 /* RX line status error */
  98. #define SC16IS7XX_IIR_RTOI_SRC 0x0c /* RX time-out interrupt */
  99. #define SC16IS7XX_IIR_MSI_SRC 0x00 /* Modem status interrupt
  100. * - only on 75x/76x
  101. */
  102. #define SC16IS7XX_IIR_INPIN_SRC 0x30 /* Input pin change of state
  103. * - only on 75x/76x
  104. */
  105. #define SC16IS7XX_IIR_XOFFI_SRC 0x10 /* Received Xoff */
  106. #define SC16IS7XX_IIR_CTSRTS_SRC 0x20 /* nCTS,nRTS change of state
  107. * from active (LOW)
  108. * to inactive (HIGH)
  109. */
  110. /* LCR register bits */
  111. #define SC16IS7XX_LCR_LENGTH0_BIT (1 << 0) /* Word length bit 0 */
  112. #define SC16IS7XX_LCR_LENGTH1_BIT (1 << 1) /* Word length bit 1
  113. *
  114. * Word length bits table:
  115. * 00 -> 5 bit words
  116. * 01 -> 6 bit words
  117. * 10 -> 7 bit words
  118. * 11 -> 8 bit words
  119. */
  120. #define SC16IS7XX_LCR_STOPLEN_BIT (1 << 2) /* STOP length bit
  121. *
  122. * STOP length bit table:
  123. * 0 -> 1 stop bit
  124. * 1 -> 1-1.5 stop bits if
  125. * word length is 5,
  126. * 2 stop bits otherwise
  127. */
  128. #define SC16IS7XX_LCR_PARITY_BIT (1 << 3) /* Parity bit enable */
  129. #define SC16IS7XX_LCR_EVENPARITY_BIT (1 << 4) /* Even parity bit enable */
  130. #define SC16IS7XX_LCR_FORCEPARITY_BIT (1 << 5) /* 9-bit multidrop parity */
  131. #define SC16IS7XX_LCR_TXBREAK_BIT (1 << 6) /* TX break enable */
  132. #define SC16IS7XX_LCR_DLAB_BIT (1 << 7) /* Divisor Latch enable */
  133. #define SC16IS7XX_LCR_WORD_LEN_5 (0x00)
  134. #define SC16IS7XX_LCR_WORD_LEN_6 (0x01)
  135. #define SC16IS7XX_LCR_WORD_LEN_7 (0x02)
  136. #define SC16IS7XX_LCR_WORD_LEN_8 (0x03)
  137. #define SC16IS7XX_LCR_CONF_MODE_A SC16IS7XX_LCR_DLAB_BIT /* Special
  138. * reg set */
  139. #define SC16IS7XX_LCR_CONF_MODE_B 0xBF /* Enhanced
  140. * reg set */
  141. /* MCR register bits */
  142. #define SC16IS7XX_MCR_DTR_BIT (1 << 0) /* DTR complement
  143. * - only on 75x/76x
  144. */
  145. #define SC16IS7XX_MCR_RTS_BIT (1 << 1) /* RTS complement */
  146. #define SC16IS7XX_MCR_TCRTLR_BIT (1 << 2) /* TCR/TLR register enable */
  147. #define SC16IS7XX_MCR_LOOP_BIT (1 << 4) /* Enable loopback test mode */
  148. #define SC16IS7XX_MCR_XONANY_BIT (1 << 5) /* Enable Xon Any
  149. * - write enabled
  150. * if (EFR[4] == 1)
  151. */
  152. #define SC16IS7XX_MCR_IRDA_BIT (1 << 6) /* Enable IrDA mode
  153. * - write enabled
  154. * if (EFR[4] == 1)
  155. */
  156. #define SC16IS7XX_MCR_CLKSEL_BIT (1 << 7) /* Divide clock by 4
  157. * - write enabled
  158. * if (EFR[4] == 1)
  159. */
  160. /* LSR register bits */
  161. #define SC16IS7XX_LSR_DR_BIT (1 << 0) /* Receiver data ready */
  162. #define SC16IS7XX_LSR_OE_BIT (1 << 1) /* Overrun Error */
  163. #define SC16IS7XX_LSR_PE_BIT (1 << 2) /* Parity Error */
  164. #define SC16IS7XX_LSR_FE_BIT (1 << 3) /* Frame Error */
  165. #define SC16IS7XX_LSR_BI_BIT (1 << 4) /* Break Interrupt */
  166. #define SC16IS7XX_LSR_BRK_ERROR_MASK 0x1E /* BI, FE, PE, OE bits */
  167. #define SC16IS7XX_LSR_THRE_BIT (1 << 5) /* TX holding register empty */
  168. #define SC16IS7XX_LSR_TEMT_BIT (1 << 6) /* Transmitter empty */
  169. #define SC16IS7XX_LSR_FIFOE_BIT (1 << 7) /* Fifo Error */
  170. /* MSR register bits */
  171. #define SC16IS7XX_MSR_DCTS_BIT (1 << 0) /* Delta CTS Clear To Send */
  172. #define SC16IS7XX_MSR_DDSR_BIT (1 << 1) /* Delta DSR Data Set Ready
  173. * or (IO4)
  174. * - only on 75x/76x
  175. */
  176. #define SC16IS7XX_MSR_DRI_BIT (1 << 2) /* Delta RI Ring Indicator
  177. * or (IO7)
  178. * - only on 75x/76x
  179. */
  180. #define SC16IS7XX_MSR_DCD_BIT (1 << 3) /* Delta CD Carrier Detect
  181. * or (IO6)
  182. * - only on 75x/76x
  183. */
  184. #define SC16IS7XX_MSR_CTS_BIT (1 << 0) /* CTS */
  185. #define SC16IS7XX_MSR_DSR_BIT (1 << 1) /* DSR (IO4)
  186. * - only on 75x/76x
  187. */
  188. #define SC16IS7XX_MSR_RI_BIT (1 << 2) /* RI (IO7)
  189. * - only on 75x/76x
  190. */
  191. #define SC16IS7XX_MSR_CD_BIT (1 << 3) /* CD (IO6)
  192. * - only on 75x/76x
  193. */
  194. #define SC16IS7XX_MSR_DELTA_MASK 0x0F /* Any of the delta bits! */
  195. /*
  196. * TCR register bits
  197. * TCR trigger levels are available from 0 to 60 characters with a granularity
  198. * of four.
  199. * The programmer must program the TCR such that TCR[3:0] > TCR[7:4]. There is
  200. * no built-in hardware check to make sure this condition is met. Also, the TCR
  201. * must be programmed with this condition before auto RTS or software flow
  202. * control is enabled to avoid spurious operation of the device.
  203. */
  204. #define SC16IS7XX_TCR_RX_HALT(words) ((((words) / 4) & 0x0f) << 0)
  205. #define SC16IS7XX_TCR_RX_RESUME(words) ((((words) / 4) & 0x0f) << 4)
  206. /*
  207. * TLR register bits
  208. * If TLR[3:0] or TLR[7:4] are logical 0, the selectable trigger levels via the
  209. * FIFO Control Register (FCR) are used for the transmit and receive FIFO
  210. * trigger levels. Trigger levels from 4 characters to 60 characters are
  211. * available with a granularity of four.
  212. *
  213. * When the trigger level setting in TLR is zero, the SC16IS740/750/760 uses the
  214. * trigger level setting defined in FCR. If TLR has non-zero trigger level value
  215. * the trigger level defined in FCR is discarded. This applies to both transmit
  216. * FIFO and receive FIFO trigger level setting.
  217. *
  218. * When TLR is used for RX trigger level control, FCR[7:6] should be left at the
  219. * default state, that is, '00'.
  220. */
  221. #define SC16IS7XX_TLR_TX_TRIGGER(words) ((((words) / 4) & 0x0f) << 0)
  222. #define SC16IS7XX_TLR_RX_TRIGGER(words) ((((words) / 4) & 0x0f) << 4)
  223. /* IOControl register bits (Only 750/760) */
  224. #define SC16IS7XX_IOCONTROL_LATCH_BIT (1 << 0) /* Enable input latching */
  225. #define SC16IS7XX_IOCONTROL_GPIO_BIT (1 << 1) /* Enable GPIO[7:4] */
  226. #define SC16IS7XX_IOCONTROL_SRESET_BIT (1 << 3) /* Software Reset */
  227. /* EFCR register bits */
  228. #define SC16IS7XX_EFCR_9BIT_MODE_BIT (1 << 0) /* Enable 9-bit or Multidrop
  229. * mode (RS485) */
  230. #define SC16IS7XX_EFCR_RXDISABLE_BIT (1 << 1) /* Disable receiver */
  231. #define SC16IS7XX_EFCR_TXDISABLE_BIT (1 << 2) /* Disable transmitter */
  232. #define SC16IS7XX_EFCR_AUTO_RS485_BIT (1 << 4) /* Auto RS485 RTS direction */
  233. #define SC16IS7XX_EFCR_RTS_INVERT_BIT (1 << 5) /* RTS output inversion */
  234. #define SC16IS7XX_EFCR_IRDA_MODE_BIT (1 << 7) /* IrDA mode
  235. * 0 = rate upto 115.2 kbit/s
  236. * - Only 750/760
  237. * 1 = rate upto 1.152 Mbit/s
  238. * - Only 760
  239. */
  240. /* EFR register bits */
  241. #define SC16IS7XX_EFR_AUTORTS_BIT (1 << 6) /* Auto RTS flow ctrl enable */
  242. #define SC16IS7XX_EFR_AUTOCTS_BIT (1 << 7) /* Auto CTS flow ctrl enable */
  243. #define SC16IS7XX_EFR_XOFF2_DETECT_BIT (1 << 5) /* Enable Xoff2 detection */
  244. #define SC16IS7XX_EFR_ENABLE_BIT (1 << 4) /* Enable enhanced functions
  245. * and writing to IER[7:4],
  246. * FCR[5:4], MCR[7:5]
  247. */
  248. #define SC16IS7XX_EFR_SWFLOW3_BIT (1 << 3) /* SWFLOW bit 3 */
  249. #define SC16IS7XX_EFR_SWFLOW2_BIT (1 << 2) /* SWFLOW bit 2
  250. *
  251. * SWFLOW bits 3 & 2 table:
  252. * 00 -> no transmitter flow
  253. * control
  254. * 01 -> transmitter generates
  255. * XON2 and XOFF2
  256. * 10 -> transmitter generates
  257. * XON1 and XOFF1
  258. * 11 -> transmitter generates
  259. * XON1, XON2, XOFF1 and
  260. * XOFF2
  261. */
  262. #define SC16IS7XX_EFR_SWFLOW1_BIT (1 << 1) /* SWFLOW bit 2 */
  263. #define SC16IS7XX_EFR_SWFLOW0_BIT (1 << 0) /* SWFLOW bit 3
  264. *
  265. * SWFLOW bits 3 & 2 table:
  266. * 00 -> no received flow
  267. * control
  268. * 01 -> receiver compares
  269. * XON2 and XOFF2
  270. * 10 -> receiver compares
  271. * XON1 and XOFF1
  272. * 11 -> receiver compares
  273. * XON1, XON2, XOFF1 and
  274. * XOFF2
  275. */
  276. /* Misc definitions */
  277. #define SC16IS7XX_FIFO_SIZE (64)
  278. #define SC16IS7XX_REG_SHIFT 2
  279. struct sc16is7xx_devtype {
  280. char name[10];
  281. int nr_gpio;
  282. int nr_uart;
  283. };
  284. #define SC16IS7XX_RECONF_MD (1 << 0)
  285. #define SC16IS7XX_RECONF_IER (1 << 1)
  286. #define SC16IS7XX_RECONF_RS485 (1 << 2)
  287. struct sc16is7xx_one_config {
  288. unsigned int flags;
  289. u8 ier_clear;
  290. };
  291. struct sc16is7xx_one {
  292. struct uart_port port;
  293. u8 line;
  294. struct kthread_work tx_work;
  295. struct kthread_work reg_work;
  296. struct sc16is7xx_one_config config;
  297. };
  298. struct sc16is7xx_port {
  299. const struct sc16is7xx_devtype *devtype;
  300. struct regmap *regmap;
  301. struct clk *clk;
  302. #ifdef CONFIG_GPIOLIB
  303. struct gpio_chip gpio;
  304. #endif
  305. unsigned char buf[SC16IS7XX_FIFO_SIZE];
  306. struct kthread_worker kworker;
  307. struct task_struct *kworker_task;
  308. struct kthread_work irq_work;
  309. struct sc16is7xx_one p[0];
  310. };
  311. static unsigned long sc16is7xx_lines;
  312. static struct uart_driver sc16is7xx_uart = {
  313. .owner = THIS_MODULE,
  314. .dev_name = "ttySC",
  315. .nr = SC16IS7XX_MAX_DEVS,
  316. };
  317. #define to_sc16is7xx_port(p,e) ((container_of((p), struct sc16is7xx_port, e)))
  318. #define to_sc16is7xx_one(p,e) ((container_of((p), struct sc16is7xx_one, e)))
  319. static int sc16is7xx_line(struct uart_port *port)
  320. {
  321. struct sc16is7xx_one *one = to_sc16is7xx_one(port, port);
  322. return one->line;
  323. }
  324. static u8 sc16is7xx_port_read(struct uart_port *port, u8 reg)
  325. {
  326. struct sc16is7xx_port *s = dev_get_drvdata(port->dev);
  327. unsigned int val = 0;
  328. const u8 line = sc16is7xx_line(port);
  329. regmap_read(s->regmap, (reg << SC16IS7XX_REG_SHIFT) | line, &val);
  330. return val;
  331. }
  332. static void sc16is7xx_port_write(struct uart_port *port, u8 reg, u8 val)
  333. {
  334. struct sc16is7xx_port *s = dev_get_drvdata(port->dev);
  335. const u8 line = sc16is7xx_line(port);
  336. regmap_write(s->regmap, (reg << SC16IS7XX_REG_SHIFT) | line, val);
  337. }
  338. static void sc16is7xx_fifo_read(struct uart_port *port, unsigned int rxlen)
  339. {
  340. struct sc16is7xx_port *s = dev_get_drvdata(port->dev);
  341. const u8 line = sc16is7xx_line(port);
  342. u8 addr = (SC16IS7XX_RHR_REG << SC16IS7XX_REG_SHIFT) | line;
  343. regcache_cache_bypass(s->regmap, true);
  344. regmap_raw_read(s->regmap, addr, s->buf, rxlen);
  345. regcache_cache_bypass(s->regmap, false);
  346. }
  347. static void sc16is7xx_fifo_write(struct uart_port *port, u8 to_send)
  348. {
  349. struct sc16is7xx_port *s = dev_get_drvdata(port->dev);
  350. const u8 line = sc16is7xx_line(port);
  351. u8 addr = (SC16IS7XX_THR_REG << SC16IS7XX_REG_SHIFT) | line;
  352. regcache_cache_bypass(s->regmap, true);
  353. regmap_raw_write(s->regmap, addr, s->buf, to_send);
  354. regcache_cache_bypass(s->regmap, false);
  355. }
  356. static void sc16is7xx_port_update(struct uart_port *port, u8 reg,
  357. u8 mask, u8 val)
  358. {
  359. struct sc16is7xx_port *s = dev_get_drvdata(port->dev);
  360. const u8 line = sc16is7xx_line(port);
  361. regmap_update_bits(s->regmap, (reg << SC16IS7XX_REG_SHIFT) | line,
  362. mask, val);
  363. }
  364. static int sc16is7xx_alloc_line(void)
  365. {
  366. int i;
  367. BUILD_BUG_ON(SC16IS7XX_MAX_DEVS > BITS_PER_LONG);
  368. for (i = 0; i < SC16IS7XX_MAX_DEVS; i++)
  369. if (!test_and_set_bit(i, &sc16is7xx_lines))
  370. break;
  371. return i;
  372. }
  373. static void sc16is7xx_power(struct uart_port *port, int on)
  374. {
  375. sc16is7xx_port_update(port, SC16IS7XX_IER_REG,
  376. SC16IS7XX_IER_SLEEP_BIT,
  377. on ? 0 : SC16IS7XX_IER_SLEEP_BIT);
  378. }
  379. static const struct sc16is7xx_devtype sc16is74x_devtype = {
  380. .name = "SC16IS74X",
  381. .nr_gpio = 0,
  382. .nr_uart = 1,
  383. };
  384. static const struct sc16is7xx_devtype sc16is750_devtype = {
  385. .name = "SC16IS750",
  386. .nr_gpio = 8,
  387. .nr_uart = 1,
  388. };
  389. static const struct sc16is7xx_devtype sc16is752_devtype = {
  390. .name = "SC16IS752",
  391. .nr_gpio = 8,
  392. .nr_uart = 2,
  393. };
  394. static const struct sc16is7xx_devtype sc16is760_devtype = {
  395. .name = "SC16IS760",
  396. .nr_gpio = 8,
  397. .nr_uart = 1,
  398. };
  399. static const struct sc16is7xx_devtype sc16is762_devtype = {
  400. .name = "SC16IS762",
  401. .nr_gpio = 8,
  402. .nr_uart = 2,
  403. };
  404. static bool sc16is7xx_regmap_volatile(struct device *dev, unsigned int reg)
  405. {
  406. switch (reg >> SC16IS7XX_REG_SHIFT) {
  407. case SC16IS7XX_RHR_REG:
  408. case SC16IS7XX_IIR_REG:
  409. case SC16IS7XX_LSR_REG:
  410. case SC16IS7XX_MSR_REG:
  411. case SC16IS7XX_TXLVL_REG:
  412. case SC16IS7XX_RXLVL_REG:
  413. case SC16IS7XX_IOSTATE_REG:
  414. return true;
  415. default:
  416. break;
  417. }
  418. return false;
  419. }
  420. static bool sc16is7xx_regmap_precious(struct device *dev, unsigned int reg)
  421. {
  422. switch (reg >> SC16IS7XX_REG_SHIFT) {
  423. case SC16IS7XX_RHR_REG:
  424. return true;
  425. default:
  426. break;
  427. }
  428. return false;
  429. }
  430. static int sc16is7xx_set_baud(struct uart_port *port, int baud)
  431. {
  432. struct sc16is7xx_port *s = dev_get_drvdata(port->dev);
  433. u8 lcr;
  434. u8 prescaler = 0;
  435. unsigned long clk = port->uartclk, div = clk / 16 / baud;
  436. if (div > 0xffff) {
  437. prescaler = SC16IS7XX_MCR_CLKSEL_BIT;
  438. div /= 4;
  439. }
  440. lcr = sc16is7xx_port_read(port, SC16IS7XX_LCR_REG);
  441. /* Open the LCR divisors for configuration */
  442. sc16is7xx_port_write(port, SC16IS7XX_LCR_REG,
  443. SC16IS7XX_LCR_CONF_MODE_B);
  444. /* Enable enhanced features */
  445. regcache_cache_bypass(s->regmap, true);
  446. sc16is7xx_port_write(port, SC16IS7XX_EFR_REG,
  447. SC16IS7XX_EFR_ENABLE_BIT);
  448. regcache_cache_bypass(s->regmap, false);
  449. /* Put LCR back to the normal mode */
  450. sc16is7xx_port_write(port, SC16IS7XX_LCR_REG, lcr);
  451. sc16is7xx_port_update(port, SC16IS7XX_MCR_REG,
  452. SC16IS7XX_MCR_CLKSEL_BIT,
  453. prescaler);
  454. /* Open the LCR divisors for configuration */
  455. sc16is7xx_port_write(port, SC16IS7XX_LCR_REG,
  456. SC16IS7XX_LCR_CONF_MODE_A);
  457. /* Write the new divisor */
  458. regcache_cache_bypass(s->regmap, true);
  459. sc16is7xx_port_write(port, SC16IS7XX_DLH_REG, div / 256);
  460. sc16is7xx_port_write(port, SC16IS7XX_DLL_REG, div % 256);
  461. regcache_cache_bypass(s->regmap, false);
  462. /* Put LCR back to the normal mode */
  463. sc16is7xx_port_write(port, SC16IS7XX_LCR_REG, lcr);
  464. return DIV_ROUND_CLOSEST(clk / 16, div);
  465. }
  466. static void sc16is7xx_handle_rx(struct uart_port *port, unsigned int rxlen,
  467. unsigned int iir)
  468. {
  469. struct sc16is7xx_port *s = dev_get_drvdata(port->dev);
  470. unsigned int lsr = 0, ch, flag, bytes_read, i;
  471. bool read_lsr = (iir == SC16IS7XX_IIR_RLSE_SRC) ? true : false;
  472. if (unlikely(rxlen >= sizeof(s->buf))) {
  473. dev_warn_ratelimited(port->dev,
  474. "ttySC%i: Possible RX FIFO overrun: %d\n",
  475. port->line, rxlen);
  476. port->icount.buf_overrun++;
  477. /* Ensure sanity of RX level */
  478. rxlen = sizeof(s->buf);
  479. }
  480. while (rxlen) {
  481. /* Only read lsr if there are possible errors in FIFO */
  482. if (read_lsr) {
  483. lsr = sc16is7xx_port_read(port, SC16IS7XX_LSR_REG);
  484. if (!(lsr & SC16IS7XX_LSR_FIFOE_BIT))
  485. read_lsr = false; /* No errors left in FIFO */
  486. } else
  487. lsr = 0;
  488. if (read_lsr) {
  489. s->buf[0] = sc16is7xx_port_read(port, SC16IS7XX_RHR_REG);
  490. bytes_read = 1;
  491. } else {
  492. sc16is7xx_fifo_read(port, rxlen);
  493. bytes_read = rxlen;
  494. }
  495. lsr &= SC16IS7XX_LSR_BRK_ERROR_MASK;
  496. port->icount.rx++;
  497. flag = TTY_NORMAL;
  498. if (unlikely(lsr)) {
  499. if (lsr & SC16IS7XX_LSR_BI_BIT) {
  500. port->icount.brk++;
  501. if (uart_handle_break(port))
  502. continue;
  503. } else if (lsr & SC16IS7XX_LSR_PE_BIT)
  504. port->icount.parity++;
  505. else if (lsr & SC16IS7XX_LSR_FE_BIT)
  506. port->icount.frame++;
  507. else if (lsr & SC16IS7XX_LSR_OE_BIT)
  508. port->icount.overrun++;
  509. lsr &= port->read_status_mask;
  510. if (lsr & SC16IS7XX_LSR_BI_BIT)
  511. flag = TTY_BREAK;
  512. else if (lsr & SC16IS7XX_LSR_PE_BIT)
  513. flag = TTY_PARITY;
  514. else if (lsr & SC16IS7XX_LSR_FE_BIT)
  515. flag = TTY_FRAME;
  516. else if (lsr & SC16IS7XX_LSR_OE_BIT)
  517. flag = TTY_OVERRUN;
  518. }
  519. for (i = 0; i < bytes_read; ++i) {
  520. ch = s->buf[i];
  521. if (uart_handle_sysrq_char(port, ch))
  522. continue;
  523. if (lsr & port->ignore_status_mask)
  524. continue;
  525. uart_insert_char(port, lsr, SC16IS7XX_LSR_OE_BIT, ch,
  526. flag);
  527. }
  528. rxlen -= bytes_read;
  529. }
  530. tty_flip_buffer_push(&port->state->port);
  531. }
  532. static void sc16is7xx_handle_tx(struct uart_port *port)
  533. {
  534. struct sc16is7xx_port *s = dev_get_drvdata(port->dev);
  535. struct circ_buf *xmit = &port->state->xmit;
  536. unsigned int txlen, to_send, i;
  537. if (unlikely(port->x_char)) {
  538. sc16is7xx_port_write(port, SC16IS7XX_THR_REG, port->x_char);
  539. port->icount.tx++;
  540. port->x_char = 0;
  541. return;
  542. }
  543. if (uart_circ_empty(xmit) || uart_tx_stopped(port))
  544. return;
  545. /* Get length of data pending in circular buffer */
  546. to_send = uart_circ_chars_pending(xmit);
  547. if (likely(to_send)) {
  548. /* Limit to size of TX FIFO */
  549. txlen = sc16is7xx_port_read(port, SC16IS7XX_TXLVL_REG);
  550. to_send = (to_send > txlen) ? txlen : to_send;
  551. /* Add data to send */
  552. port->icount.tx += to_send;
  553. /* Convert to linear buffer */
  554. for (i = 0; i < to_send; ++i) {
  555. s->buf[i] = xmit->buf[xmit->tail];
  556. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  557. }
  558. sc16is7xx_fifo_write(port, to_send);
  559. }
  560. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  561. uart_write_wakeup(port);
  562. }
  563. static void sc16is7xx_port_irq(struct sc16is7xx_port *s, int portno)
  564. {
  565. struct uart_port *port = &s->p[portno].port;
  566. do {
  567. unsigned int iir, msr, rxlen;
  568. iir = sc16is7xx_port_read(port, SC16IS7XX_IIR_REG);
  569. if (iir & SC16IS7XX_IIR_NO_INT_BIT)
  570. break;
  571. iir &= SC16IS7XX_IIR_ID_MASK;
  572. switch (iir) {
  573. case SC16IS7XX_IIR_RDI_SRC:
  574. case SC16IS7XX_IIR_RLSE_SRC:
  575. case SC16IS7XX_IIR_RTOI_SRC:
  576. case SC16IS7XX_IIR_XOFFI_SRC:
  577. rxlen = sc16is7xx_port_read(port, SC16IS7XX_RXLVL_REG);
  578. if (rxlen)
  579. sc16is7xx_handle_rx(port, rxlen, iir);
  580. break;
  581. case SC16IS7XX_IIR_CTSRTS_SRC:
  582. msr = sc16is7xx_port_read(port, SC16IS7XX_MSR_REG);
  583. uart_handle_cts_change(port,
  584. !!(msr & SC16IS7XX_MSR_CTS_BIT));
  585. break;
  586. case SC16IS7XX_IIR_THRI_SRC:
  587. sc16is7xx_handle_tx(port);
  588. break;
  589. default:
  590. dev_err_ratelimited(port->dev,
  591. "ttySC%i: Unexpected interrupt: %x",
  592. port->line, iir);
  593. break;
  594. }
  595. } while (1);
  596. }
  597. static void sc16is7xx_ist(struct kthread_work *ws)
  598. {
  599. struct sc16is7xx_port *s = to_sc16is7xx_port(ws, irq_work);
  600. int i;
  601. for (i = 0; i < s->devtype->nr_uart; ++i)
  602. sc16is7xx_port_irq(s, i);
  603. }
  604. static irqreturn_t sc16is7xx_irq(int irq, void *dev_id)
  605. {
  606. struct sc16is7xx_port *s = (struct sc16is7xx_port *)dev_id;
  607. queue_kthread_work(&s->kworker, &s->irq_work);
  608. return IRQ_HANDLED;
  609. }
  610. static void sc16is7xx_tx_proc(struct kthread_work *ws)
  611. {
  612. struct uart_port *port = &(to_sc16is7xx_one(ws, tx_work)->port);
  613. if ((port->rs485.flags & SER_RS485_ENABLED) &&
  614. (port->rs485.delay_rts_before_send > 0))
  615. msleep(port->rs485.delay_rts_before_send);
  616. sc16is7xx_handle_tx(port);
  617. }
  618. static void sc16is7xx_reconf_rs485(struct uart_port *port)
  619. {
  620. const u32 mask = SC16IS7XX_EFCR_AUTO_RS485_BIT |
  621. SC16IS7XX_EFCR_RTS_INVERT_BIT;
  622. u32 efcr = 0;
  623. struct serial_rs485 *rs485 = &port->rs485;
  624. unsigned long irqflags;
  625. spin_lock_irqsave(&port->lock, irqflags);
  626. if (rs485->flags & SER_RS485_ENABLED) {
  627. efcr |= SC16IS7XX_EFCR_AUTO_RS485_BIT;
  628. if (rs485->flags & SER_RS485_RTS_AFTER_SEND)
  629. efcr |= SC16IS7XX_EFCR_RTS_INVERT_BIT;
  630. }
  631. spin_unlock_irqrestore(&port->lock, irqflags);
  632. sc16is7xx_port_update(port, SC16IS7XX_EFCR_REG, mask, efcr);
  633. }
  634. static void sc16is7xx_reg_proc(struct kthread_work *ws)
  635. {
  636. struct sc16is7xx_one *one = to_sc16is7xx_one(ws, reg_work);
  637. struct sc16is7xx_one_config config;
  638. unsigned long irqflags;
  639. spin_lock_irqsave(&one->port.lock, irqflags);
  640. config = one->config;
  641. memset(&one->config, 0, sizeof(one->config));
  642. spin_unlock_irqrestore(&one->port.lock, irqflags);
  643. if (config.flags & SC16IS7XX_RECONF_MD)
  644. sc16is7xx_port_update(&one->port, SC16IS7XX_MCR_REG,
  645. SC16IS7XX_MCR_LOOP_BIT,
  646. (one->port.mctrl & TIOCM_LOOP) ?
  647. SC16IS7XX_MCR_LOOP_BIT : 0);
  648. if (config.flags & SC16IS7XX_RECONF_IER)
  649. sc16is7xx_port_update(&one->port, SC16IS7XX_IER_REG,
  650. config.ier_clear, 0);
  651. if (config.flags & SC16IS7XX_RECONF_RS485)
  652. sc16is7xx_reconf_rs485(&one->port);
  653. }
  654. static void sc16is7xx_ier_clear(struct uart_port *port, u8 bit)
  655. {
  656. struct sc16is7xx_port *s = dev_get_drvdata(port->dev);
  657. struct sc16is7xx_one *one = to_sc16is7xx_one(port, port);
  658. one->config.flags |= SC16IS7XX_RECONF_IER;
  659. one->config.ier_clear |= bit;
  660. queue_kthread_work(&s->kworker, &one->reg_work);
  661. }
  662. static void sc16is7xx_stop_tx(struct uart_port *port)
  663. {
  664. sc16is7xx_ier_clear(port, SC16IS7XX_IER_THRI_BIT);
  665. }
  666. static void sc16is7xx_stop_rx(struct uart_port *port)
  667. {
  668. sc16is7xx_ier_clear(port, SC16IS7XX_IER_RDI_BIT);
  669. }
  670. static void sc16is7xx_start_tx(struct uart_port *port)
  671. {
  672. struct sc16is7xx_port *s = dev_get_drvdata(port->dev);
  673. struct sc16is7xx_one *one = to_sc16is7xx_one(port, port);
  674. queue_kthread_work(&s->kworker, &one->tx_work);
  675. }
  676. static unsigned int sc16is7xx_tx_empty(struct uart_port *port)
  677. {
  678. unsigned int lsr;
  679. lsr = sc16is7xx_port_read(port, SC16IS7XX_LSR_REG);
  680. return (lsr & SC16IS7XX_LSR_TEMT_BIT) ? TIOCSER_TEMT : 0;
  681. }
  682. static unsigned int sc16is7xx_get_mctrl(struct uart_port *port)
  683. {
  684. /* DCD and DSR are not wired and CTS/RTS is handled automatically
  685. * so just indicate DSR and CAR asserted
  686. */
  687. return TIOCM_DSR | TIOCM_CAR;
  688. }
  689. static void sc16is7xx_set_mctrl(struct uart_port *port, unsigned int mctrl)
  690. {
  691. struct sc16is7xx_port *s = dev_get_drvdata(port->dev);
  692. struct sc16is7xx_one *one = to_sc16is7xx_one(port, port);
  693. one->config.flags |= SC16IS7XX_RECONF_MD;
  694. queue_kthread_work(&s->kworker, &one->reg_work);
  695. }
  696. static void sc16is7xx_break_ctl(struct uart_port *port, int break_state)
  697. {
  698. sc16is7xx_port_update(port, SC16IS7XX_LCR_REG,
  699. SC16IS7XX_LCR_TXBREAK_BIT,
  700. break_state ? SC16IS7XX_LCR_TXBREAK_BIT : 0);
  701. }
  702. static void sc16is7xx_set_termios(struct uart_port *port,
  703. struct ktermios *termios,
  704. struct ktermios *old)
  705. {
  706. struct sc16is7xx_port *s = dev_get_drvdata(port->dev);
  707. unsigned int lcr, flow = 0;
  708. int baud;
  709. /* Mask termios capabilities we don't support */
  710. termios->c_cflag &= ~CMSPAR;
  711. /* Word size */
  712. switch (termios->c_cflag & CSIZE) {
  713. case CS5:
  714. lcr = SC16IS7XX_LCR_WORD_LEN_5;
  715. break;
  716. case CS6:
  717. lcr = SC16IS7XX_LCR_WORD_LEN_6;
  718. break;
  719. case CS7:
  720. lcr = SC16IS7XX_LCR_WORD_LEN_7;
  721. break;
  722. case CS8:
  723. lcr = SC16IS7XX_LCR_WORD_LEN_8;
  724. break;
  725. default:
  726. lcr = SC16IS7XX_LCR_WORD_LEN_8;
  727. termios->c_cflag &= ~CSIZE;
  728. termios->c_cflag |= CS8;
  729. break;
  730. }
  731. /* Parity */
  732. if (termios->c_cflag & PARENB) {
  733. lcr |= SC16IS7XX_LCR_PARITY_BIT;
  734. if (!(termios->c_cflag & PARODD))
  735. lcr |= SC16IS7XX_LCR_EVENPARITY_BIT;
  736. }
  737. /* Stop bits */
  738. if (termios->c_cflag & CSTOPB)
  739. lcr |= SC16IS7XX_LCR_STOPLEN_BIT; /* 2 stops */
  740. /* Set read status mask */
  741. port->read_status_mask = SC16IS7XX_LSR_OE_BIT;
  742. if (termios->c_iflag & INPCK)
  743. port->read_status_mask |= SC16IS7XX_LSR_PE_BIT |
  744. SC16IS7XX_LSR_FE_BIT;
  745. if (termios->c_iflag & (BRKINT | PARMRK))
  746. port->read_status_mask |= SC16IS7XX_LSR_BI_BIT;
  747. /* Set status ignore mask */
  748. port->ignore_status_mask = 0;
  749. if (termios->c_iflag & IGNBRK)
  750. port->ignore_status_mask |= SC16IS7XX_LSR_BI_BIT;
  751. if (!(termios->c_cflag & CREAD))
  752. port->ignore_status_mask |= SC16IS7XX_LSR_BRK_ERROR_MASK;
  753. sc16is7xx_port_write(port, SC16IS7XX_LCR_REG,
  754. SC16IS7XX_LCR_CONF_MODE_B);
  755. /* Configure flow control */
  756. regcache_cache_bypass(s->regmap, true);
  757. sc16is7xx_port_write(port, SC16IS7XX_XON1_REG, termios->c_cc[VSTART]);
  758. sc16is7xx_port_write(port, SC16IS7XX_XOFF1_REG, termios->c_cc[VSTOP]);
  759. if (termios->c_cflag & CRTSCTS)
  760. flow |= SC16IS7XX_EFR_AUTOCTS_BIT |
  761. SC16IS7XX_EFR_AUTORTS_BIT;
  762. if (termios->c_iflag & IXON)
  763. flow |= SC16IS7XX_EFR_SWFLOW3_BIT;
  764. if (termios->c_iflag & IXOFF)
  765. flow |= SC16IS7XX_EFR_SWFLOW1_BIT;
  766. sc16is7xx_port_write(port, SC16IS7XX_EFR_REG, flow);
  767. regcache_cache_bypass(s->regmap, false);
  768. /* Update LCR register */
  769. sc16is7xx_port_write(port, SC16IS7XX_LCR_REG, lcr);
  770. /* Get baud rate generator configuration */
  771. baud = uart_get_baud_rate(port, termios, old,
  772. port->uartclk / 16 / 4 / 0xffff,
  773. port->uartclk / 16);
  774. /* Setup baudrate generator */
  775. baud = sc16is7xx_set_baud(port, baud);
  776. /* Update timeout according to new baud rate */
  777. uart_update_timeout(port, termios->c_cflag, baud);
  778. }
  779. static int sc16is7xx_config_rs485(struct uart_port *port,
  780. struct serial_rs485 *rs485)
  781. {
  782. struct sc16is7xx_port *s = dev_get_drvdata(port->dev);
  783. struct sc16is7xx_one *one = to_sc16is7xx_one(port, port);
  784. if (rs485->flags & SER_RS485_ENABLED) {
  785. bool rts_during_rx, rts_during_tx;
  786. rts_during_rx = rs485->flags & SER_RS485_RTS_AFTER_SEND;
  787. rts_during_tx = rs485->flags & SER_RS485_RTS_ON_SEND;
  788. if (rts_during_rx == rts_during_tx)
  789. dev_err(port->dev,
  790. "unsupported RTS signalling on_send:%d after_send:%d - exactly one of RS485 RTS flags should be set\n",
  791. rts_during_tx, rts_during_rx);
  792. /*
  793. * RTS signal is handled by HW, it's timing can't be influenced.
  794. * However, it's sometimes useful to delay TX even without RTS
  795. * control therefore we try to handle .delay_rts_before_send.
  796. */
  797. if (rs485->delay_rts_after_send)
  798. return -EINVAL;
  799. }
  800. port->rs485 = *rs485;
  801. one->config.flags |= SC16IS7XX_RECONF_RS485;
  802. queue_kthread_work(&s->kworker, &one->reg_work);
  803. return 0;
  804. }
  805. static int sc16is7xx_startup(struct uart_port *port)
  806. {
  807. struct sc16is7xx_port *s = dev_get_drvdata(port->dev);
  808. unsigned int val;
  809. sc16is7xx_power(port, 1);
  810. /* Reset FIFOs*/
  811. val = SC16IS7XX_FCR_RXRESET_BIT | SC16IS7XX_FCR_TXRESET_BIT;
  812. sc16is7xx_port_write(port, SC16IS7XX_FCR_REG, val);
  813. udelay(5);
  814. sc16is7xx_port_write(port, SC16IS7XX_FCR_REG,
  815. SC16IS7XX_FCR_FIFO_BIT);
  816. /* Enable EFR */
  817. sc16is7xx_port_write(port, SC16IS7XX_LCR_REG,
  818. SC16IS7XX_LCR_CONF_MODE_B);
  819. regcache_cache_bypass(s->regmap, true);
  820. /* Enable write access to enhanced features and internal clock div */
  821. sc16is7xx_port_write(port, SC16IS7XX_EFR_REG,
  822. SC16IS7XX_EFR_ENABLE_BIT);
  823. /* Enable TCR/TLR */
  824. sc16is7xx_port_update(port, SC16IS7XX_MCR_REG,
  825. SC16IS7XX_MCR_TCRTLR_BIT,
  826. SC16IS7XX_MCR_TCRTLR_BIT);
  827. /* Configure flow control levels */
  828. /* Flow control halt level 48, resume level 24 */
  829. sc16is7xx_port_write(port, SC16IS7XX_TCR_REG,
  830. SC16IS7XX_TCR_RX_RESUME(24) |
  831. SC16IS7XX_TCR_RX_HALT(48));
  832. regcache_cache_bypass(s->regmap, false);
  833. /* Now, initialize the UART */
  834. sc16is7xx_port_write(port, SC16IS7XX_LCR_REG, SC16IS7XX_LCR_WORD_LEN_8);
  835. /* Enable the Rx and Tx FIFO */
  836. sc16is7xx_port_update(port, SC16IS7XX_EFCR_REG,
  837. SC16IS7XX_EFCR_RXDISABLE_BIT |
  838. SC16IS7XX_EFCR_TXDISABLE_BIT,
  839. 0);
  840. /* Enable RX, TX, CTS change interrupts */
  841. val = SC16IS7XX_IER_RDI_BIT | SC16IS7XX_IER_THRI_BIT |
  842. SC16IS7XX_IER_CTSI_BIT;
  843. sc16is7xx_port_write(port, SC16IS7XX_IER_REG, val);
  844. return 0;
  845. }
  846. static void sc16is7xx_shutdown(struct uart_port *port)
  847. {
  848. struct sc16is7xx_port *s = dev_get_drvdata(port->dev);
  849. /* Disable all interrupts */
  850. sc16is7xx_port_write(port, SC16IS7XX_IER_REG, 0);
  851. /* Disable TX/RX */
  852. sc16is7xx_port_update(port, SC16IS7XX_EFCR_REG,
  853. SC16IS7XX_EFCR_RXDISABLE_BIT |
  854. SC16IS7XX_EFCR_TXDISABLE_BIT,
  855. SC16IS7XX_EFCR_RXDISABLE_BIT |
  856. SC16IS7XX_EFCR_TXDISABLE_BIT);
  857. sc16is7xx_power(port, 0);
  858. flush_kthread_worker(&s->kworker);
  859. }
  860. static const char *sc16is7xx_type(struct uart_port *port)
  861. {
  862. struct sc16is7xx_port *s = dev_get_drvdata(port->dev);
  863. return (port->type == PORT_SC16IS7XX) ? s->devtype->name : NULL;
  864. }
  865. static int sc16is7xx_request_port(struct uart_port *port)
  866. {
  867. /* Do nothing */
  868. return 0;
  869. }
  870. static void sc16is7xx_config_port(struct uart_port *port, int flags)
  871. {
  872. if (flags & UART_CONFIG_TYPE)
  873. port->type = PORT_SC16IS7XX;
  874. }
  875. static int sc16is7xx_verify_port(struct uart_port *port,
  876. struct serial_struct *s)
  877. {
  878. if ((s->type != PORT_UNKNOWN) && (s->type != PORT_SC16IS7XX))
  879. return -EINVAL;
  880. if (s->irq != port->irq)
  881. return -EINVAL;
  882. return 0;
  883. }
  884. static void sc16is7xx_pm(struct uart_port *port, unsigned int state,
  885. unsigned int oldstate)
  886. {
  887. sc16is7xx_power(port, (state == UART_PM_STATE_ON) ? 1 : 0);
  888. }
  889. static void sc16is7xx_null_void(struct uart_port *port)
  890. {
  891. /* Do nothing */
  892. }
  893. static const struct uart_ops sc16is7xx_ops = {
  894. .tx_empty = sc16is7xx_tx_empty,
  895. .set_mctrl = sc16is7xx_set_mctrl,
  896. .get_mctrl = sc16is7xx_get_mctrl,
  897. .stop_tx = sc16is7xx_stop_tx,
  898. .start_tx = sc16is7xx_start_tx,
  899. .stop_rx = sc16is7xx_stop_rx,
  900. .break_ctl = sc16is7xx_break_ctl,
  901. .startup = sc16is7xx_startup,
  902. .shutdown = sc16is7xx_shutdown,
  903. .set_termios = sc16is7xx_set_termios,
  904. .type = sc16is7xx_type,
  905. .request_port = sc16is7xx_request_port,
  906. .release_port = sc16is7xx_null_void,
  907. .config_port = sc16is7xx_config_port,
  908. .verify_port = sc16is7xx_verify_port,
  909. .pm = sc16is7xx_pm,
  910. };
  911. #ifdef CONFIG_GPIOLIB
  912. static int sc16is7xx_gpio_get(struct gpio_chip *chip, unsigned offset)
  913. {
  914. unsigned int val;
  915. struct sc16is7xx_port *s = container_of(chip, struct sc16is7xx_port,
  916. gpio);
  917. struct uart_port *port = &s->p[0].port;
  918. val = sc16is7xx_port_read(port, SC16IS7XX_IOSTATE_REG);
  919. return !!(val & BIT(offset));
  920. }
  921. static void sc16is7xx_gpio_set(struct gpio_chip *chip, unsigned offset, int val)
  922. {
  923. struct sc16is7xx_port *s = container_of(chip, struct sc16is7xx_port,
  924. gpio);
  925. struct uart_port *port = &s->p[0].port;
  926. sc16is7xx_port_update(port, SC16IS7XX_IOSTATE_REG, BIT(offset),
  927. val ? BIT(offset) : 0);
  928. }
  929. static int sc16is7xx_gpio_direction_input(struct gpio_chip *chip,
  930. unsigned offset)
  931. {
  932. struct sc16is7xx_port *s = container_of(chip, struct sc16is7xx_port,
  933. gpio);
  934. struct uart_port *port = &s->p[0].port;
  935. sc16is7xx_port_update(port, SC16IS7XX_IODIR_REG, BIT(offset), 0);
  936. return 0;
  937. }
  938. static int sc16is7xx_gpio_direction_output(struct gpio_chip *chip,
  939. unsigned offset, int val)
  940. {
  941. struct sc16is7xx_port *s = container_of(chip, struct sc16is7xx_port,
  942. gpio);
  943. struct uart_port *port = &s->p[0].port;
  944. sc16is7xx_port_update(port, SC16IS7XX_IOSTATE_REG, BIT(offset),
  945. val ? BIT(offset) : 0);
  946. sc16is7xx_port_update(port, SC16IS7XX_IODIR_REG, BIT(offset),
  947. BIT(offset));
  948. return 0;
  949. }
  950. #endif
  951. static int sc16is7xx_probe(struct device *dev,
  952. const struct sc16is7xx_devtype *devtype,
  953. struct regmap *regmap, int irq, unsigned long flags)
  954. {
  955. struct sched_param sched_param = { .sched_priority = MAX_RT_PRIO / 2 };
  956. unsigned long freq, *pfreq = dev_get_platdata(dev);
  957. int i, ret;
  958. struct sc16is7xx_port *s;
  959. if (IS_ERR(regmap))
  960. return PTR_ERR(regmap);
  961. /* Alloc port structure */
  962. s = devm_kzalloc(dev, sizeof(*s) +
  963. sizeof(struct sc16is7xx_one) * devtype->nr_uart,
  964. GFP_KERNEL);
  965. if (!s) {
  966. dev_err(dev, "Error allocating port structure\n");
  967. return -ENOMEM;
  968. }
  969. s->clk = devm_clk_get(dev, NULL);
  970. if (IS_ERR(s->clk)) {
  971. if (pfreq)
  972. freq = *pfreq;
  973. else
  974. return PTR_ERR(s->clk);
  975. } else {
  976. clk_prepare_enable(s->clk);
  977. freq = clk_get_rate(s->clk);
  978. }
  979. s->regmap = regmap;
  980. s->devtype = devtype;
  981. dev_set_drvdata(dev, s);
  982. init_kthread_worker(&s->kworker);
  983. init_kthread_work(&s->irq_work, sc16is7xx_ist);
  984. s->kworker_task = kthread_run(kthread_worker_fn, &s->kworker,
  985. "sc16is7xx");
  986. if (IS_ERR(s->kworker_task)) {
  987. ret = PTR_ERR(s->kworker_task);
  988. goto out_clk;
  989. }
  990. sched_setscheduler(s->kworker_task, SCHED_FIFO, &sched_param);
  991. #ifdef CONFIG_GPIOLIB
  992. if (devtype->nr_gpio) {
  993. /* Setup GPIO cotroller */
  994. s->gpio.owner = THIS_MODULE;
  995. s->gpio.dev = dev;
  996. s->gpio.label = dev_name(dev);
  997. s->gpio.direction_input = sc16is7xx_gpio_direction_input;
  998. s->gpio.get = sc16is7xx_gpio_get;
  999. s->gpio.direction_output = sc16is7xx_gpio_direction_output;
  1000. s->gpio.set = sc16is7xx_gpio_set;
  1001. s->gpio.base = -1;
  1002. s->gpio.ngpio = devtype->nr_gpio;
  1003. s->gpio.can_sleep = 1;
  1004. ret = gpiochip_add(&s->gpio);
  1005. if (ret)
  1006. goto out_thread;
  1007. }
  1008. #endif
  1009. for (i = 0; i < devtype->nr_uart; ++i) {
  1010. s->p[i].line = i;
  1011. /* Initialize port data */
  1012. s->p[i].port.dev = dev;
  1013. s->p[i].port.irq = irq;
  1014. s->p[i].port.type = PORT_SC16IS7XX;
  1015. s->p[i].port.fifosize = SC16IS7XX_FIFO_SIZE;
  1016. s->p[i].port.flags = UPF_FIXED_TYPE | UPF_LOW_LATENCY;
  1017. s->p[i].port.iotype = UPIO_PORT;
  1018. s->p[i].port.uartclk = freq;
  1019. s->p[i].port.rs485_config = sc16is7xx_config_rs485;
  1020. s->p[i].port.ops = &sc16is7xx_ops;
  1021. s->p[i].port.line = sc16is7xx_alloc_line();
  1022. if (s->p[i].port.line >= SC16IS7XX_MAX_DEVS) {
  1023. ret = -ENOMEM;
  1024. goto out_ports;
  1025. }
  1026. /* Disable all interrupts */
  1027. sc16is7xx_port_write(&s->p[i].port, SC16IS7XX_IER_REG, 0);
  1028. /* Disable TX/RX */
  1029. sc16is7xx_port_write(&s->p[i].port, SC16IS7XX_EFCR_REG,
  1030. SC16IS7XX_EFCR_RXDISABLE_BIT |
  1031. SC16IS7XX_EFCR_TXDISABLE_BIT);
  1032. /* Initialize kthread work structs */
  1033. init_kthread_work(&s->p[i].tx_work, sc16is7xx_tx_proc);
  1034. init_kthread_work(&s->p[i].reg_work, sc16is7xx_reg_proc);
  1035. /* Register port */
  1036. uart_add_one_port(&sc16is7xx_uart, &s->p[i].port);
  1037. /* Go to suspend mode */
  1038. sc16is7xx_power(&s->p[i].port, 0);
  1039. }
  1040. /* Setup interrupt */
  1041. ret = devm_request_irq(dev, irq, sc16is7xx_irq,
  1042. IRQF_ONESHOT | flags, dev_name(dev), s);
  1043. if (!ret)
  1044. return 0;
  1045. out_ports:
  1046. for (i--; i >= 0; i--) {
  1047. uart_remove_one_port(&sc16is7xx_uart, &s->p[i].port);
  1048. clear_bit(s->p[i].port.line, &sc16is7xx_lines);
  1049. }
  1050. #ifdef CONFIG_GPIOLIB
  1051. if (devtype->nr_gpio)
  1052. gpiochip_remove(&s->gpio);
  1053. out_thread:
  1054. #endif
  1055. kthread_stop(s->kworker_task);
  1056. out_clk:
  1057. if (!IS_ERR(s->clk))
  1058. clk_disable_unprepare(s->clk);
  1059. return ret;
  1060. }
  1061. static int sc16is7xx_remove(struct device *dev)
  1062. {
  1063. struct sc16is7xx_port *s = dev_get_drvdata(dev);
  1064. int i;
  1065. #ifdef CONFIG_GPIOLIB
  1066. if (s->devtype->nr_gpio)
  1067. gpiochip_remove(&s->gpio);
  1068. #endif
  1069. for (i = 0; i < s->devtype->nr_uart; i++) {
  1070. uart_remove_one_port(&sc16is7xx_uart, &s->p[i].port);
  1071. clear_bit(s->p[i].port.line, &sc16is7xx_lines);
  1072. sc16is7xx_power(&s->p[i].port, 0);
  1073. }
  1074. flush_kthread_worker(&s->kworker);
  1075. kthread_stop(s->kworker_task);
  1076. if (!IS_ERR(s->clk))
  1077. clk_disable_unprepare(s->clk);
  1078. return 0;
  1079. }
  1080. static const struct of_device_id __maybe_unused sc16is7xx_dt_ids[] = {
  1081. { .compatible = "nxp,sc16is740", .data = &sc16is74x_devtype, },
  1082. { .compatible = "nxp,sc16is741", .data = &sc16is74x_devtype, },
  1083. { .compatible = "nxp,sc16is750", .data = &sc16is750_devtype, },
  1084. { .compatible = "nxp,sc16is752", .data = &sc16is752_devtype, },
  1085. { .compatible = "nxp,sc16is760", .data = &sc16is760_devtype, },
  1086. { .compatible = "nxp,sc16is762", .data = &sc16is762_devtype, },
  1087. { }
  1088. };
  1089. MODULE_DEVICE_TABLE(of, sc16is7xx_dt_ids);
  1090. static struct regmap_config regcfg = {
  1091. .reg_bits = 7,
  1092. .pad_bits = 1,
  1093. .val_bits = 8,
  1094. .cache_type = REGCACHE_RBTREE,
  1095. .volatile_reg = sc16is7xx_regmap_volatile,
  1096. .precious_reg = sc16is7xx_regmap_precious,
  1097. };
  1098. #ifdef CONFIG_SERIAL_SC16IS7XX_SPI
  1099. static int sc16is7xx_spi_probe(struct spi_device *spi)
  1100. {
  1101. const struct sc16is7xx_devtype *devtype;
  1102. unsigned long flags = 0;
  1103. struct regmap *regmap;
  1104. int ret;
  1105. /* Setup SPI bus */
  1106. spi->bits_per_word = 8;
  1107. /* only supports mode 0 on SC16IS762 */
  1108. spi->mode = spi->mode ? : SPI_MODE_0;
  1109. spi->max_speed_hz = spi->max_speed_hz ? : 15000000;
  1110. ret = spi_setup(spi);
  1111. if (ret)
  1112. return ret;
  1113. if (spi->dev.of_node) {
  1114. const struct of_device_id *of_id =
  1115. of_match_device(sc16is7xx_dt_ids, &spi->dev);
  1116. devtype = (struct sc16is7xx_devtype *)of_id->data;
  1117. } else {
  1118. const struct spi_device_id *id_entry = spi_get_device_id(spi);
  1119. devtype = (struct sc16is7xx_devtype *)id_entry->driver_data;
  1120. flags = IRQF_TRIGGER_FALLING;
  1121. }
  1122. regcfg.max_register = (0xf << SC16IS7XX_REG_SHIFT) |
  1123. (devtype->nr_uart - 1);
  1124. regmap = devm_regmap_init_spi(spi, &regcfg);
  1125. return sc16is7xx_probe(&spi->dev, devtype, regmap, spi->irq, flags);
  1126. }
  1127. static int sc16is7xx_spi_remove(struct spi_device *spi)
  1128. {
  1129. return sc16is7xx_remove(&spi->dev);
  1130. }
  1131. static const struct spi_device_id sc16is7xx_spi_id_table[] = {
  1132. { "sc16is74x", (kernel_ulong_t)&sc16is74x_devtype, },
  1133. { "sc16is740", (kernel_ulong_t)&sc16is74x_devtype, },
  1134. { "sc16is741", (kernel_ulong_t)&sc16is74x_devtype, },
  1135. { "sc16is750", (kernel_ulong_t)&sc16is750_devtype, },
  1136. { "sc16is752", (kernel_ulong_t)&sc16is752_devtype, },
  1137. { "sc16is760", (kernel_ulong_t)&sc16is760_devtype, },
  1138. { "sc16is762", (kernel_ulong_t)&sc16is762_devtype, },
  1139. { }
  1140. };
  1141. MODULE_DEVICE_TABLE(spi, sc16is7xx_spi_id_table);
  1142. static struct spi_driver sc16is7xx_spi_uart_driver = {
  1143. .driver = {
  1144. .name = SC16IS7XX_NAME,
  1145. .owner = THIS_MODULE,
  1146. .of_match_table = of_match_ptr(sc16is7xx_dt_ids),
  1147. },
  1148. .probe = sc16is7xx_spi_probe,
  1149. .remove = sc16is7xx_spi_remove,
  1150. .id_table = sc16is7xx_spi_id_table,
  1151. };
  1152. MODULE_ALIAS("spi:sc16is7xx");
  1153. #endif
  1154. #ifdef CONFIG_SERIAL_SC16IS7XX_I2C
  1155. static int sc16is7xx_i2c_probe(struct i2c_client *i2c,
  1156. const struct i2c_device_id *id)
  1157. {
  1158. const struct sc16is7xx_devtype *devtype;
  1159. unsigned long flags = 0;
  1160. struct regmap *regmap;
  1161. if (i2c->dev.of_node) {
  1162. const struct of_device_id *of_id =
  1163. of_match_device(sc16is7xx_dt_ids, &i2c->dev);
  1164. devtype = (struct sc16is7xx_devtype *)of_id->data;
  1165. } else {
  1166. devtype = (struct sc16is7xx_devtype *)id->driver_data;
  1167. flags = IRQF_TRIGGER_FALLING;
  1168. }
  1169. regcfg.max_register = (0xf << SC16IS7XX_REG_SHIFT) |
  1170. (devtype->nr_uart - 1);
  1171. regmap = devm_regmap_init_i2c(i2c, &regcfg);
  1172. return sc16is7xx_probe(&i2c->dev, devtype, regmap, i2c->irq, flags);
  1173. }
  1174. static int sc16is7xx_i2c_remove(struct i2c_client *client)
  1175. {
  1176. return sc16is7xx_remove(&client->dev);
  1177. }
  1178. static const struct i2c_device_id sc16is7xx_i2c_id_table[] = {
  1179. { "sc16is74x", (kernel_ulong_t)&sc16is74x_devtype, },
  1180. { "sc16is740", (kernel_ulong_t)&sc16is74x_devtype, },
  1181. { "sc16is741", (kernel_ulong_t)&sc16is74x_devtype, },
  1182. { "sc16is750", (kernel_ulong_t)&sc16is750_devtype, },
  1183. { "sc16is752", (kernel_ulong_t)&sc16is752_devtype, },
  1184. { "sc16is760", (kernel_ulong_t)&sc16is760_devtype, },
  1185. { "sc16is762", (kernel_ulong_t)&sc16is762_devtype, },
  1186. { }
  1187. };
  1188. MODULE_DEVICE_TABLE(i2c, sc16is7xx_i2c_id_table);
  1189. static struct i2c_driver sc16is7xx_i2c_uart_driver = {
  1190. .driver = {
  1191. .name = SC16IS7XX_NAME,
  1192. .of_match_table = of_match_ptr(sc16is7xx_dt_ids),
  1193. },
  1194. .probe = sc16is7xx_i2c_probe,
  1195. .remove = sc16is7xx_i2c_remove,
  1196. .id_table = sc16is7xx_i2c_id_table,
  1197. };
  1198. MODULE_ALIAS("i2c:sc16is7xx");
  1199. #endif
  1200. static int __init sc16is7xx_init(void)
  1201. {
  1202. int ret;
  1203. ret = uart_register_driver(&sc16is7xx_uart);
  1204. if (ret) {
  1205. pr_err("Registering UART driver failed\n");
  1206. return ret;
  1207. }
  1208. #ifdef CONFIG_SERIAL_SC16IS7XX_I2C
  1209. ret = i2c_add_driver(&sc16is7xx_i2c_uart_driver);
  1210. if (ret < 0) {
  1211. pr_err("failed to init sc16is7xx i2c --> %d\n", ret);
  1212. return ret;
  1213. }
  1214. #endif
  1215. #ifdef CONFIG_SERIAL_SC16IS7XX_SPI
  1216. ret = spi_register_driver(&sc16is7xx_spi_uart_driver);
  1217. if (ret < 0) {
  1218. pr_err("failed to init sc16is7xx spi --> %d\n", ret);
  1219. return ret;
  1220. }
  1221. #endif
  1222. return ret;
  1223. }
  1224. module_init(sc16is7xx_init);
  1225. static void __exit sc16is7xx_exit(void)
  1226. {
  1227. #ifdef CONFIG_SERIAL_SC16IS7XX_I2C
  1228. i2c_del_driver(&sc16is7xx_i2c_uart_driver);
  1229. #endif
  1230. #ifdef CONFIG_SERIAL_SC16IS7XX_SPI
  1231. spi_unregister_driver(&sc16is7xx_spi_uart_driver);
  1232. #endif
  1233. uart_unregister_driver(&sc16is7xx_uart);
  1234. }
  1235. module_exit(sc16is7xx_exit);
  1236. MODULE_LICENSE("GPL");
  1237. MODULE_AUTHOR("Jon Ringle <jringle@gridpoint.com>");
  1238. MODULE_DESCRIPTION("SC16IS7XX serial driver");