sc16is7xx.c 43 KB

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