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