sc16is7xx.c 44 KB

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