sc16is7xx.c 43 KB

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