sc16is7xx.c 43 KB

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