sc16is7xx.c 43 KB

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