sc16is7xx.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552
  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 mutex efr_lock;
  306. struct sc16is7xx_one p[0];
  307. };
  308. static unsigned long sc16is7xx_lines;
  309. static struct uart_driver sc16is7xx_uart = {
  310. .owner = THIS_MODULE,
  311. .dev_name = "ttySC",
  312. .nr = SC16IS7XX_MAX_DEVS,
  313. };
  314. #define to_sc16is7xx_port(p,e) ((container_of((p), struct sc16is7xx_port, e)))
  315. #define to_sc16is7xx_one(p,e) ((container_of((p), struct sc16is7xx_one, e)))
  316. static int sc16is7xx_line(struct uart_port *port)
  317. {
  318. struct sc16is7xx_one *one = to_sc16is7xx_one(port, port);
  319. return one->line;
  320. }
  321. static u8 sc16is7xx_port_read(struct uart_port *port, u8 reg)
  322. {
  323. struct sc16is7xx_port *s = dev_get_drvdata(port->dev);
  324. unsigned int val = 0;
  325. const u8 line = sc16is7xx_line(port);
  326. regmap_read(s->regmap, (reg << SC16IS7XX_REG_SHIFT) | line, &val);
  327. return val;
  328. }
  329. static void sc16is7xx_port_write(struct uart_port *port, u8 reg, u8 val)
  330. {
  331. struct sc16is7xx_port *s = dev_get_drvdata(port->dev);
  332. const u8 line = sc16is7xx_line(port);
  333. regmap_write(s->regmap, (reg << SC16IS7XX_REG_SHIFT) | line, val);
  334. }
  335. static void sc16is7xx_fifo_read(struct uart_port *port, unsigned int rxlen)
  336. {
  337. struct sc16is7xx_port *s = dev_get_drvdata(port->dev);
  338. const u8 line = sc16is7xx_line(port);
  339. u8 addr = (SC16IS7XX_RHR_REG << SC16IS7XX_REG_SHIFT) | line;
  340. regcache_cache_bypass(s->regmap, true);
  341. regmap_raw_read(s->regmap, addr, s->buf, rxlen);
  342. regcache_cache_bypass(s->regmap, false);
  343. }
  344. static void sc16is7xx_fifo_write(struct uart_port *port, u8 to_send)
  345. {
  346. struct sc16is7xx_port *s = dev_get_drvdata(port->dev);
  347. const u8 line = sc16is7xx_line(port);
  348. u8 addr = (SC16IS7XX_THR_REG << SC16IS7XX_REG_SHIFT) | line;
  349. /*
  350. * Don't send zero-length data, at least on SPI it confuses the chip
  351. * delivering wrong TXLVL data.
  352. */
  353. if (unlikely(!to_send))
  354. return;
  355. regcache_cache_bypass(s->regmap, true);
  356. regmap_raw_write(s->regmap, addr, s->buf, to_send);
  357. regcache_cache_bypass(s->regmap, false);
  358. }
  359. static void sc16is7xx_port_update(struct uart_port *port, u8 reg,
  360. u8 mask, u8 val)
  361. {
  362. struct sc16is7xx_port *s = dev_get_drvdata(port->dev);
  363. const u8 line = sc16is7xx_line(port);
  364. regmap_update_bits(s->regmap, (reg << SC16IS7XX_REG_SHIFT) | line,
  365. mask, val);
  366. }
  367. static int sc16is7xx_alloc_line(void)
  368. {
  369. int i;
  370. BUILD_BUG_ON(SC16IS7XX_MAX_DEVS > BITS_PER_LONG);
  371. for (i = 0; i < SC16IS7XX_MAX_DEVS; i++)
  372. if (!test_and_set_bit(i, &sc16is7xx_lines))
  373. break;
  374. return i;
  375. }
  376. static void sc16is7xx_power(struct uart_port *port, int on)
  377. {
  378. sc16is7xx_port_update(port, SC16IS7XX_IER_REG,
  379. SC16IS7XX_IER_SLEEP_BIT,
  380. on ? 0 : SC16IS7XX_IER_SLEEP_BIT);
  381. }
  382. static const struct sc16is7xx_devtype sc16is74x_devtype = {
  383. .name = "SC16IS74X",
  384. .nr_gpio = 0,
  385. .nr_uart = 1,
  386. };
  387. static const struct sc16is7xx_devtype sc16is750_devtype = {
  388. .name = "SC16IS750",
  389. .nr_gpio = 8,
  390. .nr_uart = 1,
  391. };
  392. static const struct sc16is7xx_devtype sc16is752_devtype = {
  393. .name = "SC16IS752",
  394. .nr_gpio = 8,
  395. .nr_uart = 2,
  396. };
  397. static const struct sc16is7xx_devtype sc16is760_devtype = {
  398. .name = "SC16IS760",
  399. .nr_gpio = 8,
  400. .nr_uart = 1,
  401. };
  402. static const struct sc16is7xx_devtype sc16is762_devtype = {
  403. .name = "SC16IS762",
  404. .nr_gpio = 8,
  405. .nr_uart = 2,
  406. };
  407. static bool sc16is7xx_regmap_volatile(struct device *dev, unsigned int reg)
  408. {
  409. switch (reg >> SC16IS7XX_REG_SHIFT) {
  410. case SC16IS7XX_RHR_REG:
  411. case SC16IS7XX_IIR_REG:
  412. case SC16IS7XX_LSR_REG:
  413. case SC16IS7XX_MSR_REG:
  414. case SC16IS7XX_TXLVL_REG:
  415. case SC16IS7XX_RXLVL_REG:
  416. case SC16IS7XX_IOSTATE_REG:
  417. return true;
  418. default:
  419. break;
  420. }
  421. return false;
  422. }
  423. static bool sc16is7xx_regmap_precious(struct device *dev, unsigned int reg)
  424. {
  425. switch (reg >> SC16IS7XX_REG_SHIFT) {
  426. case SC16IS7XX_RHR_REG:
  427. return true;
  428. default:
  429. break;
  430. }
  431. return false;
  432. }
  433. static int sc16is7xx_set_baud(struct uart_port *port, int baud)
  434. {
  435. struct sc16is7xx_port *s = dev_get_drvdata(port->dev);
  436. u8 lcr;
  437. u8 prescaler = 0;
  438. unsigned long clk = port->uartclk, div = clk / 16 / baud;
  439. if (div > 0xffff) {
  440. prescaler = SC16IS7XX_MCR_CLKSEL_BIT;
  441. div /= 4;
  442. }
  443. /* In an amazing feat of design, the Enhanced Features Register shares
  444. * the address of the Interrupt Identification Register, and is
  445. * switched in by writing a magic value (0xbf) to the Line Control
  446. * Register. Any interrupt firing during this time will see the EFR
  447. * where it expects the IIR to be, leading to "Unexpected interrupt"
  448. * messages.
  449. *
  450. * Prevent this possibility by claiming a mutex while accessing the
  451. * EFR, and claiming the same mutex from within the interrupt handler.
  452. * This is similar to disabling the interrupt, but that doesn't work
  453. * because the bulk of the interrupt processing is run as a workqueue
  454. * job in thread context.
  455. */
  456. mutex_lock(&s->efr_lock);
  457. lcr = sc16is7xx_port_read(port, SC16IS7XX_LCR_REG);
  458. /* Open the LCR divisors for configuration */
  459. sc16is7xx_port_write(port, SC16IS7XX_LCR_REG,
  460. SC16IS7XX_LCR_CONF_MODE_B);
  461. /* Enable enhanced features */
  462. regcache_cache_bypass(s->regmap, true);
  463. sc16is7xx_port_write(port, SC16IS7XX_EFR_REG,
  464. SC16IS7XX_EFR_ENABLE_BIT);
  465. regcache_cache_bypass(s->regmap, false);
  466. /* Put LCR back to the normal mode */
  467. sc16is7xx_port_write(port, SC16IS7XX_LCR_REG, lcr);
  468. mutex_unlock(&s->efr_lock);
  469. sc16is7xx_port_update(port, SC16IS7XX_MCR_REG,
  470. SC16IS7XX_MCR_CLKSEL_BIT,
  471. prescaler);
  472. /* Open the LCR divisors for configuration */
  473. sc16is7xx_port_write(port, SC16IS7XX_LCR_REG,
  474. SC16IS7XX_LCR_CONF_MODE_A);
  475. /* Write the new divisor */
  476. regcache_cache_bypass(s->regmap, true);
  477. sc16is7xx_port_write(port, SC16IS7XX_DLH_REG, div / 256);
  478. sc16is7xx_port_write(port, SC16IS7XX_DLL_REG, div % 256);
  479. regcache_cache_bypass(s->regmap, false);
  480. /* Put LCR back to the normal mode */
  481. sc16is7xx_port_write(port, SC16IS7XX_LCR_REG, lcr);
  482. return DIV_ROUND_CLOSEST(clk / 16, div);
  483. }
  484. static void sc16is7xx_handle_rx(struct uart_port *port, unsigned int rxlen,
  485. unsigned int iir)
  486. {
  487. struct sc16is7xx_port *s = dev_get_drvdata(port->dev);
  488. unsigned int lsr = 0, ch, flag, bytes_read, i;
  489. bool read_lsr = (iir == SC16IS7XX_IIR_RLSE_SRC) ? true : false;
  490. if (unlikely(rxlen >= sizeof(s->buf))) {
  491. dev_warn_ratelimited(port->dev,
  492. "ttySC%i: Possible RX FIFO overrun: %d\n",
  493. port->line, rxlen);
  494. port->icount.buf_overrun++;
  495. /* Ensure sanity of RX level */
  496. rxlen = sizeof(s->buf);
  497. }
  498. while (rxlen) {
  499. /* Only read lsr if there are possible errors in FIFO */
  500. if (read_lsr) {
  501. lsr = sc16is7xx_port_read(port, SC16IS7XX_LSR_REG);
  502. if (!(lsr & SC16IS7XX_LSR_FIFOE_BIT))
  503. read_lsr = false; /* No errors left in FIFO */
  504. } else
  505. lsr = 0;
  506. if (read_lsr) {
  507. s->buf[0] = sc16is7xx_port_read(port, SC16IS7XX_RHR_REG);
  508. bytes_read = 1;
  509. } else {
  510. sc16is7xx_fifo_read(port, rxlen);
  511. bytes_read = rxlen;
  512. }
  513. lsr &= SC16IS7XX_LSR_BRK_ERROR_MASK;
  514. port->icount.rx++;
  515. flag = TTY_NORMAL;
  516. if (unlikely(lsr)) {
  517. if (lsr & SC16IS7XX_LSR_BI_BIT) {
  518. port->icount.brk++;
  519. if (uart_handle_break(port))
  520. continue;
  521. } else if (lsr & SC16IS7XX_LSR_PE_BIT)
  522. port->icount.parity++;
  523. else if (lsr & SC16IS7XX_LSR_FE_BIT)
  524. port->icount.frame++;
  525. else if (lsr & SC16IS7XX_LSR_OE_BIT)
  526. port->icount.overrun++;
  527. lsr &= port->read_status_mask;
  528. if (lsr & SC16IS7XX_LSR_BI_BIT)
  529. flag = TTY_BREAK;
  530. else if (lsr & SC16IS7XX_LSR_PE_BIT)
  531. flag = TTY_PARITY;
  532. else if (lsr & SC16IS7XX_LSR_FE_BIT)
  533. flag = TTY_FRAME;
  534. else if (lsr & SC16IS7XX_LSR_OE_BIT)
  535. flag = TTY_OVERRUN;
  536. }
  537. for (i = 0; i < bytes_read; ++i) {
  538. ch = s->buf[i];
  539. if (uart_handle_sysrq_char(port, ch))
  540. continue;
  541. if (lsr & port->ignore_status_mask)
  542. continue;
  543. uart_insert_char(port, lsr, SC16IS7XX_LSR_OE_BIT, ch,
  544. flag);
  545. }
  546. rxlen -= bytes_read;
  547. }
  548. tty_flip_buffer_push(&port->state->port);
  549. }
  550. static void sc16is7xx_handle_tx(struct uart_port *port)
  551. {
  552. struct sc16is7xx_port *s = dev_get_drvdata(port->dev);
  553. struct circ_buf *xmit = &port->state->xmit;
  554. unsigned int txlen, to_send, i;
  555. if (unlikely(port->x_char)) {
  556. sc16is7xx_port_write(port, SC16IS7XX_THR_REG, port->x_char);
  557. port->icount.tx++;
  558. port->x_char = 0;
  559. return;
  560. }
  561. if (uart_circ_empty(xmit) || uart_tx_stopped(port))
  562. return;
  563. /* Get length of data pending in circular buffer */
  564. to_send = uart_circ_chars_pending(xmit);
  565. if (likely(to_send)) {
  566. /* Limit to size of TX FIFO */
  567. txlen = sc16is7xx_port_read(port, SC16IS7XX_TXLVL_REG);
  568. if (txlen > SC16IS7XX_FIFO_SIZE) {
  569. dev_err_ratelimited(port->dev,
  570. "chip reports %d free bytes in TX fifo, but it only has %d",
  571. txlen, SC16IS7XX_FIFO_SIZE);
  572. txlen = 0;
  573. }
  574. to_send = (to_send > txlen) ? txlen : to_send;
  575. /* Add data to send */
  576. port->icount.tx += to_send;
  577. /* Convert to linear buffer */
  578. for (i = 0; i < to_send; ++i) {
  579. s->buf[i] = xmit->buf[xmit->tail];
  580. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  581. }
  582. sc16is7xx_fifo_write(port, to_send);
  583. }
  584. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  585. uart_write_wakeup(port);
  586. }
  587. static bool sc16is7xx_port_irq(struct sc16is7xx_port *s, int portno)
  588. {
  589. struct uart_port *port = &s->p[portno].port;
  590. do {
  591. unsigned int iir, rxlen;
  592. iir = sc16is7xx_port_read(port, SC16IS7XX_IIR_REG);
  593. if (iir & SC16IS7XX_IIR_NO_INT_BIT)
  594. return false;
  595. iir &= SC16IS7XX_IIR_ID_MASK;
  596. switch (iir) {
  597. case SC16IS7XX_IIR_RDI_SRC:
  598. case SC16IS7XX_IIR_RLSE_SRC:
  599. case SC16IS7XX_IIR_RTOI_SRC:
  600. case SC16IS7XX_IIR_XOFFI_SRC:
  601. rxlen = sc16is7xx_port_read(port, SC16IS7XX_RXLVL_REG);
  602. if (rxlen)
  603. sc16is7xx_handle_rx(port, rxlen, iir);
  604. break;
  605. case SC16IS7XX_IIR_THRI_SRC:
  606. sc16is7xx_handle_tx(port);
  607. break;
  608. default:
  609. dev_err_ratelimited(port->dev,
  610. "ttySC%i: Unexpected interrupt: %x",
  611. port->line, iir);
  612. break;
  613. }
  614. } while (0);
  615. return true;
  616. }
  617. static void sc16is7xx_ist(struct kthread_work *ws)
  618. {
  619. struct sc16is7xx_port *s = to_sc16is7xx_port(ws, irq_work);
  620. mutex_lock(&s->efr_lock);
  621. while (1) {
  622. bool keep_polling = false;
  623. int i;
  624. for (i = 0; i < s->devtype->nr_uart; ++i)
  625. keep_polling |= sc16is7xx_port_irq(s, i);
  626. if (!keep_polling)
  627. break;
  628. }
  629. mutex_unlock(&s->efr_lock);
  630. }
  631. static irqreturn_t sc16is7xx_irq(int irq, void *dev_id)
  632. {
  633. struct sc16is7xx_port *s = (struct sc16is7xx_port *)dev_id;
  634. kthread_queue_work(&s->kworker, &s->irq_work);
  635. return IRQ_HANDLED;
  636. }
  637. static void sc16is7xx_tx_proc(struct kthread_work *ws)
  638. {
  639. struct uart_port *port = &(to_sc16is7xx_one(ws, tx_work)->port);
  640. if ((port->rs485.flags & SER_RS485_ENABLED) &&
  641. (port->rs485.delay_rts_before_send > 0))
  642. msleep(port->rs485.delay_rts_before_send);
  643. sc16is7xx_handle_tx(port);
  644. }
  645. static void sc16is7xx_reconf_rs485(struct uart_port *port)
  646. {
  647. const u32 mask = SC16IS7XX_EFCR_AUTO_RS485_BIT |
  648. SC16IS7XX_EFCR_RTS_INVERT_BIT;
  649. u32 efcr = 0;
  650. struct serial_rs485 *rs485 = &port->rs485;
  651. unsigned long irqflags;
  652. spin_lock_irqsave(&port->lock, irqflags);
  653. if (rs485->flags & SER_RS485_ENABLED) {
  654. efcr |= SC16IS7XX_EFCR_AUTO_RS485_BIT;
  655. if (rs485->flags & SER_RS485_RTS_AFTER_SEND)
  656. efcr |= SC16IS7XX_EFCR_RTS_INVERT_BIT;
  657. }
  658. spin_unlock_irqrestore(&port->lock, irqflags);
  659. sc16is7xx_port_update(port, SC16IS7XX_EFCR_REG, mask, efcr);
  660. }
  661. static void sc16is7xx_reg_proc(struct kthread_work *ws)
  662. {
  663. struct sc16is7xx_one *one = to_sc16is7xx_one(ws, reg_work);
  664. struct sc16is7xx_one_config config;
  665. unsigned long irqflags;
  666. spin_lock_irqsave(&one->port.lock, irqflags);
  667. config = one->config;
  668. memset(&one->config, 0, sizeof(one->config));
  669. spin_unlock_irqrestore(&one->port.lock, irqflags);
  670. if (config.flags & SC16IS7XX_RECONF_MD) {
  671. sc16is7xx_port_update(&one->port, SC16IS7XX_MCR_REG,
  672. SC16IS7XX_MCR_LOOP_BIT,
  673. (one->port.mctrl & TIOCM_LOOP) ?
  674. SC16IS7XX_MCR_LOOP_BIT : 0);
  675. sc16is7xx_port_update(&one->port, SC16IS7XX_MCR_REG,
  676. SC16IS7XX_MCR_RTS_BIT,
  677. (one->port.mctrl & TIOCM_RTS) ?
  678. SC16IS7XX_MCR_RTS_BIT : 0);
  679. sc16is7xx_port_update(&one->port, SC16IS7XX_MCR_REG,
  680. SC16IS7XX_MCR_DTR_BIT,
  681. (one->port.mctrl & TIOCM_DTR) ?
  682. SC16IS7XX_MCR_DTR_BIT : 0);
  683. }
  684. if (config.flags & SC16IS7XX_RECONF_IER)
  685. sc16is7xx_port_update(&one->port, SC16IS7XX_IER_REG,
  686. config.ier_clear, 0);
  687. if (config.flags & SC16IS7XX_RECONF_RS485)
  688. sc16is7xx_reconf_rs485(&one->port);
  689. }
  690. static void sc16is7xx_ier_clear(struct uart_port *port, u8 bit)
  691. {
  692. struct sc16is7xx_port *s = dev_get_drvdata(port->dev);
  693. struct sc16is7xx_one *one = to_sc16is7xx_one(port, port);
  694. one->config.flags |= SC16IS7XX_RECONF_IER;
  695. one->config.ier_clear |= bit;
  696. kthread_queue_work(&s->kworker, &one->reg_work);
  697. }
  698. static void sc16is7xx_stop_tx(struct uart_port *port)
  699. {
  700. sc16is7xx_ier_clear(port, SC16IS7XX_IER_THRI_BIT);
  701. }
  702. static void sc16is7xx_stop_rx(struct uart_port *port)
  703. {
  704. sc16is7xx_ier_clear(port, SC16IS7XX_IER_RDI_BIT);
  705. }
  706. static void sc16is7xx_start_tx(struct uart_port *port)
  707. {
  708. struct sc16is7xx_port *s = dev_get_drvdata(port->dev);
  709. struct sc16is7xx_one *one = to_sc16is7xx_one(port, port);
  710. kthread_queue_work(&s->kworker, &one->tx_work);
  711. }
  712. static unsigned int sc16is7xx_tx_empty(struct uart_port *port)
  713. {
  714. unsigned int lsr;
  715. lsr = sc16is7xx_port_read(port, SC16IS7XX_LSR_REG);
  716. return (lsr & SC16IS7XX_LSR_TEMT_BIT) ? TIOCSER_TEMT : 0;
  717. }
  718. static unsigned int sc16is7xx_get_mctrl(struct uart_port *port)
  719. {
  720. /* DCD and DSR are not wired and CTS/RTS is handled automatically
  721. * so just indicate DSR and CAR asserted
  722. */
  723. return TIOCM_DSR | TIOCM_CAR;
  724. }
  725. static void sc16is7xx_set_mctrl(struct uart_port *port, unsigned int mctrl)
  726. {
  727. struct sc16is7xx_port *s = dev_get_drvdata(port->dev);
  728. struct sc16is7xx_one *one = to_sc16is7xx_one(port, port);
  729. one->config.flags |= SC16IS7XX_RECONF_MD;
  730. kthread_queue_work(&s->kworker, &one->reg_work);
  731. }
  732. static void sc16is7xx_break_ctl(struct uart_port *port, int break_state)
  733. {
  734. sc16is7xx_port_update(port, SC16IS7XX_LCR_REG,
  735. SC16IS7XX_LCR_TXBREAK_BIT,
  736. break_state ? SC16IS7XX_LCR_TXBREAK_BIT : 0);
  737. }
  738. static void sc16is7xx_set_termios(struct uart_port *port,
  739. struct ktermios *termios,
  740. struct ktermios *old)
  741. {
  742. struct sc16is7xx_port *s = dev_get_drvdata(port->dev);
  743. unsigned int lcr, flow = 0;
  744. int baud;
  745. /* Mask termios capabilities we don't support */
  746. termios->c_cflag &= ~CMSPAR;
  747. /* Word size */
  748. switch (termios->c_cflag & CSIZE) {
  749. case CS5:
  750. lcr = SC16IS7XX_LCR_WORD_LEN_5;
  751. break;
  752. case CS6:
  753. lcr = SC16IS7XX_LCR_WORD_LEN_6;
  754. break;
  755. case CS7:
  756. lcr = SC16IS7XX_LCR_WORD_LEN_7;
  757. break;
  758. case CS8:
  759. lcr = SC16IS7XX_LCR_WORD_LEN_8;
  760. break;
  761. default:
  762. lcr = SC16IS7XX_LCR_WORD_LEN_8;
  763. termios->c_cflag &= ~CSIZE;
  764. termios->c_cflag |= CS8;
  765. break;
  766. }
  767. /* Parity */
  768. if (termios->c_cflag & PARENB) {
  769. lcr |= SC16IS7XX_LCR_PARITY_BIT;
  770. if (!(termios->c_cflag & PARODD))
  771. lcr |= SC16IS7XX_LCR_EVENPARITY_BIT;
  772. }
  773. /* Stop bits */
  774. if (termios->c_cflag & CSTOPB)
  775. lcr |= SC16IS7XX_LCR_STOPLEN_BIT; /* 2 stops */
  776. /* Set read status mask */
  777. port->read_status_mask = SC16IS7XX_LSR_OE_BIT;
  778. if (termios->c_iflag & INPCK)
  779. port->read_status_mask |= SC16IS7XX_LSR_PE_BIT |
  780. SC16IS7XX_LSR_FE_BIT;
  781. if (termios->c_iflag & (BRKINT | PARMRK))
  782. port->read_status_mask |= SC16IS7XX_LSR_BI_BIT;
  783. /* Set status ignore mask */
  784. port->ignore_status_mask = 0;
  785. if (termios->c_iflag & IGNBRK)
  786. port->ignore_status_mask |= SC16IS7XX_LSR_BI_BIT;
  787. if (!(termios->c_cflag & CREAD))
  788. port->ignore_status_mask |= SC16IS7XX_LSR_BRK_ERROR_MASK;
  789. /* As above, claim the mutex while accessing the EFR. */
  790. mutex_lock(&s->efr_lock);
  791. sc16is7xx_port_write(port, SC16IS7XX_LCR_REG,
  792. SC16IS7XX_LCR_CONF_MODE_B);
  793. /* Configure flow control */
  794. regcache_cache_bypass(s->regmap, true);
  795. sc16is7xx_port_write(port, SC16IS7XX_XON1_REG, termios->c_cc[VSTART]);
  796. sc16is7xx_port_write(port, SC16IS7XX_XOFF1_REG, termios->c_cc[VSTOP]);
  797. if (termios->c_cflag & CRTSCTS)
  798. flow |= SC16IS7XX_EFR_AUTOCTS_BIT |
  799. SC16IS7XX_EFR_AUTORTS_BIT;
  800. if (termios->c_iflag & IXON)
  801. flow |= SC16IS7XX_EFR_SWFLOW3_BIT;
  802. if (termios->c_iflag & IXOFF)
  803. flow |= SC16IS7XX_EFR_SWFLOW1_BIT;
  804. sc16is7xx_port_write(port, SC16IS7XX_EFR_REG, flow);
  805. regcache_cache_bypass(s->regmap, false);
  806. /* Update LCR register */
  807. sc16is7xx_port_write(port, SC16IS7XX_LCR_REG, lcr);
  808. mutex_unlock(&s->efr_lock);
  809. /* Get baud rate generator configuration */
  810. baud = uart_get_baud_rate(port, termios, old,
  811. port->uartclk / 16 / 4 / 0xffff,
  812. port->uartclk / 16);
  813. /* Setup baudrate generator */
  814. baud = sc16is7xx_set_baud(port, baud);
  815. /* Update timeout according to new baud rate */
  816. uart_update_timeout(port, termios->c_cflag, baud);
  817. }
  818. static int sc16is7xx_config_rs485(struct uart_port *port,
  819. struct serial_rs485 *rs485)
  820. {
  821. struct sc16is7xx_port *s = dev_get_drvdata(port->dev);
  822. struct sc16is7xx_one *one = to_sc16is7xx_one(port, port);
  823. if (rs485->flags & SER_RS485_ENABLED) {
  824. bool rts_during_rx, rts_during_tx;
  825. rts_during_rx = rs485->flags & SER_RS485_RTS_AFTER_SEND;
  826. rts_during_tx = rs485->flags & SER_RS485_RTS_ON_SEND;
  827. if (rts_during_rx == rts_during_tx)
  828. dev_err(port->dev,
  829. "unsupported RTS signalling on_send:%d after_send:%d - exactly one of RS485 RTS flags should be set\n",
  830. rts_during_tx, rts_during_rx);
  831. /*
  832. * RTS signal is handled by HW, it's timing can't be influenced.
  833. * However, it's sometimes useful to delay TX even without RTS
  834. * control therefore we try to handle .delay_rts_before_send.
  835. */
  836. if (rs485->delay_rts_after_send)
  837. return -EINVAL;
  838. }
  839. port->rs485 = *rs485;
  840. one->config.flags |= SC16IS7XX_RECONF_RS485;
  841. kthread_queue_work(&s->kworker, &one->reg_work);
  842. return 0;
  843. }
  844. static int sc16is7xx_startup(struct uart_port *port)
  845. {
  846. struct sc16is7xx_port *s = dev_get_drvdata(port->dev);
  847. unsigned int val;
  848. sc16is7xx_power(port, 1);
  849. /* Reset FIFOs*/
  850. val = SC16IS7XX_FCR_RXRESET_BIT | SC16IS7XX_FCR_TXRESET_BIT;
  851. sc16is7xx_port_write(port, SC16IS7XX_FCR_REG, val);
  852. udelay(5);
  853. sc16is7xx_port_write(port, SC16IS7XX_FCR_REG,
  854. SC16IS7XX_FCR_FIFO_BIT);
  855. /* Enable EFR */
  856. sc16is7xx_port_write(port, SC16IS7XX_LCR_REG,
  857. SC16IS7XX_LCR_CONF_MODE_B);
  858. regcache_cache_bypass(s->regmap, true);
  859. /* Enable write access to enhanced features and internal clock div */
  860. sc16is7xx_port_write(port, SC16IS7XX_EFR_REG,
  861. SC16IS7XX_EFR_ENABLE_BIT);
  862. /* Enable TCR/TLR */
  863. sc16is7xx_port_update(port, SC16IS7XX_MCR_REG,
  864. SC16IS7XX_MCR_TCRTLR_BIT,
  865. SC16IS7XX_MCR_TCRTLR_BIT);
  866. /* Configure flow control levels */
  867. /* Flow control halt level 48, resume level 24 */
  868. sc16is7xx_port_write(port, SC16IS7XX_TCR_REG,
  869. SC16IS7XX_TCR_RX_RESUME(24) |
  870. SC16IS7XX_TCR_RX_HALT(48));
  871. regcache_cache_bypass(s->regmap, false);
  872. /* Now, initialize the UART */
  873. sc16is7xx_port_write(port, SC16IS7XX_LCR_REG, SC16IS7XX_LCR_WORD_LEN_8);
  874. /* Enable the Rx and Tx FIFO */
  875. sc16is7xx_port_update(port, SC16IS7XX_EFCR_REG,
  876. SC16IS7XX_EFCR_RXDISABLE_BIT |
  877. SC16IS7XX_EFCR_TXDISABLE_BIT,
  878. 0);
  879. /* Enable RX, TX interrupts */
  880. val = SC16IS7XX_IER_RDI_BIT | SC16IS7XX_IER_THRI_BIT;
  881. sc16is7xx_port_write(port, SC16IS7XX_IER_REG, val);
  882. return 0;
  883. }
  884. static void sc16is7xx_shutdown(struct uart_port *port)
  885. {
  886. struct sc16is7xx_port *s = dev_get_drvdata(port->dev);
  887. /* Disable all interrupts */
  888. sc16is7xx_port_write(port, SC16IS7XX_IER_REG, 0);
  889. /* Disable TX/RX */
  890. sc16is7xx_port_update(port, SC16IS7XX_EFCR_REG,
  891. SC16IS7XX_EFCR_RXDISABLE_BIT |
  892. SC16IS7XX_EFCR_TXDISABLE_BIT,
  893. SC16IS7XX_EFCR_RXDISABLE_BIT |
  894. SC16IS7XX_EFCR_TXDISABLE_BIT);
  895. sc16is7xx_power(port, 0);
  896. kthread_flush_worker(&s->kworker);
  897. }
  898. static const char *sc16is7xx_type(struct uart_port *port)
  899. {
  900. struct sc16is7xx_port *s = dev_get_drvdata(port->dev);
  901. return (port->type == PORT_SC16IS7XX) ? s->devtype->name : NULL;
  902. }
  903. static int sc16is7xx_request_port(struct uart_port *port)
  904. {
  905. /* Do nothing */
  906. return 0;
  907. }
  908. static void sc16is7xx_config_port(struct uart_port *port, int flags)
  909. {
  910. if (flags & UART_CONFIG_TYPE)
  911. port->type = PORT_SC16IS7XX;
  912. }
  913. static int sc16is7xx_verify_port(struct uart_port *port,
  914. struct serial_struct *s)
  915. {
  916. if ((s->type != PORT_UNKNOWN) && (s->type != PORT_SC16IS7XX))
  917. return -EINVAL;
  918. if (s->irq != port->irq)
  919. return -EINVAL;
  920. return 0;
  921. }
  922. static void sc16is7xx_pm(struct uart_port *port, unsigned int state,
  923. unsigned int oldstate)
  924. {
  925. sc16is7xx_power(port, (state == UART_PM_STATE_ON) ? 1 : 0);
  926. }
  927. static void sc16is7xx_null_void(struct uart_port *port)
  928. {
  929. /* Do nothing */
  930. }
  931. static const struct uart_ops sc16is7xx_ops = {
  932. .tx_empty = sc16is7xx_tx_empty,
  933. .set_mctrl = sc16is7xx_set_mctrl,
  934. .get_mctrl = sc16is7xx_get_mctrl,
  935. .stop_tx = sc16is7xx_stop_tx,
  936. .start_tx = sc16is7xx_start_tx,
  937. .stop_rx = sc16is7xx_stop_rx,
  938. .break_ctl = sc16is7xx_break_ctl,
  939. .startup = sc16is7xx_startup,
  940. .shutdown = sc16is7xx_shutdown,
  941. .set_termios = sc16is7xx_set_termios,
  942. .type = sc16is7xx_type,
  943. .request_port = sc16is7xx_request_port,
  944. .release_port = sc16is7xx_null_void,
  945. .config_port = sc16is7xx_config_port,
  946. .verify_port = sc16is7xx_verify_port,
  947. .pm = sc16is7xx_pm,
  948. };
  949. #ifdef CONFIG_GPIOLIB
  950. static int sc16is7xx_gpio_get(struct gpio_chip *chip, unsigned offset)
  951. {
  952. unsigned int val;
  953. struct sc16is7xx_port *s = gpiochip_get_data(chip);
  954. struct uart_port *port = &s->p[0].port;
  955. val = sc16is7xx_port_read(port, SC16IS7XX_IOSTATE_REG);
  956. return !!(val & BIT(offset));
  957. }
  958. static void sc16is7xx_gpio_set(struct gpio_chip *chip, unsigned offset, int val)
  959. {
  960. struct sc16is7xx_port *s = gpiochip_get_data(chip);
  961. struct uart_port *port = &s->p[0].port;
  962. sc16is7xx_port_update(port, SC16IS7XX_IOSTATE_REG, BIT(offset),
  963. val ? BIT(offset) : 0);
  964. }
  965. static int sc16is7xx_gpio_direction_input(struct gpio_chip *chip,
  966. unsigned offset)
  967. {
  968. struct sc16is7xx_port *s = gpiochip_get_data(chip);
  969. struct uart_port *port = &s->p[0].port;
  970. sc16is7xx_port_update(port, SC16IS7XX_IODIR_REG, BIT(offset), 0);
  971. return 0;
  972. }
  973. static int sc16is7xx_gpio_direction_output(struct gpio_chip *chip,
  974. unsigned offset, int val)
  975. {
  976. struct sc16is7xx_port *s = gpiochip_get_data(chip);
  977. struct uart_port *port = &s->p[0].port;
  978. u8 state = sc16is7xx_port_read(port, SC16IS7XX_IOSTATE_REG);
  979. if (val)
  980. state |= BIT(offset);
  981. else
  982. state &= ~BIT(offset);
  983. sc16is7xx_port_write(port, SC16IS7XX_IOSTATE_REG, state);
  984. sc16is7xx_port_update(port, SC16IS7XX_IODIR_REG, BIT(offset),
  985. BIT(offset));
  986. return 0;
  987. }
  988. #endif
  989. static int sc16is7xx_probe(struct device *dev,
  990. const struct sc16is7xx_devtype *devtype,
  991. struct regmap *regmap, int irq, unsigned long flags)
  992. {
  993. struct sched_param sched_param = { .sched_priority = MAX_RT_PRIO / 2 };
  994. unsigned long freq, *pfreq = dev_get_platdata(dev);
  995. int i, ret;
  996. struct sc16is7xx_port *s;
  997. if (IS_ERR(regmap))
  998. return PTR_ERR(regmap);
  999. /* Alloc port structure */
  1000. s = devm_kzalloc(dev, sizeof(*s) +
  1001. sizeof(struct sc16is7xx_one) * devtype->nr_uart,
  1002. GFP_KERNEL);
  1003. if (!s) {
  1004. dev_err(dev, "Error allocating port structure\n");
  1005. return -ENOMEM;
  1006. }
  1007. s->clk = devm_clk_get(dev, NULL);
  1008. if (IS_ERR(s->clk)) {
  1009. if (pfreq)
  1010. freq = *pfreq;
  1011. else
  1012. return PTR_ERR(s->clk);
  1013. } else {
  1014. ret = clk_prepare_enable(s->clk);
  1015. if (ret)
  1016. return ret;
  1017. freq = clk_get_rate(s->clk);
  1018. }
  1019. s->regmap = regmap;
  1020. s->devtype = devtype;
  1021. dev_set_drvdata(dev, s);
  1022. mutex_init(&s->efr_lock);
  1023. kthread_init_worker(&s->kworker);
  1024. kthread_init_work(&s->irq_work, sc16is7xx_ist);
  1025. s->kworker_task = kthread_run(kthread_worker_fn, &s->kworker,
  1026. "sc16is7xx");
  1027. if (IS_ERR(s->kworker_task)) {
  1028. ret = PTR_ERR(s->kworker_task);
  1029. goto out_clk;
  1030. }
  1031. sched_setscheduler(s->kworker_task, SCHED_FIFO, &sched_param);
  1032. #ifdef CONFIG_GPIOLIB
  1033. if (devtype->nr_gpio) {
  1034. /* Setup GPIO cotroller */
  1035. s->gpio.owner = THIS_MODULE;
  1036. s->gpio.parent = dev;
  1037. s->gpio.label = dev_name(dev);
  1038. s->gpio.direction_input = sc16is7xx_gpio_direction_input;
  1039. s->gpio.get = sc16is7xx_gpio_get;
  1040. s->gpio.direction_output = sc16is7xx_gpio_direction_output;
  1041. s->gpio.set = sc16is7xx_gpio_set;
  1042. s->gpio.base = -1;
  1043. s->gpio.ngpio = devtype->nr_gpio;
  1044. s->gpio.can_sleep = 1;
  1045. ret = gpiochip_add_data(&s->gpio, s);
  1046. if (ret)
  1047. goto out_thread;
  1048. }
  1049. #endif
  1050. /* reset device, purging any pending irq / data */
  1051. regmap_write(s->regmap, SC16IS7XX_IOCONTROL_REG << SC16IS7XX_REG_SHIFT,
  1052. SC16IS7XX_IOCONTROL_SRESET_BIT);
  1053. for (i = 0; i < devtype->nr_uart; ++i) {
  1054. s->p[i].line = i;
  1055. /* Initialize port data */
  1056. s->p[i].port.dev = dev;
  1057. s->p[i].port.irq = irq;
  1058. s->p[i].port.type = PORT_SC16IS7XX;
  1059. s->p[i].port.fifosize = SC16IS7XX_FIFO_SIZE;
  1060. s->p[i].port.flags = UPF_FIXED_TYPE | UPF_LOW_LATENCY;
  1061. s->p[i].port.iotype = UPIO_PORT;
  1062. s->p[i].port.uartclk = freq;
  1063. s->p[i].port.rs485_config = sc16is7xx_config_rs485;
  1064. s->p[i].port.ops = &sc16is7xx_ops;
  1065. s->p[i].port.line = sc16is7xx_alloc_line();
  1066. if (s->p[i].port.line >= SC16IS7XX_MAX_DEVS) {
  1067. ret = -ENOMEM;
  1068. goto out_ports;
  1069. }
  1070. /* Disable all interrupts */
  1071. sc16is7xx_port_write(&s->p[i].port, SC16IS7XX_IER_REG, 0);
  1072. /* Disable TX/RX */
  1073. sc16is7xx_port_write(&s->p[i].port, SC16IS7XX_EFCR_REG,
  1074. SC16IS7XX_EFCR_RXDISABLE_BIT |
  1075. SC16IS7XX_EFCR_TXDISABLE_BIT);
  1076. /* Initialize kthread work structs */
  1077. kthread_init_work(&s->p[i].tx_work, sc16is7xx_tx_proc);
  1078. kthread_init_work(&s->p[i].reg_work, sc16is7xx_reg_proc);
  1079. /* Register port */
  1080. uart_add_one_port(&sc16is7xx_uart, &s->p[i].port);
  1081. /* Enable EFR */
  1082. sc16is7xx_port_write(&s->p[i].port, SC16IS7XX_LCR_REG,
  1083. SC16IS7XX_LCR_CONF_MODE_B);
  1084. regcache_cache_bypass(s->regmap, true);
  1085. /* Enable write access to enhanced features */
  1086. sc16is7xx_port_write(&s->p[i].port, SC16IS7XX_EFR_REG,
  1087. SC16IS7XX_EFR_ENABLE_BIT);
  1088. regcache_cache_bypass(s->regmap, false);
  1089. /* Restore access to general registers */
  1090. sc16is7xx_port_write(&s->p[i].port, SC16IS7XX_LCR_REG, 0x00);
  1091. /* Go to suspend mode */
  1092. sc16is7xx_power(&s->p[i].port, 0);
  1093. }
  1094. /* Setup interrupt */
  1095. ret = devm_request_irq(dev, irq, sc16is7xx_irq,
  1096. flags, dev_name(dev), s);
  1097. if (!ret)
  1098. return 0;
  1099. out_ports:
  1100. for (i--; i >= 0; i--) {
  1101. uart_remove_one_port(&sc16is7xx_uart, &s->p[i].port);
  1102. clear_bit(s->p[i].port.line, &sc16is7xx_lines);
  1103. }
  1104. #ifdef CONFIG_GPIOLIB
  1105. if (devtype->nr_gpio)
  1106. gpiochip_remove(&s->gpio);
  1107. out_thread:
  1108. #endif
  1109. kthread_stop(s->kworker_task);
  1110. out_clk:
  1111. if (!IS_ERR(s->clk))
  1112. clk_disable_unprepare(s->clk);
  1113. return ret;
  1114. }
  1115. static int sc16is7xx_remove(struct device *dev)
  1116. {
  1117. struct sc16is7xx_port *s = dev_get_drvdata(dev);
  1118. int i;
  1119. #ifdef CONFIG_GPIOLIB
  1120. if (s->devtype->nr_gpio)
  1121. gpiochip_remove(&s->gpio);
  1122. #endif
  1123. for (i = 0; i < s->devtype->nr_uart; i++) {
  1124. uart_remove_one_port(&sc16is7xx_uart, &s->p[i].port);
  1125. clear_bit(s->p[i].port.line, &sc16is7xx_lines);
  1126. sc16is7xx_power(&s->p[i].port, 0);
  1127. }
  1128. kthread_flush_worker(&s->kworker);
  1129. kthread_stop(s->kworker_task);
  1130. if (!IS_ERR(s->clk))
  1131. clk_disable_unprepare(s->clk);
  1132. return 0;
  1133. }
  1134. static const struct of_device_id __maybe_unused sc16is7xx_dt_ids[] = {
  1135. { .compatible = "nxp,sc16is740", .data = &sc16is74x_devtype, },
  1136. { .compatible = "nxp,sc16is741", .data = &sc16is74x_devtype, },
  1137. { .compatible = "nxp,sc16is750", .data = &sc16is750_devtype, },
  1138. { .compatible = "nxp,sc16is752", .data = &sc16is752_devtype, },
  1139. { .compatible = "nxp,sc16is760", .data = &sc16is760_devtype, },
  1140. { .compatible = "nxp,sc16is762", .data = &sc16is762_devtype, },
  1141. { }
  1142. };
  1143. MODULE_DEVICE_TABLE(of, sc16is7xx_dt_ids);
  1144. static struct regmap_config regcfg = {
  1145. .reg_bits = 7,
  1146. .pad_bits = 1,
  1147. .val_bits = 8,
  1148. .cache_type = REGCACHE_RBTREE,
  1149. .volatile_reg = sc16is7xx_regmap_volatile,
  1150. .precious_reg = sc16is7xx_regmap_precious,
  1151. };
  1152. #ifdef CONFIG_SERIAL_SC16IS7XX_SPI
  1153. static int sc16is7xx_spi_probe(struct spi_device *spi)
  1154. {
  1155. const struct sc16is7xx_devtype *devtype;
  1156. unsigned long flags = 0;
  1157. struct regmap *regmap;
  1158. int ret;
  1159. /* Setup SPI bus */
  1160. spi->bits_per_word = 8;
  1161. /* only supports mode 0 on SC16IS762 */
  1162. spi->mode = spi->mode ? : SPI_MODE_0;
  1163. spi->max_speed_hz = spi->max_speed_hz ? : 15000000;
  1164. ret = spi_setup(spi);
  1165. if (ret)
  1166. return ret;
  1167. if (spi->dev.of_node) {
  1168. const struct of_device_id *of_id =
  1169. of_match_device(sc16is7xx_dt_ids, &spi->dev);
  1170. if (!of_id)
  1171. return -ENODEV;
  1172. devtype = (struct sc16is7xx_devtype *)of_id->data;
  1173. } else {
  1174. const struct spi_device_id *id_entry = spi_get_device_id(spi);
  1175. devtype = (struct sc16is7xx_devtype *)id_entry->driver_data;
  1176. flags = IRQF_TRIGGER_FALLING;
  1177. }
  1178. regcfg.max_register = (0xf << SC16IS7XX_REG_SHIFT) |
  1179. (devtype->nr_uart - 1);
  1180. regmap = devm_regmap_init_spi(spi, &regcfg);
  1181. return sc16is7xx_probe(&spi->dev, devtype, regmap, spi->irq, flags);
  1182. }
  1183. static int sc16is7xx_spi_remove(struct spi_device *spi)
  1184. {
  1185. return sc16is7xx_remove(&spi->dev);
  1186. }
  1187. static const struct spi_device_id sc16is7xx_spi_id_table[] = {
  1188. { "sc16is74x", (kernel_ulong_t)&sc16is74x_devtype, },
  1189. { "sc16is740", (kernel_ulong_t)&sc16is74x_devtype, },
  1190. { "sc16is741", (kernel_ulong_t)&sc16is74x_devtype, },
  1191. { "sc16is750", (kernel_ulong_t)&sc16is750_devtype, },
  1192. { "sc16is752", (kernel_ulong_t)&sc16is752_devtype, },
  1193. { "sc16is760", (kernel_ulong_t)&sc16is760_devtype, },
  1194. { "sc16is762", (kernel_ulong_t)&sc16is762_devtype, },
  1195. { }
  1196. };
  1197. MODULE_DEVICE_TABLE(spi, sc16is7xx_spi_id_table);
  1198. static struct spi_driver sc16is7xx_spi_uart_driver = {
  1199. .driver = {
  1200. .name = SC16IS7XX_NAME,
  1201. .of_match_table = of_match_ptr(sc16is7xx_dt_ids),
  1202. },
  1203. .probe = sc16is7xx_spi_probe,
  1204. .remove = sc16is7xx_spi_remove,
  1205. .id_table = sc16is7xx_spi_id_table,
  1206. };
  1207. MODULE_ALIAS("spi:sc16is7xx");
  1208. #endif
  1209. #ifdef CONFIG_SERIAL_SC16IS7XX_I2C
  1210. static int sc16is7xx_i2c_probe(struct i2c_client *i2c,
  1211. const struct i2c_device_id *id)
  1212. {
  1213. const struct sc16is7xx_devtype *devtype;
  1214. unsigned long flags = 0;
  1215. struct regmap *regmap;
  1216. if (i2c->dev.of_node) {
  1217. const struct of_device_id *of_id =
  1218. of_match_device(sc16is7xx_dt_ids, &i2c->dev);
  1219. if (!of_id)
  1220. return -ENODEV;
  1221. devtype = (struct sc16is7xx_devtype *)of_id->data;
  1222. } else {
  1223. devtype = (struct sc16is7xx_devtype *)id->driver_data;
  1224. flags = IRQF_TRIGGER_FALLING;
  1225. }
  1226. regcfg.max_register = (0xf << SC16IS7XX_REG_SHIFT) |
  1227. (devtype->nr_uart - 1);
  1228. regmap = devm_regmap_init_i2c(i2c, &regcfg);
  1229. return sc16is7xx_probe(&i2c->dev, devtype, regmap, i2c->irq, flags);
  1230. }
  1231. static int sc16is7xx_i2c_remove(struct i2c_client *client)
  1232. {
  1233. return sc16is7xx_remove(&client->dev);
  1234. }
  1235. static const struct i2c_device_id sc16is7xx_i2c_id_table[] = {
  1236. { "sc16is74x", (kernel_ulong_t)&sc16is74x_devtype, },
  1237. { "sc16is740", (kernel_ulong_t)&sc16is74x_devtype, },
  1238. { "sc16is741", (kernel_ulong_t)&sc16is74x_devtype, },
  1239. { "sc16is750", (kernel_ulong_t)&sc16is750_devtype, },
  1240. { "sc16is752", (kernel_ulong_t)&sc16is752_devtype, },
  1241. { "sc16is760", (kernel_ulong_t)&sc16is760_devtype, },
  1242. { "sc16is762", (kernel_ulong_t)&sc16is762_devtype, },
  1243. { }
  1244. };
  1245. MODULE_DEVICE_TABLE(i2c, sc16is7xx_i2c_id_table);
  1246. static struct i2c_driver sc16is7xx_i2c_uart_driver = {
  1247. .driver = {
  1248. .name = SC16IS7XX_NAME,
  1249. .of_match_table = of_match_ptr(sc16is7xx_dt_ids),
  1250. },
  1251. .probe = sc16is7xx_i2c_probe,
  1252. .remove = sc16is7xx_i2c_remove,
  1253. .id_table = sc16is7xx_i2c_id_table,
  1254. };
  1255. #endif
  1256. static int __init sc16is7xx_init(void)
  1257. {
  1258. int ret;
  1259. ret = uart_register_driver(&sc16is7xx_uart);
  1260. if (ret) {
  1261. pr_err("Registering UART driver failed\n");
  1262. return ret;
  1263. }
  1264. #ifdef CONFIG_SERIAL_SC16IS7XX_I2C
  1265. ret = i2c_add_driver(&sc16is7xx_i2c_uart_driver);
  1266. if (ret < 0) {
  1267. pr_err("failed to init sc16is7xx i2c --> %d\n", ret);
  1268. goto err_i2c;
  1269. }
  1270. #endif
  1271. #ifdef CONFIG_SERIAL_SC16IS7XX_SPI
  1272. ret = spi_register_driver(&sc16is7xx_spi_uart_driver);
  1273. if (ret < 0) {
  1274. pr_err("failed to init sc16is7xx spi --> %d\n", ret);
  1275. goto err_spi;
  1276. }
  1277. #endif
  1278. return ret;
  1279. #ifdef CONFIG_SERIAL_SC16IS7XX_SPI
  1280. err_spi:
  1281. #ifdef CONFIG_SERIAL_SC16IS7XX_I2C
  1282. i2c_del_driver(&sc16is7xx_i2c_uart_driver);
  1283. #endif
  1284. #endif
  1285. err_i2c:
  1286. uart_unregister_driver(&sc16is7xx_uart);
  1287. return ret;
  1288. }
  1289. module_init(sc16is7xx_init);
  1290. static void __exit sc16is7xx_exit(void)
  1291. {
  1292. #ifdef CONFIG_SERIAL_SC16IS7XX_I2C
  1293. i2c_del_driver(&sc16is7xx_i2c_uart_driver);
  1294. #endif
  1295. #ifdef CONFIG_SERIAL_SC16IS7XX_SPI
  1296. spi_unregister_driver(&sc16is7xx_spi_uart_driver);
  1297. #endif
  1298. uart_unregister_driver(&sc16is7xx_uart);
  1299. }
  1300. module_exit(sc16is7xx_exit);
  1301. MODULE_LICENSE("GPL");
  1302. MODULE_AUTHOR("Jon Ringle <jringle@gridpoint.com>");
  1303. MODULE_DESCRIPTION("SC16IS7XX serial driver");