pch_uart.c 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. *Copyright (C) 2011 LAPIS Semiconductor Co., Ltd.
  4. */
  5. #if defined(CONFIG_SERIAL_PCH_UART_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
  6. #define SUPPORT_SYSRQ
  7. #endif
  8. #include <linux/kernel.h>
  9. #include <linux/serial_reg.h>
  10. #include <linux/slab.h>
  11. #include <linux/module.h>
  12. #include <linux/pci.h>
  13. #include <linux/console.h>
  14. #include <linux/serial_core.h>
  15. #include <linux/tty.h>
  16. #include <linux/tty_flip.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/io.h>
  19. #include <linux/dmi.h>
  20. #include <linux/nmi.h>
  21. #include <linux/delay.h>
  22. #include <linux/of.h>
  23. #include <linux/debugfs.h>
  24. #include <linux/dmaengine.h>
  25. #include <linux/pch_dma.h>
  26. enum {
  27. PCH_UART_HANDLED_RX_INT_SHIFT,
  28. PCH_UART_HANDLED_TX_INT_SHIFT,
  29. PCH_UART_HANDLED_RX_ERR_INT_SHIFT,
  30. PCH_UART_HANDLED_RX_TRG_INT_SHIFT,
  31. PCH_UART_HANDLED_MS_INT_SHIFT,
  32. PCH_UART_HANDLED_LS_INT_SHIFT,
  33. };
  34. #define PCH_UART_DRIVER_DEVICE "ttyPCH"
  35. /* Set the max number of UART port
  36. * Intel EG20T PCH: 4 port
  37. * LAPIS Semiconductor ML7213 IOH: 3 port
  38. * LAPIS Semiconductor ML7223 IOH: 2 port
  39. */
  40. #define PCH_UART_NR 4
  41. #define PCH_UART_HANDLED_RX_INT (1<<((PCH_UART_HANDLED_RX_INT_SHIFT)<<1))
  42. #define PCH_UART_HANDLED_TX_INT (1<<((PCH_UART_HANDLED_TX_INT_SHIFT)<<1))
  43. #define PCH_UART_HANDLED_RX_ERR_INT (1<<((\
  44. PCH_UART_HANDLED_RX_ERR_INT_SHIFT)<<1))
  45. #define PCH_UART_HANDLED_RX_TRG_INT (1<<((\
  46. PCH_UART_HANDLED_RX_TRG_INT_SHIFT)<<1))
  47. #define PCH_UART_HANDLED_MS_INT (1<<((PCH_UART_HANDLED_MS_INT_SHIFT)<<1))
  48. #define PCH_UART_HANDLED_LS_INT (1<<((PCH_UART_HANDLED_LS_INT_SHIFT)<<1))
  49. #define PCH_UART_RBR 0x00
  50. #define PCH_UART_THR 0x00
  51. #define PCH_UART_IER_MASK (PCH_UART_IER_ERBFI|PCH_UART_IER_ETBEI|\
  52. PCH_UART_IER_ELSI|PCH_UART_IER_EDSSI)
  53. #define PCH_UART_IER_ERBFI 0x00000001
  54. #define PCH_UART_IER_ETBEI 0x00000002
  55. #define PCH_UART_IER_ELSI 0x00000004
  56. #define PCH_UART_IER_EDSSI 0x00000008
  57. #define PCH_UART_IIR_IP 0x00000001
  58. #define PCH_UART_IIR_IID 0x00000006
  59. #define PCH_UART_IIR_MSI 0x00000000
  60. #define PCH_UART_IIR_TRI 0x00000002
  61. #define PCH_UART_IIR_RRI 0x00000004
  62. #define PCH_UART_IIR_REI 0x00000006
  63. #define PCH_UART_IIR_TOI 0x00000008
  64. #define PCH_UART_IIR_FIFO256 0x00000020
  65. #define PCH_UART_IIR_FIFO64 PCH_UART_IIR_FIFO256
  66. #define PCH_UART_IIR_FE 0x000000C0
  67. #define PCH_UART_FCR_FIFOE 0x00000001
  68. #define PCH_UART_FCR_RFR 0x00000002
  69. #define PCH_UART_FCR_TFR 0x00000004
  70. #define PCH_UART_FCR_DMS 0x00000008
  71. #define PCH_UART_FCR_FIFO256 0x00000020
  72. #define PCH_UART_FCR_RFTL 0x000000C0
  73. #define PCH_UART_FCR_RFTL1 0x00000000
  74. #define PCH_UART_FCR_RFTL64 0x00000040
  75. #define PCH_UART_FCR_RFTL128 0x00000080
  76. #define PCH_UART_FCR_RFTL224 0x000000C0
  77. #define PCH_UART_FCR_RFTL16 PCH_UART_FCR_RFTL64
  78. #define PCH_UART_FCR_RFTL32 PCH_UART_FCR_RFTL128
  79. #define PCH_UART_FCR_RFTL56 PCH_UART_FCR_RFTL224
  80. #define PCH_UART_FCR_RFTL4 PCH_UART_FCR_RFTL64
  81. #define PCH_UART_FCR_RFTL8 PCH_UART_FCR_RFTL128
  82. #define PCH_UART_FCR_RFTL14 PCH_UART_FCR_RFTL224
  83. #define PCH_UART_FCR_RFTL_SHIFT 6
  84. #define PCH_UART_LCR_WLS 0x00000003
  85. #define PCH_UART_LCR_STB 0x00000004
  86. #define PCH_UART_LCR_PEN 0x00000008
  87. #define PCH_UART_LCR_EPS 0x00000010
  88. #define PCH_UART_LCR_SP 0x00000020
  89. #define PCH_UART_LCR_SB 0x00000040
  90. #define PCH_UART_LCR_DLAB 0x00000080
  91. #define PCH_UART_LCR_NP 0x00000000
  92. #define PCH_UART_LCR_OP PCH_UART_LCR_PEN
  93. #define PCH_UART_LCR_EP (PCH_UART_LCR_PEN | PCH_UART_LCR_EPS)
  94. #define PCH_UART_LCR_1P (PCH_UART_LCR_PEN | PCH_UART_LCR_SP)
  95. #define PCH_UART_LCR_0P (PCH_UART_LCR_PEN | PCH_UART_LCR_EPS |\
  96. PCH_UART_LCR_SP)
  97. #define PCH_UART_LCR_5BIT 0x00000000
  98. #define PCH_UART_LCR_6BIT 0x00000001
  99. #define PCH_UART_LCR_7BIT 0x00000002
  100. #define PCH_UART_LCR_8BIT 0x00000003
  101. #define PCH_UART_MCR_DTR 0x00000001
  102. #define PCH_UART_MCR_RTS 0x00000002
  103. #define PCH_UART_MCR_OUT 0x0000000C
  104. #define PCH_UART_MCR_LOOP 0x00000010
  105. #define PCH_UART_MCR_AFE 0x00000020
  106. #define PCH_UART_LSR_DR 0x00000001
  107. #define PCH_UART_LSR_ERR (1<<7)
  108. #define PCH_UART_MSR_DCTS 0x00000001
  109. #define PCH_UART_MSR_DDSR 0x00000002
  110. #define PCH_UART_MSR_TERI 0x00000004
  111. #define PCH_UART_MSR_DDCD 0x00000008
  112. #define PCH_UART_MSR_CTS 0x00000010
  113. #define PCH_UART_MSR_DSR 0x00000020
  114. #define PCH_UART_MSR_RI 0x00000040
  115. #define PCH_UART_MSR_DCD 0x00000080
  116. #define PCH_UART_MSR_DELTA (PCH_UART_MSR_DCTS | PCH_UART_MSR_DDSR |\
  117. PCH_UART_MSR_TERI | PCH_UART_MSR_DDCD)
  118. #define PCH_UART_DLL 0x00
  119. #define PCH_UART_DLM 0x01
  120. #define PCH_UART_BRCSR 0x0E
  121. #define PCH_UART_IID_RLS (PCH_UART_IIR_REI)
  122. #define PCH_UART_IID_RDR (PCH_UART_IIR_RRI)
  123. #define PCH_UART_IID_RDR_TO (PCH_UART_IIR_RRI | PCH_UART_IIR_TOI)
  124. #define PCH_UART_IID_THRE (PCH_UART_IIR_TRI)
  125. #define PCH_UART_IID_MS (PCH_UART_IIR_MSI)
  126. #define PCH_UART_HAL_PARITY_NONE (PCH_UART_LCR_NP)
  127. #define PCH_UART_HAL_PARITY_ODD (PCH_UART_LCR_OP)
  128. #define PCH_UART_HAL_PARITY_EVEN (PCH_UART_LCR_EP)
  129. #define PCH_UART_HAL_PARITY_FIX1 (PCH_UART_LCR_1P)
  130. #define PCH_UART_HAL_PARITY_FIX0 (PCH_UART_LCR_0P)
  131. #define PCH_UART_HAL_5BIT (PCH_UART_LCR_5BIT)
  132. #define PCH_UART_HAL_6BIT (PCH_UART_LCR_6BIT)
  133. #define PCH_UART_HAL_7BIT (PCH_UART_LCR_7BIT)
  134. #define PCH_UART_HAL_8BIT (PCH_UART_LCR_8BIT)
  135. #define PCH_UART_HAL_STB1 0
  136. #define PCH_UART_HAL_STB2 (PCH_UART_LCR_STB)
  137. #define PCH_UART_HAL_CLR_TX_FIFO (PCH_UART_FCR_TFR)
  138. #define PCH_UART_HAL_CLR_RX_FIFO (PCH_UART_FCR_RFR)
  139. #define PCH_UART_HAL_CLR_ALL_FIFO (PCH_UART_HAL_CLR_TX_FIFO | \
  140. PCH_UART_HAL_CLR_RX_FIFO)
  141. #define PCH_UART_HAL_DMA_MODE0 0
  142. #define PCH_UART_HAL_FIFO_DIS 0
  143. #define PCH_UART_HAL_FIFO16 (PCH_UART_FCR_FIFOE)
  144. #define PCH_UART_HAL_FIFO256 (PCH_UART_FCR_FIFOE | \
  145. PCH_UART_FCR_FIFO256)
  146. #define PCH_UART_HAL_FIFO64 (PCH_UART_HAL_FIFO256)
  147. #define PCH_UART_HAL_TRIGGER1 (PCH_UART_FCR_RFTL1)
  148. #define PCH_UART_HAL_TRIGGER64 (PCH_UART_FCR_RFTL64)
  149. #define PCH_UART_HAL_TRIGGER128 (PCH_UART_FCR_RFTL128)
  150. #define PCH_UART_HAL_TRIGGER224 (PCH_UART_FCR_RFTL224)
  151. #define PCH_UART_HAL_TRIGGER16 (PCH_UART_FCR_RFTL16)
  152. #define PCH_UART_HAL_TRIGGER32 (PCH_UART_FCR_RFTL32)
  153. #define PCH_UART_HAL_TRIGGER56 (PCH_UART_FCR_RFTL56)
  154. #define PCH_UART_HAL_TRIGGER4 (PCH_UART_FCR_RFTL4)
  155. #define PCH_UART_HAL_TRIGGER8 (PCH_UART_FCR_RFTL8)
  156. #define PCH_UART_HAL_TRIGGER14 (PCH_UART_FCR_RFTL14)
  157. #define PCH_UART_HAL_TRIGGER_L (PCH_UART_FCR_RFTL64)
  158. #define PCH_UART_HAL_TRIGGER_M (PCH_UART_FCR_RFTL128)
  159. #define PCH_UART_HAL_TRIGGER_H (PCH_UART_FCR_RFTL224)
  160. #define PCH_UART_HAL_RX_INT (PCH_UART_IER_ERBFI)
  161. #define PCH_UART_HAL_TX_INT (PCH_UART_IER_ETBEI)
  162. #define PCH_UART_HAL_RX_ERR_INT (PCH_UART_IER_ELSI)
  163. #define PCH_UART_HAL_MS_INT (PCH_UART_IER_EDSSI)
  164. #define PCH_UART_HAL_ALL_INT (PCH_UART_IER_MASK)
  165. #define PCH_UART_HAL_DTR (PCH_UART_MCR_DTR)
  166. #define PCH_UART_HAL_RTS (PCH_UART_MCR_RTS)
  167. #define PCH_UART_HAL_OUT (PCH_UART_MCR_OUT)
  168. #define PCH_UART_HAL_LOOP (PCH_UART_MCR_LOOP)
  169. #define PCH_UART_HAL_AFE (PCH_UART_MCR_AFE)
  170. #define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
  171. #define DEFAULT_UARTCLK 1843200 /* 1.8432 MHz */
  172. #define CMITC_UARTCLK 192000000 /* 192.0000 MHz */
  173. #define FRI2_64_UARTCLK 64000000 /* 64.0000 MHz */
  174. #define FRI2_48_UARTCLK 48000000 /* 48.0000 MHz */
  175. #define NTC1_UARTCLK 64000000 /* 64.0000 MHz */
  176. #define MINNOW_UARTCLK 50000000 /* 50.0000 MHz */
  177. struct pch_uart_buffer {
  178. unsigned char *buf;
  179. int size;
  180. };
  181. struct eg20t_port {
  182. struct uart_port port;
  183. int port_type;
  184. void __iomem *membase;
  185. resource_size_t mapbase;
  186. unsigned int iobase;
  187. struct pci_dev *pdev;
  188. int fifo_size;
  189. unsigned int uartclk;
  190. int start_tx;
  191. int start_rx;
  192. int tx_empty;
  193. int trigger;
  194. int trigger_level;
  195. struct pch_uart_buffer rxbuf;
  196. unsigned int dmsr;
  197. unsigned int fcr;
  198. unsigned int mcr;
  199. unsigned int use_dma;
  200. struct dma_async_tx_descriptor *desc_tx;
  201. struct dma_async_tx_descriptor *desc_rx;
  202. struct pch_dma_slave param_tx;
  203. struct pch_dma_slave param_rx;
  204. struct dma_chan *chan_tx;
  205. struct dma_chan *chan_rx;
  206. struct scatterlist *sg_tx_p;
  207. int nent;
  208. int orig_nent;
  209. struct scatterlist sg_rx;
  210. int tx_dma_use;
  211. void *rx_buf_virt;
  212. dma_addr_t rx_buf_dma;
  213. struct dentry *debugfs;
  214. #define IRQ_NAME_SIZE 17
  215. char irq_name[IRQ_NAME_SIZE];
  216. /* protect the eg20t_port private structure and io access to membase */
  217. spinlock_t lock;
  218. };
  219. /**
  220. * struct pch_uart_driver_data - private data structure for UART-DMA
  221. * @port_type: The type of UART port
  222. * @line_no: UART port line number (0, 1, 2...)
  223. */
  224. struct pch_uart_driver_data {
  225. int port_type;
  226. int line_no;
  227. };
  228. enum pch_uart_num_t {
  229. pch_et20t_uart0 = 0,
  230. pch_et20t_uart1,
  231. pch_et20t_uart2,
  232. pch_et20t_uart3,
  233. pch_ml7213_uart0,
  234. pch_ml7213_uart1,
  235. pch_ml7213_uart2,
  236. pch_ml7223_uart0,
  237. pch_ml7223_uart1,
  238. pch_ml7831_uart0,
  239. pch_ml7831_uart1,
  240. };
  241. static struct pch_uart_driver_data drv_dat[] = {
  242. [pch_et20t_uart0] = {PORT_PCH_8LINE, 0},
  243. [pch_et20t_uart1] = {PORT_PCH_2LINE, 1},
  244. [pch_et20t_uart2] = {PORT_PCH_2LINE, 2},
  245. [pch_et20t_uart3] = {PORT_PCH_2LINE, 3},
  246. [pch_ml7213_uart0] = {PORT_PCH_8LINE, 0},
  247. [pch_ml7213_uart1] = {PORT_PCH_2LINE, 1},
  248. [pch_ml7213_uart2] = {PORT_PCH_2LINE, 2},
  249. [pch_ml7223_uart0] = {PORT_PCH_8LINE, 0},
  250. [pch_ml7223_uart1] = {PORT_PCH_2LINE, 1},
  251. [pch_ml7831_uart0] = {PORT_PCH_8LINE, 0},
  252. [pch_ml7831_uart1] = {PORT_PCH_2LINE, 1},
  253. };
  254. #ifdef CONFIG_SERIAL_PCH_UART_CONSOLE
  255. static struct eg20t_port *pch_uart_ports[PCH_UART_NR];
  256. #endif
  257. static unsigned int default_baud = 9600;
  258. static unsigned int user_uartclk = 0;
  259. static const int trigger_level_256[4] = { 1, 64, 128, 224 };
  260. static const int trigger_level_64[4] = { 1, 16, 32, 56 };
  261. static const int trigger_level_16[4] = { 1, 4, 8, 14 };
  262. static const int trigger_level_1[4] = { 1, 1, 1, 1 };
  263. #ifdef CONFIG_DEBUG_FS
  264. #define PCH_REGS_BUFSIZE 1024
  265. static ssize_t port_show_regs(struct file *file, char __user *user_buf,
  266. size_t count, loff_t *ppos)
  267. {
  268. struct eg20t_port *priv = file->private_data;
  269. char *buf;
  270. u32 len = 0;
  271. ssize_t ret;
  272. unsigned char lcr;
  273. buf = kzalloc(PCH_REGS_BUFSIZE, GFP_KERNEL);
  274. if (!buf)
  275. return 0;
  276. len += snprintf(buf + len, PCH_REGS_BUFSIZE - len,
  277. "PCH EG20T port[%d] regs:\n", priv->port.line);
  278. len += snprintf(buf + len, PCH_REGS_BUFSIZE - len,
  279. "=================================\n");
  280. len += snprintf(buf + len, PCH_REGS_BUFSIZE - len,
  281. "IER: \t0x%02x\n", ioread8(priv->membase + UART_IER));
  282. len += snprintf(buf + len, PCH_REGS_BUFSIZE - len,
  283. "IIR: \t0x%02x\n", ioread8(priv->membase + UART_IIR));
  284. len += snprintf(buf + len, PCH_REGS_BUFSIZE - len,
  285. "LCR: \t0x%02x\n", ioread8(priv->membase + UART_LCR));
  286. len += snprintf(buf + len, PCH_REGS_BUFSIZE - len,
  287. "MCR: \t0x%02x\n", ioread8(priv->membase + UART_MCR));
  288. len += snprintf(buf + len, PCH_REGS_BUFSIZE - len,
  289. "LSR: \t0x%02x\n", ioread8(priv->membase + UART_LSR));
  290. len += snprintf(buf + len, PCH_REGS_BUFSIZE - len,
  291. "MSR: \t0x%02x\n", ioread8(priv->membase + UART_MSR));
  292. len += snprintf(buf + len, PCH_REGS_BUFSIZE - len,
  293. "BRCSR: \t0x%02x\n",
  294. ioread8(priv->membase + PCH_UART_BRCSR));
  295. lcr = ioread8(priv->membase + UART_LCR);
  296. iowrite8(PCH_UART_LCR_DLAB, priv->membase + UART_LCR);
  297. len += snprintf(buf + len, PCH_REGS_BUFSIZE - len,
  298. "DLL: \t0x%02x\n", ioread8(priv->membase + UART_DLL));
  299. len += snprintf(buf + len, PCH_REGS_BUFSIZE - len,
  300. "DLM: \t0x%02x\n", ioread8(priv->membase + UART_DLM));
  301. iowrite8(lcr, priv->membase + UART_LCR);
  302. if (len > PCH_REGS_BUFSIZE)
  303. len = PCH_REGS_BUFSIZE;
  304. ret = simple_read_from_buffer(user_buf, count, ppos, buf, len);
  305. kfree(buf);
  306. return ret;
  307. }
  308. static const struct file_operations port_regs_ops = {
  309. .owner = THIS_MODULE,
  310. .open = simple_open,
  311. .read = port_show_regs,
  312. .llseek = default_llseek,
  313. };
  314. #endif /* CONFIG_DEBUG_FS */
  315. static const struct dmi_system_id pch_uart_dmi_table[] = {
  316. {
  317. .ident = "CM-iTC",
  318. {
  319. DMI_MATCH(DMI_BOARD_NAME, "CM-iTC"),
  320. },
  321. (void *)CMITC_UARTCLK,
  322. },
  323. {
  324. .ident = "FRI2",
  325. {
  326. DMI_MATCH(DMI_BIOS_VERSION, "FRI2"),
  327. },
  328. (void *)FRI2_64_UARTCLK,
  329. },
  330. {
  331. .ident = "Fish River Island II",
  332. {
  333. DMI_MATCH(DMI_PRODUCT_NAME, "Fish River Island II"),
  334. },
  335. (void *)FRI2_48_UARTCLK,
  336. },
  337. {
  338. .ident = "COMe-mTT",
  339. {
  340. DMI_MATCH(DMI_BOARD_NAME, "COMe-mTT"),
  341. },
  342. (void *)NTC1_UARTCLK,
  343. },
  344. {
  345. .ident = "nanoETXexpress-TT",
  346. {
  347. DMI_MATCH(DMI_BOARD_NAME, "nanoETXexpress-TT"),
  348. },
  349. (void *)NTC1_UARTCLK,
  350. },
  351. {
  352. .ident = "MinnowBoard",
  353. {
  354. DMI_MATCH(DMI_BOARD_NAME, "MinnowBoard"),
  355. },
  356. (void *)MINNOW_UARTCLK,
  357. },
  358. { }
  359. };
  360. /* Return UART clock, checking for board specific clocks. */
  361. static unsigned int pch_uart_get_uartclk(void)
  362. {
  363. const struct dmi_system_id *d;
  364. if (user_uartclk)
  365. return user_uartclk;
  366. d = dmi_first_match(pch_uart_dmi_table);
  367. if (d)
  368. return (unsigned long)d->driver_data;
  369. return DEFAULT_UARTCLK;
  370. }
  371. static void pch_uart_hal_enable_interrupt(struct eg20t_port *priv,
  372. unsigned int flag)
  373. {
  374. u8 ier = ioread8(priv->membase + UART_IER);
  375. ier |= flag & PCH_UART_IER_MASK;
  376. iowrite8(ier, priv->membase + UART_IER);
  377. }
  378. static void pch_uart_hal_disable_interrupt(struct eg20t_port *priv,
  379. unsigned int flag)
  380. {
  381. u8 ier = ioread8(priv->membase + UART_IER);
  382. ier &= ~(flag & PCH_UART_IER_MASK);
  383. iowrite8(ier, priv->membase + UART_IER);
  384. }
  385. static int pch_uart_hal_set_line(struct eg20t_port *priv, unsigned int baud,
  386. unsigned int parity, unsigned int bits,
  387. unsigned int stb)
  388. {
  389. unsigned int dll, dlm, lcr;
  390. int div;
  391. div = DIV_ROUND_CLOSEST(priv->uartclk / 16, baud);
  392. if (div < 0 || USHRT_MAX <= div) {
  393. dev_err(priv->port.dev, "Invalid Baud(div=0x%x)\n", div);
  394. return -EINVAL;
  395. }
  396. dll = (unsigned int)div & 0x00FFU;
  397. dlm = ((unsigned int)div >> 8) & 0x00FFU;
  398. if (parity & ~(PCH_UART_LCR_PEN | PCH_UART_LCR_EPS | PCH_UART_LCR_SP)) {
  399. dev_err(priv->port.dev, "Invalid parity(0x%x)\n", parity);
  400. return -EINVAL;
  401. }
  402. if (bits & ~PCH_UART_LCR_WLS) {
  403. dev_err(priv->port.dev, "Invalid bits(0x%x)\n", bits);
  404. return -EINVAL;
  405. }
  406. if (stb & ~PCH_UART_LCR_STB) {
  407. dev_err(priv->port.dev, "Invalid STB(0x%x)\n", stb);
  408. return -EINVAL;
  409. }
  410. lcr = parity;
  411. lcr |= bits;
  412. lcr |= stb;
  413. dev_dbg(priv->port.dev, "%s:baud = %u, div = %04x, lcr = %02x (%lu)\n",
  414. __func__, baud, div, lcr, jiffies);
  415. iowrite8(PCH_UART_LCR_DLAB, priv->membase + UART_LCR);
  416. iowrite8(dll, priv->membase + PCH_UART_DLL);
  417. iowrite8(dlm, priv->membase + PCH_UART_DLM);
  418. iowrite8(lcr, priv->membase + UART_LCR);
  419. return 0;
  420. }
  421. static int pch_uart_hal_fifo_reset(struct eg20t_port *priv,
  422. unsigned int flag)
  423. {
  424. if (flag & ~(PCH_UART_FCR_TFR | PCH_UART_FCR_RFR)) {
  425. dev_err(priv->port.dev, "%s:Invalid flag(0x%x)\n",
  426. __func__, flag);
  427. return -EINVAL;
  428. }
  429. iowrite8(PCH_UART_FCR_FIFOE | priv->fcr, priv->membase + UART_FCR);
  430. iowrite8(PCH_UART_FCR_FIFOE | priv->fcr | flag,
  431. priv->membase + UART_FCR);
  432. iowrite8(priv->fcr, priv->membase + UART_FCR);
  433. return 0;
  434. }
  435. static int pch_uart_hal_set_fifo(struct eg20t_port *priv,
  436. unsigned int dmamode,
  437. unsigned int fifo_size, unsigned int trigger)
  438. {
  439. u8 fcr;
  440. if (dmamode & ~PCH_UART_FCR_DMS) {
  441. dev_err(priv->port.dev, "%s:Invalid DMA Mode(0x%x)\n",
  442. __func__, dmamode);
  443. return -EINVAL;
  444. }
  445. if (fifo_size & ~(PCH_UART_FCR_FIFOE | PCH_UART_FCR_FIFO256)) {
  446. dev_err(priv->port.dev, "%s:Invalid FIFO SIZE(0x%x)\n",
  447. __func__, fifo_size);
  448. return -EINVAL;
  449. }
  450. if (trigger & ~PCH_UART_FCR_RFTL) {
  451. dev_err(priv->port.dev, "%s:Invalid TRIGGER(0x%x)\n",
  452. __func__, trigger);
  453. return -EINVAL;
  454. }
  455. switch (priv->fifo_size) {
  456. case 256:
  457. priv->trigger_level =
  458. trigger_level_256[trigger >> PCH_UART_FCR_RFTL_SHIFT];
  459. break;
  460. case 64:
  461. priv->trigger_level =
  462. trigger_level_64[trigger >> PCH_UART_FCR_RFTL_SHIFT];
  463. break;
  464. case 16:
  465. priv->trigger_level =
  466. trigger_level_16[trigger >> PCH_UART_FCR_RFTL_SHIFT];
  467. break;
  468. default:
  469. priv->trigger_level =
  470. trigger_level_1[trigger >> PCH_UART_FCR_RFTL_SHIFT];
  471. break;
  472. }
  473. fcr =
  474. dmamode | fifo_size | trigger | PCH_UART_FCR_RFR | PCH_UART_FCR_TFR;
  475. iowrite8(PCH_UART_FCR_FIFOE, priv->membase + UART_FCR);
  476. iowrite8(PCH_UART_FCR_FIFOE | PCH_UART_FCR_RFR | PCH_UART_FCR_TFR,
  477. priv->membase + UART_FCR);
  478. iowrite8(fcr, priv->membase + UART_FCR);
  479. priv->fcr = fcr;
  480. return 0;
  481. }
  482. static u8 pch_uart_hal_get_modem(struct eg20t_port *priv)
  483. {
  484. unsigned int msr = ioread8(priv->membase + UART_MSR);
  485. priv->dmsr = msr & PCH_UART_MSR_DELTA;
  486. return (u8)msr;
  487. }
  488. static void pch_uart_hal_write(struct eg20t_port *priv,
  489. const unsigned char *buf, int tx_size)
  490. {
  491. int i;
  492. unsigned int thr;
  493. for (i = 0; i < tx_size;) {
  494. thr = buf[i++];
  495. iowrite8(thr, priv->membase + PCH_UART_THR);
  496. }
  497. }
  498. static int pch_uart_hal_read(struct eg20t_port *priv, unsigned char *buf,
  499. int rx_size)
  500. {
  501. int i;
  502. u8 rbr, lsr;
  503. struct uart_port *port = &priv->port;
  504. lsr = ioread8(priv->membase + UART_LSR);
  505. for (i = 0, lsr = ioread8(priv->membase + UART_LSR);
  506. i < rx_size && lsr & (UART_LSR_DR | UART_LSR_BI);
  507. lsr = ioread8(priv->membase + UART_LSR)) {
  508. rbr = ioread8(priv->membase + PCH_UART_RBR);
  509. if (lsr & UART_LSR_BI) {
  510. port->icount.brk++;
  511. if (uart_handle_break(port))
  512. continue;
  513. }
  514. #ifdef SUPPORT_SYSRQ
  515. if (port->sysrq) {
  516. if (uart_handle_sysrq_char(port, rbr))
  517. continue;
  518. }
  519. #endif
  520. buf[i++] = rbr;
  521. }
  522. return i;
  523. }
  524. static unsigned char pch_uart_hal_get_iid(struct eg20t_port *priv)
  525. {
  526. return ioread8(priv->membase + UART_IIR) &\
  527. (PCH_UART_IIR_IID | PCH_UART_IIR_TOI | PCH_UART_IIR_IP);
  528. }
  529. static u8 pch_uart_hal_get_line_status(struct eg20t_port *priv)
  530. {
  531. return ioread8(priv->membase + UART_LSR);
  532. }
  533. static void pch_uart_hal_set_break(struct eg20t_port *priv, int on)
  534. {
  535. unsigned int lcr;
  536. lcr = ioread8(priv->membase + UART_LCR);
  537. if (on)
  538. lcr |= PCH_UART_LCR_SB;
  539. else
  540. lcr &= ~PCH_UART_LCR_SB;
  541. iowrite8(lcr, priv->membase + UART_LCR);
  542. }
  543. static int push_rx(struct eg20t_port *priv, const unsigned char *buf,
  544. int size)
  545. {
  546. struct uart_port *port = &priv->port;
  547. struct tty_port *tport = &port->state->port;
  548. tty_insert_flip_string(tport, buf, size);
  549. tty_flip_buffer_push(tport);
  550. return 0;
  551. }
  552. static int pop_tx_x(struct eg20t_port *priv, unsigned char *buf)
  553. {
  554. int ret = 0;
  555. struct uart_port *port = &priv->port;
  556. if (port->x_char) {
  557. dev_dbg(priv->port.dev, "%s:X character send %02x (%lu)\n",
  558. __func__, port->x_char, jiffies);
  559. buf[0] = port->x_char;
  560. port->x_char = 0;
  561. ret = 1;
  562. }
  563. return ret;
  564. }
  565. static int dma_push_rx(struct eg20t_port *priv, int size)
  566. {
  567. int room;
  568. struct uart_port *port = &priv->port;
  569. struct tty_port *tport = &port->state->port;
  570. room = tty_buffer_request_room(tport, size);
  571. if (room < size)
  572. dev_warn(port->dev, "Rx overrun: dropping %u bytes\n",
  573. size - room);
  574. if (!room)
  575. return 0;
  576. tty_insert_flip_string(tport, sg_virt(&priv->sg_rx), size);
  577. port->icount.rx += room;
  578. return room;
  579. }
  580. static void pch_free_dma(struct uart_port *port)
  581. {
  582. struct eg20t_port *priv;
  583. priv = container_of(port, struct eg20t_port, port);
  584. if (priv->chan_tx) {
  585. dma_release_channel(priv->chan_tx);
  586. priv->chan_tx = NULL;
  587. }
  588. if (priv->chan_rx) {
  589. dma_release_channel(priv->chan_rx);
  590. priv->chan_rx = NULL;
  591. }
  592. if (priv->rx_buf_dma) {
  593. dma_free_coherent(port->dev, port->fifosize, priv->rx_buf_virt,
  594. priv->rx_buf_dma);
  595. priv->rx_buf_virt = NULL;
  596. priv->rx_buf_dma = 0;
  597. }
  598. return;
  599. }
  600. static bool filter(struct dma_chan *chan, void *slave)
  601. {
  602. struct pch_dma_slave *param = slave;
  603. if ((chan->chan_id == param->chan_id) && (param->dma_dev ==
  604. chan->device->dev)) {
  605. chan->private = param;
  606. return true;
  607. } else {
  608. return false;
  609. }
  610. }
  611. static void pch_request_dma(struct uart_port *port)
  612. {
  613. dma_cap_mask_t mask;
  614. struct dma_chan *chan;
  615. struct pci_dev *dma_dev;
  616. struct pch_dma_slave *param;
  617. struct eg20t_port *priv =
  618. container_of(port, struct eg20t_port, port);
  619. dma_cap_zero(mask);
  620. dma_cap_set(DMA_SLAVE, mask);
  621. /* Get DMA's dev information */
  622. dma_dev = pci_get_slot(priv->pdev->bus,
  623. PCI_DEVFN(PCI_SLOT(priv->pdev->devfn), 0));
  624. /* Set Tx DMA */
  625. param = &priv->param_tx;
  626. param->dma_dev = &dma_dev->dev;
  627. param->chan_id = priv->port.line * 2; /* Tx = 0, 2, 4, ... */
  628. param->tx_reg = port->mapbase + UART_TX;
  629. chan = dma_request_channel(mask, filter, param);
  630. if (!chan) {
  631. dev_err(priv->port.dev, "%s:dma_request_channel FAILS(Tx)\n",
  632. __func__);
  633. return;
  634. }
  635. priv->chan_tx = chan;
  636. /* Set Rx DMA */
  637. param = &priv->param_rx;
  638. param->dma_dev = &dma_dev->dev;
  639. param->chan_id = priv->port.line * 2 + 1; /* Rx = Tx + 1 */
  640. param->rx_reg = port->mapbase + UART_RX;
  641. chan = dma_request_channel(mask, filter, param);
  642. if (!chan) {
  643. dev_err(priv->port.dev, "%s:dma_request_channel FAILS(Rx)\n",
  644. __func__);
  645. dma_release_channel(priv->chan_tx);
  646. priv->chan_tx = NULL;
  647. return;
  648. }
  649. /* Get Consistent memory for DMA */
  650. priv->rx_buf_virt = dma_alloc_coherent(port->dev, port->fifosize,
  651. &priv->rx_buf_dma, GFP_KERNEL);
  652. priv->chan_rx = chan;
  653. }
  654. static void pch_dma_rx_complete(void *arg)
  655. {
  656. struct eg20t_port *priv = arg;
  657. struct uart_port *port = &priv->port;
  658. int count;
  659. dma_sync_sg_for_cpu(port->dev, &priv->sg_rx, 1, DMA_FROM_DEVICE);
  660. count = dma_push_rx(priv, priv->trigger_level);
  661. if (count)
  662. tty_flip_buffer_push(&port->state->port);
  663. async_tx_ack(priv->desc_rx);
  664. pch_uart_hal_enable_interrupt(priv, PCH_UART_HAL_RX_INT |
  665. PCH_UART_HAL_RX_ERR_INT);
  666. }
  667. static void pch_dma_tx_complete(void *arg)
  668. {
  669. struct eg20t_port *priv = arg;
  670. struct uart_port *port = &priv->port;
  671. struct circ_buf *xmit = &port->state->xmit;
  672. struct scatterlist *sg = priv->sg_tx_p;
  673. int i;
  674. for (i = 0; i < priv->nent; i++, sg++) {
  675. xmit->tail += sg_dma_len(sg);
  676. port->icount.tx += sg_dma_len(sg);
  677. }
  678. xmit->tail &= UART_XMIT_SIZE - 1;
  679. async_tx_ack(priv->desc_tx);
  680. dma_unmap_sg(port->dev, sg, priv->orig_nent, DMA_TO_DEVICE);
  681. priv->tx_dma_use = 0;
  682. priv->nent = 0;
  683. priv->orig_nent = 0;
  684. kfree(priv->sg_tx_p);
  685. pch_uart_hal_enable_interrupt(priv, PCH_UART_HAL_TX_INT);
  686. }
  687. static int pop_tx(struct eg20t_port *priv, int size)
  688. {
  689. int count = 0;
  690. struct uart_port *port = &priv->port;
  691. struct circ_buf *xmit = &port->state->xmit;
  692. if (uart_tx_stopped(port) || uart_circ_empty(xmit) || count >= size)
  693. goto pop_tx_end;
  694. do {
  695. int cnt_to_end =
  696. CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
  697. int sz = min(size - count, cnt_to_end);
  698. pch_uart_hal_write(priv, &xmit->buf[xmit->tail], sz);
  699. xmit->tail = (xmit->tail + sz) & (UART_XMIT_SIZE - 1);
  700. count += sz;
  701. } while (!uart_circ_empty(xmit) && count < size);
  702. pop_tx_end:
  703. dev_dbg(priv->port.dev, "%d characters. Remained %d characters.(%lu)\n",
  704. count, size - count, jiffies);
  705. return count;
  706. }
  707. static int handle_rx_to(struct eg20t_port *priv)
  708. {
  709. struct pch_uart_buffer *buf;
  710. int rx_size;
  711. int ret;
  712. if (!priv->start_rx) {
  713. pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_RX_INT |
  714. PCH_UART_HAL_RX_ERR_INT);
  715. return 0;
  716. }
  717. buf = &priv->rxbuf;
  718. do {
  719. rx_size = pch_uart_hal_read(priv, buf->buf, buf->size);
  720. ret = push_rx(priv, buf->buf, rx_size);
  721. if (ret)
  722. return 0;
  723. } while (rx_size == buf->size);
  724. return PCH_UART_HANDLED_RX_INT;
  725. }
  726. static int handle_rx(struct eg20t_port *priv)
  727. {
  728. return handle_rx_to(priv);
  729. }
  730. static int dma_handle_rx(struct eg20t_port *priv)
  731. {
  732. struct uart_port *port = &priv->port;
  733. struct dma_async_tx_descriptor *desc;
  734. struct scatterlist *sg;
  735. priv = container_of(port, struct eg20t_port, port);
  736. sg = &priv->sg_rx;
  737. sg_init_table(&priv->sg_rx, 1); /* Initialize SG table */
  738. sg_dma_len(sg) = priv->trigger_level;
  739. sg_set_page(&priv->sg_rx, virt_to_page(priv->rx_buf_virt),
  740. sg_dma_len(sg), offset_in_page(priv->rx_buf_virt));
  741. sg_dma_address(sg) = priv->rx_buf_dma;
  742. desc = dmaengine_prep_slave_sg(priv->chan_rx,
  743. sg, 1, DMA_DEV_TO_MEM,
  744. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  745. if (!desc)
  746. return 0;
  747. priv->desc_rx = desc;
  748. desc->callback = pch_dma_rx_complete;
  749. desc->callback_param = priv;
  750. desc->tx_submit(desc);
  751. dma_async_issue_pending(priv->chan_rx);
  752. return PCH_UART_HANDLED_RX_INT;
  753. }
  754. static unsigned int handle_tx(struct eg20t_port *priv)
  755. {
  756. struct uart_port *port = &priv->port;
  757. struct circ_buf *xmit = &port->state->xmit;
  758. int fifo_size;
  759. int tx_size;
  760. int size;
  761. int tx_empty;
  762. if (!priv->start_tx) {
  763. dev_info(priv->port.dev, "%s:Tx isn't started. (%lu)\n",
  764. __func__, jiffies);
  765. pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_TX_INT);
  766. priv->tx_empty = 1;
  767. return 0;
  768. }
  769. fifo_size = max(priv->fifo_size, 1);
  770. tx_empty = 1;
  771. if (pop_tx_x(priv, xmit->buf)) {
  772. pch_uart_hal_write(priv, xmit->buf, 1);
  773. port->icount.tx++;
  774. tx_empty = 0;
  775. fifo_size--;
  776. }
  777. size = min(xmit->head - xmit->tail, fifo_size);
  778. if (size < 0)
  779. size = fifo_size;
  780. tx_size = pop_tx(priv, size);
  781. if (tx_size > 0) {
  782. port->icount.tx += tx_size;
  783. tx_empty = 0;
  784. }
  785. priv->tx_empty = tx_empty;
  786. if (tx_empty) {
  787. pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_TX_INT);
  788. uart_write_wakeup(port);
  789. }
  790. return PCH_UART_HANDLED_TX_INT;
  791. }
  792. static unsigned int dma_handle_tx(struct eg20t_port *priv)
  793. {
  794. struct uart_port *port = &priv->port;
  795. struct circ_buf *xmit = &port->state->xmit;
  796. struct scatterlist *sg;
  797. int nent;
  798. int fifo_size;
  799. int tx_empty;
  800. struct dma_async_tx_descriptor *desc;
  801. int num;
  802. int i;
  803. int bytes;
  804. int size;
  805. int rem;
  806. if (!priv->start_tx) {
  807. dev_info(priv->port.dev, "%s:Tx isn't started. (%lu)\n",
  808. __func__, jiffies);
  809. pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_TX_INT);
  810. priv->tx_empty = 1;
  811. return 0;
  812. }
  813. if (priv->tx_dma_use) {
  814. dev_dbg(priv->port.dev, "%s:Tx is not completed. (%lu)\n",
  815. __func__, jiffies);
  816. pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_TX_INT);
  817. priv->tx_empty = 1;
  818. return 0;
  819. }
  820. fifo_size = max(priv->fifo_size, 1);
  821. tx_empty = 1;
  822. if (pop_tx_x(priv, xmit->buf)) {
  823. pch_uart_hal_write(priv, xmit->buf, 1);
  824. port->icount.tx++;
  825. tx_empty = 0;
  826. fifo_size--;
  827. }
  828. bytes = min((int)CIRC_CNT(xmit->head, xmit->tail,
  829. UART_XMIT_SIZE), CIRC_CNT_TO_END(xmit->head,
  830. xmit->tail, UART_XMIT_SIZE));
  831. if (!bytes) {
  832. dev_dbg(priv->port.dev, "%s 0 bytes return\n", __func__);
  833. pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_TX_INT);
  834. uart_write_wakeup(port);
  835. return 0;
  836. }
  837. if (bytes > fifo_size) {
  838. num = bytes / fifo_size + 1;
  839. size = fifo_size;
  840. rem = bytes % fifo_size;
  841. } else {
  842. num = 1;
  843. size = bytes;
  844. rem = bytes;
  845. }
  846. dev_dbg(priv->port.dev, "%s num=%d size=%d rem=%d\n",
  847. __func__, num, size, rem);
  848. priv->tx_dma_use = 1;
  849. priv->sg_tx_p = kcalloc(num, sizeof(struct scatterlist), GFP_ATOMIC);
  850. if (!priv->sg_tx_p) {
  851. dev_err(priv->port.dev, "%s:kzalloc Failed\n", __func__);
  852. return 0;
  853. }
  854. sg_init_table(priv->sg_tx_p, num); /* Initialize SG table */
  855. sg = priv->sg_tx_p;
  856. for (i = 0; i < num; i++, sg++) {
  857. if (i == (num - 1))
  858. sg_set_page(sg, virt_to_page(xmit->buf),
  859. rem, fifo_size * i);
  860. else
  861. sg_set_page(sg, virt_to_page(xmit->buf),
  862. size, fifo_size * i);
  863. }
  864. sg = priv->sg_tx_p;
  865. nent = dma_map_sg(port->dev, sg, num, DMA_TO_DEVICE);
  866. if (!nent) {
  867. dev_err(priv->port.dev, "%s:dma_map_sg Failed\n", __func__);
  868. return 0;
  869. }
  870. priv->orig_nent = num;
  871. priv->nent = nent;
  872. for (i = 0; i < nent; i++, sg++) {
  873. sg->offset = (xmit->tail & (UART_XMIT_SIZE - 1)) +
  874. fifo_size * i;
  875. sg_dma_address(sg) = (sg_dma_address(sg) &
  876. ~(UART_XMIT_SIZE - 1)) + sg->offset;
  877. if (i == (nent - 1))
  878. sg_dma_len(sg) = rem;
  879. else
  880. sg_dma_len(sg) = size;
  881. }
  882. desc = dmaengine_prep_slave_sg(priv->chan_tx,
  883. priv->sg_tx_p, nent, DMA_MEM_TO_DEV,
  884. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  885. if (!desc) {
  886. dev_err(priv->port.dev, "%s:dmaengine_prep_slave_sg Failed\n",
  887. __func__);
  888. return 0;
  889. }
  890. dma_sync_sg_for_device(port->dev, priv->sg_tx_p, nent, DMA_TO_DEVICE);
  891. priv->desc_tx = desc;
  892. desc->callback = pch_dma_tx_complete;
  893. desc->callback_param = priv;
  894. desc->tx_submit(desc);
  895. dma_async_issue_pending(priv->chan_tx);
  896. return PCH_UART_HANDLED_TX_INT;
  897. }
  898. static void pch_uart_err_ir(struct eg20t_port *priv, unsigned int lsr)
  899. {
  900. struct uart_port *port = &priv->port;
  901. struct tty_struct *tty = tty_port_tty_get(&port->state->port);
  902. char *error_msg[5] = {};
  903. int i = 0;
  904. if (lsr & PCH_UART_LSR_ERR)
  905. error_msg[i++] = "Error data in FIFO\n";
  906. if (lsr & UART_LSR_FE) {
  907. port->icount.frame++;
  908. error_msg[i++] = " Framing Error\n";
  909. }
  910. if (lsr & UART_LSR_PE) {
  911. port->icount.parity++;
  912. error_msg[i++] = " Parity Error\n";
  913. }
  914. if (lsr & UART_LSR_OE) {
  915. port->icount.overrun++;
  916. error_msg[i++] = " Overrun Error\n";
  917. }
  918. if (tty == NULL) {
  919. for (i = 0; error_msg[i] != NULL; i++)
  920. dev_err(&priv->pdev->dev, error_msg[i]);
  921. } else {
  922. tty_kref_put(tty);
  923. }
  924. }
  925. static irqreturn_t pch_uart_interrupt(int irq, void *dev_id)
  926. {
  927. struct eg20t_port *priv = dev_id;
  928. unsigned int handled;
  929. u8 lsr;
  930. int ret = 0;
  931. unsigned char iid;
  932. unsigned long flags;
  933. int next = 1;
  934. u8 msr;
  935. spin_lock_irqsave(&priv->lock, flags);
  936. handled = 0;
  937. while (next) {
  938. iid = pch_uart_hal_get_iid(priv);
  939. if (iid & PCH_UART_IIR_IP) /* No Interrupt */
  940. break;
  941. switch (iid) {
  942. case PCH_UART_IID_RLS: /* Receiver Line Status */
  943. lsr = pch_uart_hal_get_line_status(priv);
  944. if (lsr & (PCH_UART_LSR_ERR | UART_LSR_FE |
  945. UART_LSR_PE | UART_LSR_OE)) {
  946. pch_uart_err_ir(priv, lsr);
  947. ret = PCH_UART_HANDLED_RX_ERR_INT;
  948. } else {
  949. ret = PCH_UART_HANDLED_LS_INT;
  950. }
  951. break;
  952. case PCH_UART_IID_RDR: /* Received Data Ready */
  953. if (priv->use_dma) {
  954. pch_uart_hal_disable_interrupt(priv,
  955. PCH_UART_HAL_RX_INT |
  956. PCH_UART_HAL_RX_ERR_INT);
  957. ret = dma_handle_rx(priv);
  958. if (!ret)
  959. pch_uart_hal_enable_interrupt(priv,
  960. PCH_UART_HAL_RX_INT |
  961. PCH_UART_HAL_RX_ERR_INT);
  962. } else {
  963. ret = handle_rx(priv);
  964. }
  965. break;
  966. case PCH_UART_IID_RDR_TO: /* Received Data Ready
  967. (FIFO Timeout) */
  968. ret = handle_rx_to(priv);
  969. break;
  970. case PCH_UART_IID_THRE: /* Transmitter Holding Register
  971. Empty */
  972. if (priv->use_dma)
  973. ret = dma_handle_tx(priv);
  974. else
  975. ret = handle_tx(priv);
  976. break;
  977. case PCH_UART_IID_MS: /* Modem Status */
  978. msr = pch_uart_hal_get_modem(priv);
  979. next = 0; /* MS ir prioirty is the lowest. So, MS ir
  980. means final interrupt */
  981. if ((msr & UART_MSR_ANY_DELTA) == 0)
  982. break;
  983. ret |= PCH_UART_HANDLED_MS_INT;
  984. break;
  985. default: /* Never junp to this label */
  986. dev_err(priv->port.dev, "%s:iid=%02x (%lu)\n", __func__,
  987. iid, jiffies);
  988. ret = -1;
  989. next = 0;
  990. break;
  991. }
  992. handled |= (unsigned int)ret;
  993. }
  994. spin_unlock_irqrestore(&priv->lock, flags);
  995. return IRQ_RETVAL(handled);
  996. }
  997. /* This function tests whether the transmitter fifo and shifter for the port
  998. described by 'port' is empty. */
  999. static unsigned int pch_uart_tx_empty(struct uart_port *port)
  1000. {
  1001. struct eg20t_port *priv;
  1002. priv = container_of(port, struct eg20t_port, port);
  1003. if (priv->tx_empty)
  1004. return TIOCSER_TEMT;
  1005. else
  1006. return 0;
  1007. }
  1008. /* Returns the current state of modem control inputs. */
  1009. static unsigned int pch_uart_get_mctrl(struct uart_port *port)
  1010. {
  1011. struct eg20t_port *priv;
  1012. u8 modem;
  1013. unsigned int ret = 0;
  1014. priv = container_of(port, struct eg20t_port, port);
  1015. modem = pch_uart_hal_get_modem(priv);
  1016. if (modem & UART_MSR_DCD)
  1017. ret |= TIOCM_CAR;
  1018. if (modem & UART_MSR_RI)
  1019. ret |= TIOCM_RNG;
  1020. if (modem & UART_MSR_DSR)
  1021. ret |= TIOCM_DSR;
  1022. if (modem & UART_MSR_CTS)
  1023. ret |= TIOCM_CTS;
  1024. return ret;
  1025. }
  1026. static void pch_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
  1027. {
  1028. u32 mcr = 0;
  1029. struct eg20t_port *priv = container_of(port, struct eg20t_port, port);
  1030. if (mctrl & TIOCM_DTR)
  1031. mcr |= UART_MCR_DTR;
  1032. if (mctrl & TIOCM_RTS)
  1033. mcr |= UART_MCR_RTS;
  1034. if (mctrl & TIOCM_LOOP)
  1035. mcr |= UART_MCR_LOOP;
  1036. if (priv->mcr & UART_MCR_AFE)
  1037. mcr |= UART_MCR_AFE;
  1038. if (mctrl)
  1039. iowrite8(mcr, priv->membase + UART_MCR);
  1040. }
  1041. static void pch_uart_stop_tx(struct uart_port *port)
  1042. {
  1043. struct eg20t_port *priv;
  1044. priv = container_of(port, struct eg20t_port, port);
  1045. priv->start_tx = 0;
  1046. priv->tx_dma_use = 0;
  1047. }
  1048. static void pch_uart_start_tx(struct uart_port *port)
  1049. {
  1050. struct eg20t_port *priv;
  1051. priv = container_of(port, struct eg20t_port, port);
  1052. if (priv->use_dma) {
  1053. if (priv->tx_dma_use) {
  1054. dev_dbg(priv->port.dev, "%s : Tx DMA is NOT empty.\n",
  1055. __func__);
  1056. return;
  1057. }
  1058. }
  1059. priv->start_tx = 1;
  1060. pch_uart_hal_enable_interrupt(priv, PCH_UART_HAL_TX_INT);
  1061. }
  1062. static void pch_uart_stop_rx(struct uart_port *port)
  1063. {
  1064. struct eg20t_port *priv;
  1065. priv = container_of(port, struct eg20t_port, port);
  1066. priv->start_rx = 0;
  1067. pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_RX_INT |
  1068. PCH_UART_HAL_RX_ERR_INT);
  1069. }
  1070. /* Enable the modem status interrupts. */
  1071. static void pch_uart_enable_ms(struct uart_port *port)
  1072. {
  1073. struct eg20t_port *priv;
  1074. priv = container_of(port, struct eg20t_port, port);
  1075. pch_uart_hal_enable_interrupt(priv, PCH_UART_HAL_MS_INT);
  1076. }
  1077. /* Control the transmission of a break signal. */
  1078. static void pch_uart_break_ctl(struct uart_port *port, int ctl)
  1079. {
  1080. struct eg20t_port *priv;
  1081. unsigned long flags;
  1082. priv = container_of(port, struct eg20t_port, port);
  1083. spin_lock_irqsave(&priv->lock, flags);
  1084. pch_uart_hal_set_break(priv, ctl);
  1085. spin_unlock_irqrestore(&priv->lock, flags);
  1086. }
  1087. /* Grab any interrupt resources and initialise any low level driver state. */
  1088. static int pch_uart_startup(struct uart_port *port)
  1089. {
  1090. struct eg20t_port *priv;
  1091. int ret;
  1092. int fifo_size;
  1093. int trigger_level;
  1094. priv = container_of(port, struct eg20t_port, port);
  1095. priv->tx_empty = 1;
  1096. if (port->uartclk)
  1097. priv->uartclk = port->uartclk;
  1098. else
  1099. port->uartclk = priv->uartclk;
  1100. pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_ALL_INT);
  1101. ret = pch_uart_hal_set_line(priv, default_baud,
  1102. PCH_UART_HAL_PARITY_NONE, PCH_UART_HAL_8BIT,
  1103. PCH_UART_HAL_STB1);
  1104. if (ret)
  1105. return ret;
  1106. switch (priv->fifo_size) {
  1107. case 256:
  1108. fifo_size = PCH_UART_HAL_FIFO256;
  1109. break;
  1110. case 64:
  1111. fifo_size = PCH_UART_HAL_FIFO64;
  1112. break;
  1113. case 16:
  1114. fifo_size = PCH_UART_HAL_FIFO16;
  1115. break;
  1116. case 1:
  1117. default:
  1118. fifo_size = PCH_UART_HAL_FIFO_DIS;
  1119. break;
  1120. }
  1121. switch (priv->trigger) {
  1122. case PCH_UART_HAL_TRIGGER1:
  1123. trigger_level = 1;
  1124. break;
  1125. case PCH_UART_HAL_TRIGGER_L:
  1126. trigger_level = priv->fifo_size / 4;
  1127. break;
  1128. case PCH_UART_HAL_TRIGGER_M:
  1129. trigger_level = priv->fifo_size / 2;
  1130. break;
  1131. case PCH_UART_HAL_TRIGGER_H:
  1132. default:
  1133. trigger_level = priv->fifo_size - (priv->fifo_size / 8);
  1134. break;
  1135. }
  1136. priv->trigger_level = trigger_level;
  1137. ret = pch_uart_hal_set_fifo(priv, PCH_UART_HAL_DMA_MODE0,
  1138. fifo_size, priv->trigger);
  1139. if (ret < 0)
  1140. return ret;
  1141. ret = request_irq(priv->port.irq, pch_uart_interrupt, IRQF_SHARED,
  1142. priv->irq_name, priv);
  1143. if (ret < 0)
  1144. return ret;
  1145. if (priv->use_dma)
  1146. pch_request_dma(port);
  1147. priv->start_rx = 1;
  1148. pch_uart_hal_enable_interrupt(priv, PCH_UART_HAL_RX_INT |
  1149. PCH_UART_HAL_RX_ERR_INT);
  1150. uart_update_timeout(port, CS8, default_baud);
  1151. return 0;
  1152. }
  1153. static void pch_uart_shutdown(struct uart_port *port)
  1154. {
  1155. struct eg20t_port *priv;
  1156. int ret;
  1157. priv = container_of(port, struct eg20t_port, port);
  1158. pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_ALL_INT);
  1159. pch_uart_hal_fifo_reset(priv, PCH_UART_HAL_CLR_ALL_FIFO);
  1160. ret = pch_uart_hal_set_fifo(priv, PCH_UART_HAL_DMA_MODE0,
  1161. PCH_UART_HAL_FIFO_DIS, PCH_UART_HAL_TRIGGER1);
  1162. if (ret)
  1163. dev_err(priv->port.dev,
  1164. "pch_uart_hal_set_fifo Failed(ret=%d)\n", ret);
  1165. pch_free_dma(port);
  1166. free_irq(priv->port.irq, priv);
  1167. }
  1168. /* Change the port parameters, including word length, parity, stop
  1169. *bits. Update read_status_mask and ignore_status_mask to indicate
  1170. *the types of events we are interested in receiving. */
  1171. static void pch_uart_set_termios(struct uart_port *port,
  1172. struct ktermios *termios, struct ktermios *old)
  1173. {
  1174. int rtn;
  1175. unsigned int baud, parity, bits, stb;
  1176. struct eg20t_port *priv;
  1177. unsigned long flags;
  1178. priv = container_of(port, struct eg20t_port, port);
  1179. switch (termios->c_cflag & CSIZE) {
  1180. case CS5:
  1181. bits = PCH_UART_HAL_5BIT;
  1182. break;
  1183. case CS6:
  1184. bits = PCH_UART_HAL_6BIT;
  1185. break;
  1186. case CS7:
  1187. bits = PCH_UART_HAL_7BIT;
  1188. break;
  1189. default: /* CS8 */
  1190. bits = PCH_UART_HAL_8BIT;
  1191. break;
  1192. }
  1193. if (termios->c_cflag & CSTOPB)
  1194. stb = PCH_UART_HAL_STB2;
  1195. else
  1196. stb = PCH_UART_HAL_STB1;
  1197. if (termios->c_cflag & PARENB) {
  1198. if (termios->c_cflag & PARODD)
  1199. parity = PCH_UART_HAL_PARITY_ODD;
  1200. else
  1201. parity = PCH_UART_HAL_PARITY_EVEN;
  1202. } else
  1203. parity = PCH_UART_HAL_PARITY_NONE;
  1204. /* Only UART0 has auto hardware flow function */
  1205. if ((termios->c_cflag & CRTSCTS) && (priv->fifo_size == 256))
  1206. priv->mcr |= UART_MCR_AFE;
  1207. else
  1208. priv->mcr &= ~UART_MCR_AFE;
  1209. termios->c_cflag &= ~CMSPAR; /* Mark/Space parity is not supported */
  1210. baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 16);
  1211. spin_lock_irqsave(&priv->lock, flags);
  1212. spin_lock(&port->lock);
  1213. uart_update_timeout(port, termios->c_cflag, baud);
  1214. rtn = pch_uart_hal_set_line(priv, baud, parity, bits, stb);
  1215. if (rtn)
  1216. goto out;
  1217. pch_uart_set_mctrl(&priv->port, priv->port.mctrl);
  1218. /* Don't rewrite B0 */
  1219. if (tty_termios_baud_rate(termios))
  1220. tty_termios_encode_baud_rate(termios, baud, baud);
  1221. out:
  1222. spin_unlock(&port->lock);
  1223. spin_unlock_irqrestore(&priv->lock, flags);
  1224. }
  1225. static const char *pch_uart_type(struct uart_port *port)
  1226. {
  1227. return KBUILD_MODNAME;
  1228. }
  1229. static void pch_uart_release_port(struct uart_port *port)
  1230. {
  1231. struct eg20t_port *priv;
  1232. priv = container_of(port, struct eg20t_port, port);
  1233. pci_iounmap(priv->pdev, priv->membase);
  1234. pci_release_regions(priv->pdev);
  1235. }
  1236. static int pch_uart_request_port(struct uart_port *port)
  1237. {
  1238. struct eg20t_port *priv;
  1239. int ret;
  1240. void __iomem *membase;
  1241. priv = container_of(port, struct eg20t_port, port);
  1242. ret = pci_request_regions(priv->pdev, KBUILD_MODNAME);
  1243. if (ret < 0)
  1244. return -EBUSY;
  1245. membase = pci_iomap(priv->pdev, 1, 0);
  1246. if (!membase) {
  1247. pci_release_regions(priv->pdev);
  1248. return -EBUSY;
  1249. }
  1250. priv->membase = port->membase = membase;
  1251. return 0;
  1252. }
  1253. static void pch_uart_config_port(struct uart_port *port, int type)
  1254. {
  1255. struct eg20t_port *priv;
  1256. priv = container_of(port, struct eg20t_port, port);
  1257. if (type & UART_CONFIG_TYPE) {
  1258. port->type = priv->port_type;
  1259. pch_uart_request_port(port);
  1260. }
  1261. }
  1262. static int pch_uart_verify_port(struct uart_port *port,
  1263. struct serial_struct *serinfo)
  1264. {
  1265. struct eg20t_port *priv;
  1266. priv = container_of(port, struct eg20t_port, port);
  1267. if (serinfo->flags & UPF_LOW_LATENCY) {
  1268. dev_info(priv->port.dev,
  1269. "PCH UART : Use PIO Mode (without DMA)\n");
  1270. priv->use_dma = 0;
  1271. serinfo->flags &= ~UPF_LOW_LATENCY;
  1272. } else {
  1273. #ifndef CONFIG_PCH_DMA
  1274. dev_err(priv->port.dev, "%s : PCH DMA is not Loaded.\n",
  1275. __func__);
  1276. return -EOPNOTSUPP;
  1277. #endif
  1278. if (!priv->use_dma) {
  1279. pch_request_dma(port);
  1280. if (priv->chan_rx)
  1281. priv->use_dma = 1;
  1282. }
  1283. dev_info(priv->port.dev, "PCH UART: %s\n",
  1284. priv->use_dma ?
  1285. "Use DMA Mode" : "No DMA");
  1286. }
  1287. return 0;
  1288. }
  1289. #if defined(CONFIG_CONSOLE_POLL) || defined(CONFIG_SERIAL_PCH_UART_CONSOLE)
  1290. /*
  1291. * Wait for transmitter & holding register to empty
  1292. */
  1293. static void wait_for_xmitr(struct eg20t_port *up, int bits)
  1294. {
  1295. unsigned int status, tmout = 10000;
  1296. /* Wait up to 10ms for the character(s) to be sent. */
  1297. for (;;) {
  1298. status = ioread8(up->membase + UART_LSR);
  1299. if ((status & bits) == bits)
  1300. break;
  1301. if (--tmout == 0)
  1302. break;
  1303. udelay(1);
  1304. }
  1305. /* Wait up to 1s for flow control if necessary */
  1306. if (up->port.flags & UPF_CONS_FLOW) {
  1307. unsigned int tmout;
  1308. for (tmout = 1000000; tmout; tmout--) {
  1309. unsigned int msr = ioread8(up->membase + UART_MSR);
  1310. if (msr & UART_MSR_CTS)
  1311. break;
  1312. udelay(1);
  1313. touch_nmi_watchdog();
  1314. }
  1315. }
  1316. }
  1317. #endif /* CONFIG_CONSOLE_POLL || CONFIG_SERIAL_PCH_UART_CONSOLE */
  1318. #ifdef CONFIG_CONSOLE_POLL
  1319. /*
  1320. * Console polling routines for communicate via uart while
  1321. * in an interrupt or debug context.
  1322. */
  1323. static int pch_uart_get_poll_char(struct uart_port *port)
  1324. {
  1325. struct eg20t_port *priv =
  1326. container_of(port, struct eg20t_port, port);
  1327. u8 lsr = ioread8(priv->membase + UART_LSR);
  1328. if (!(lsr & UART_LSR_DR))
  1329. return NO_POLL_CHAR;
  1330. return ioread8(priv->membase + PCH_UART_RBR);
  1331. }
  1332. static void pch_uart_put_poll_char(struct uart_port *port,
  1333. unsigned char c)
  1334. {
  1335. unsigned int ier;
  1336. struct eg20t_port *priv =
  1337. container_of(port, struct eg20t_port, port);
  1338. /*
  1339. * First save the IER then disable the interrupts
  1340. */
  1341. ier = ioread8(priv->membase + UART_IER);
  1342. pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_ALL_INT);
  1343. wait_for_xmitr(priv, UART_LSR_THRE);
  1344. /*
  1345. * Send the character out.
  1346. */
  1347. iowrite8(c, priv->membase + PCH_UART_THR);
  1348. /*
  1349. * Finally, wait for transmitter to become empty
  1350. * and restore the IER
  1351. */
  1352. wait_for_xmitr(priv, BOTH_EMPTY);
  1353. iowrite8(ier, priv->membase + UART_IER);
  1354. }
  1355. #endif /* CONFIG_CONSOLE_POLL */
  1356. static const struct uart_ops pch_uart_ops = {
  1357. .tx_empty = pch_uart_tx_empty,
  1358. .set_mctrl = pch_uart_set_mctrl,
  1359. .get_mctrl = pch_uart_get_mctrl,
  1360. .stop_tx = pch_uart_stop_tx,
  1361. .start_tx = pch_uart_start_tx,
  1362. .stop_rx = pch_uart_stop_rx,
  1363. .enable_ms = pch_uart_enable_ms,
  1364. .break_ctl = pch_uart_break_ctl,
  1365. .startup = pch_uart_startup,
  1366. .shutdown = pch_uart_shutdown,
  1367. .set_termios = pch_uart_set_termios,
  1368. /* .pm = pch_uart_pm, Not supported yet */
  1369. .type = pch_uart_type,
  1370. .release_port = pch_uart_release_port,
  1371. .request_port = pch_uart_request_port,
  1372. .config_port = pch_uart_config_port,
  1373. .verify_port = pch_uart_verify_port,
  1374. #ifdef CONFIG_CONSOLE_POLL
  1375. .poll_get_char = pch_uart_get_poll_char,
  1376. .poll_put_char = pch_uart_put_poll_char,
  1377. #endif
  1378. };
  1379. #ifdef CONFIG_SERIAL_PCH_UART_CONSOLE
  1380. static void pch_console_putchar(struct uart_port *port, int ch)
  1381. {
  1382. struct eg20t_port *priv =
  1383. container_of(port, struct eg20t_port, port);
  1384. wait_for_xmitr(priv, UART_LSR_THRE);
  1385. iowrite8(ch, priv->membase + PCH_UART_THR);
  1386. }
  1387. /*
  1388. * Print a string to the serial port trying not to disturb
  1389. * any possible real use of the port...
  1390. *
  1391. * The console_lock must be held when we get here.
  1392. */
  1393. static void
  1394. pch_console_write(struct console *co, const char *s, unsigned int count)
  1395. {
  1396. struct eg20t_port *priv;
  1397. unsigned long flags;
  1398. int priv_locked = 1;
  1399. int port_locked = 1;
  1400. u8 ier;
  1401. priv = pch_uart_ports[co->index];
  1402. touch_nmi_watchdog();
  1403. local_irq_save(flags);
  1404. if (priv->port.sysrq) {
  1405. /* call to uart_handle_sysrq_char already took the priv lock */
  1406. priv_locked = 0;
  1407. /* serial8250_handle_port() already took the port lock */
  1408. port_locked = 0;
  1409. } else if (oops_in_progress) {
  1410. priv_locked = spin_trylock(&priv->lock);
  1411. port_locked = spin_trylock(&priv->port.lock);
  1412. } else {
  1413. spin_lock(&priv->lock);
  1414. spin_lock(&priv->port.lock);
  1415. }
  1416. /*
  1417. * First save the IER then disable the interrupts
  1418. */
  1419. ier = ioread8(priv->membase + UART_IER);
  1420. pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_ALL_INT);
  1421. uart_console_write(&priv->port, s, count, pch_console_putchar);
  1422. /*
  1423. * Finally, wait for transmitter to become empty
  1424. * and restore the IER
  1425. */
  1426. wait_for_xmitr(priv, BOTH_EMPTY);
  1427. iowrite8(ier, priv->membase + UART_IER);
  1428. if (port_locked)
  1429. spin_unlock(&priv->port.lock);
  1430. if (priv_locked)
  1431. spin_unlock(&priv->lock);
  1432. local_irq_restore(flags);
  1433. }
  1434. static int __init pch_console_setup(struct console *co, char *options)
  1435. {
  1436. struct uart_port *port;
  1437. int baud = default_baud;
  1438. int bits = 8;
  1439. int parity = 'n';
  1440. int flow = 'n';
  1441. /*
  1442. * Check whether an invalid uart number has been specified, and
  1443. * if so, search for the first available port that does have
  1444. * console support.
  1445. */
  1446. if (co->index >= PCH_UART_NR)
  1447. co->index = 0;
  1448. port = &pch_uart_ports[co->index]->port;
  1449. if (!port || (!port->iobase && !port->membase))
  1450. return -ENODEV;
  1451. port->uartclk = pch_uart_get_uartclk();
  1452. if (options)
  1453. uart_parse_options(options, &baud, &parity, &bits, &flow);
  1454. return uart_set_options(port, co, baud, parity, bits, flow);
  1455. }
  1456. static struct uart_driver pch_uart_driver;
  1457. static struct console pch_console = {
  1458. .name = PCH_UART_DRIVER_DEVICE,
  1459. .write = pch_console_write,
  1460. .device = uart_console_device,
  1461. .setup = pch_console_setup,
  1462. .flags = CON_PRINTBUFFER | CON_ANYTIME,
  1463. .index = -1,
  1464. .data = &pch_uart_driver,
  1465. };
  1466. #define PCH_CONSOLE (&pch_console)
  1467. #else
  1468. #define PCH_CONSOLE NULL
  1469. #endif /* CONFIG_SERIAL_PCH_UART_CONSOLE */
  1470. static struct uart_driver pch_uart_driver = {
  1471. .owner = THIS_MODULE,
  1472. .driver_name = KBUILD_MODNAME,
  1473. .dev_name = PCH_UART_DRIVER_DEVICE,
  1474. .major = 0,
  1475. .minor = 0,
  1476. .nr = PCH_UART_NR,
  1477. .cons = PCH_CONSOLE,
  1478. };
  1479. static struct eg20t_port *pch_uart_init_port(struct pci_dev *pdev,
  1480. const struct pci_device_id *id)
  1481. {
  1482. struct eg20t_port *priv;
  1483. int ret;
  1484. unsigned int iobase;
  1485. unsigned int mapbase;
  1486. unsigned char *rxbuf;
  1487. int fifosize;
  1488. int port_type;
  1489. struct pch_uart_driver_data *board;
  1490. #ifdef CONFIG_DEBUG_FS
  1491. char name[32]; /* for debugfs file name */
  1492. #endif
  1493. board = &drv_dat[id->driver_data];
  1494. port_type = board->port_type;
  1495. priv = kzalloc(sizeof(struct eg20t_port), GFP_KERNEL);
  1496. if (priv == NULL)
  1497. goto init_port_alloc_err;
  1498. rxbuf = (unsigned char *)__get_free_page(GFP_KERNEL);
  1499. if (!rxbuf)
  1500. goto init_port_free_txbuf;
  1501. switch (port_type) {
  1502. case PORT_PCH_8LINE:
  1503. fifosize = 256; /* EG20T/ML7213: UART0 */
  1504. break;
  1505. case PORT_PCH_2LINE:
  1506. fifosize = 64; /* EG20T:UART1~3 ML7213: UART1~2*/
  1507. break;
  1508. default:
  1509. dev_err(&pdev->dev, "Invalid Port Type(=%d)\n", port_type);
  1510. goto init_port_hal_free;
  1511. }
  1512. pci_enable_msi(pdev);
  1513. pci_set_master(pdev);
  1514. spin_lock_init(&priv->lock);
  1515. iobase = pci_resource_start(pdev, 0);
  1516. mapbase = pci_resource_start(pdev, 1);
  1517. priv->mapbase = mapbase;
  1518. priv->iobase = iobase;
  1519. priv->pdev = pdev;
  1520. priv->tx_empty = 1;
  1521. priv->rxbuf.buf = rxbuf;
  1522. priv->rxbuf.size = PAGE_SIZE;
  1523. priv->fifo_size = fifosize;
  1524. priv->uartclk = pch_uart_get_uartclk();
  1525. priv->port_type = port_type;
  1526. priv->port.dev = &pdev->dev;
  1527. priv->port.iobase = iobase;
  1528. priv->port.membase = NULL;
  1529. priv->port.mapbase = mapbase;
  1530. priv->port.irq = pdev->irq;
  1531. priv->port.iotype = UPIO_PORT;
  1532. priv->port.ops = &pch_uart_ops;
  1533. priv->port.flags = UPF_BOOT_AUTOCONF;
  1534. priv->port.fifosize = fifosize;
  1535. priv->port.line = board->line_no;
  1536. priv->trigger = PCH_UART_HAL_TRIGGER_M;
  1537. snprintf(priv->irq_name, IRQ_NAME_SIZE,
  1538. KBUILD_MODNAME ":" PCH_UART_DRIVER_DEVICE "%d",
  1539. priv->port.line);
  1540. spin_lock_init(&priv->port.lock);
  1541. pci_set_drvdata(pdev, priv);
  1542. priv->trigger_level = 1;
  1543. priv->fcr = 0;
  1544. if (pdev->dev.of_node)
  1545. of_property_read_u32(pdev->dev.of_node, "clock-frequency"
  1546. , &user_uartclk);
  1547. #ifdef CONFIG_SERIAL_PCH_UART_CONSOLE
  1548. pch_uart_ports[board->line_no] = priv;
  1549. #endif
  1550. ret = uart_add_one_port(&pch_uart_driver, &priv->port);
  1551. if (ret < 0)
  1552. goto init_port_hal_free;
  1553. #ifdef CONFIG_DEBUG_FS
  1554. snprintf(name, sizeof(name), "uart%d_regs", board->line_no);
  1555. priv->debugfs = debugfs_create_file(name, S_IFREG | S_IRUGO,
  1556. NULL, priv, &port_regs_ops);
  1557. #endif
  1558. return priv;
  1559. init_port_hal_free:
  1560. #ifdef CONFIG_SERIAL_PCH_UART_CONSOLE
  1561. pch_uart_ports[board->line_no] = NULL;
  1562. #endif
  1563. free_page((unsigned long)rxbuf);
  1564. init_port_free_txbuf:
  1565. kfree(priv);
  1566. init_port_alloc_err:
  1567. return NULL;
  1568. }
  1569. static void pch_uart_exit_port(struct eg20t_port *priv)
  1570. {
  1571. #ifdef CONFIG_DEBUG_FS
  1572. debugfs_remove(priv->debugfs);
  1573. #endif
  1574. uart_remove_one_port(&pch_uart_driver, &priv->port);
  1575. free_page((unsigned long)priv->rxbuf.buf);
  1576. }
  1577. static void pch_uart_pci_remove(struct pci_dev *pdev)
  1578. {
  1579. struct eg20t_port *priv = pci_get_drvdata(pdev);
  1580. pci_disable_msi(pdev);
  1581. #ifdef CONFIG_SERIAL_PCH_UART_CONSOLE
  1582. pch_uart_ports[priv->port.line] = NULL;
  1583. #endif
  1584. pch_uart_exit_port(priv);
  1585. pci_disable_device(pdev);
  1586. kfree(priv);
  1587. return;
  1588. }
  1589. #ifdef CONFIG_PM
  1590. static int pch_uart_pci_suspend(struct pci_dev *pdev, pm_message_t state)
  1591. {
  1592. struct eg20t_port *priv = pci_get_drvdata(pdev);
  1593. uart_suspend_port(&pch_uart_driver, &priv->port);
  1594. pci_save_state(pdev);
  1595. pci_set_power_state(pdev, pci_choose_state(pdev, state));
  1596. return 0;
  1597. }
  1598. static int pch_uart_pci_resume(struct pci_dev *pdev)
  1599. {
  1600. struct eg20t_port *priv = pci_get_drvdata(pdev);
  1601. int ret;
  1602. pci_set_power_state(pdev, PCI_D0);
  1603. pci_restore_state(pdev);
  1604. ret = pci_enable_device(pdev);
  1605. if (ret) {
  1606. dev_err(&pdev->dev,
  1607. "%s-pci_enable_device failed(ret=%d) ", __func__, ret);
  1608. return ret;
  1609. }
  1610. uart_resume_port(&pch_uart_driver, &priv->port);
  1611. return 0;
  1612. }
  1613. #else
  1614. #define pch_uart_pci_suspend NULL
  1615. #define pch_uart_pci_resume NULL
  1616. #endif
  1617. static const struct pci_device_id pch_uart_pci_id[] = {
  1618. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x8811),
  1619. .driver_data = pch_et20t_uart0},
  1620. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x8812),
  1621. .driver_data = pch_et20t_uart1},
  1622. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x8813),
  1623. .driver_data = pch_et20t_uart2},
  1624. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x8814),
  1625. .driver_data = pch_et20t_uart3},
  1626. {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x8027),
  1627. .driver_data = pch_ml7213_uart0},
  1628. {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x8028),
  1629. .driver_data = pch_ml7213_uart1},
  1630. {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x8029),
  1631. .driver_data = pch_ml7213_uart2},
  1632. {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x800C),
  1633. .driver_data = pch_ml7223_uart0},
  1634. {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x800D),
  1635. .driver_data = pch_ml7223_uart1},
  1636. {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x8811),
  1637. .driver_data = pch_ml7831_uart0},
  1638. {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x8812),
  1639. .driver_data = pch_ml7831_uart1},
  1640. {0,},
  1641. };
  1642. static int pch_uart_pci_probe(struct pci_dev *pdev,
  1643. const struct pci_device_id *id)
  1644. {
  1645. int ret;
  1646. struct eg20t_port *priv;
  1647. ret = pci_enable_device(pdev);
  1648. if (ret < 0)
  1649. goto probe_error;
  1650. priv = pch_uart_init_port(pdev, id);
  1651. if (!priv) {
  1652. ret = -EBUSY;
  1653. goto probe_disable_device;
  1654. }
  1655. pci_set_drvdata(pdev, priv);
  1656. return ret;
  1657. probe_disable_device:
  1658. pci_disable_msi(pdev);
  1659. pci_disable_device(pdev);
  1660. probe_error:
  1661. return ret;
  1662. }
  1663. static struct pci_driver pch_uart_pci_driver = {
  1664. .name = "pch_uart",
  1665. .id_table = pch_uart_pci_id,
  1666. .probe = pch_uart_pci_probe,
  1667. .remove = pch_uart_pci_remove,
  1668. .suspend = pch_uart_pci_suspend,
  1669. .resume = pch_uart_pci_resume,
  1670. };
  1671. static int __init pch_uart_module_init(void)
  1672. {
  1673. int ret;
  1674. /* register as UART driver */
  1675. ret = uart_register_driver(&pch_uart_driver);
  1676. if (ret < 0)
  1677. return ret;
  1678. /* register as PCI driver */
  1679. ret = pci_register_driver(&pch_uart_pci_driver);
  1680. if (ret < 0)
  1681. uart_unregister_driver(&pch_uart_driver);
  1682. return ret;
  1683. }
  1684. module_init(pch_uart_module_init);
  1685. static void __exit pch_uart_module_exit(void)
  1686. {
  1687. pci_unregister_driver(&pch_uart_pci_driver);
  1688. uart_unregister_driver(&pch_uart_driver);
  1689. }
  1690. module_exit(pch_uart_module_exit);
  1691. MODULE_LICENSE("GPL v2");
  1692. MODULE_DESCRIPTION("Intel EG20T PCH UART PCI Driver");
  1693. MODULE_DEVICE_TABLE(pci, pch_uart_pci_id);
  1694. module_param(default_baud, uint, S_IRUGO);
  1695. MODULE_PARM_DESC(default_baud,
  1696. "Default BAUD for initial driver state and console (default 9600)");
  1697. module_param(user_uartclk, uint, S_IRUGO);
  1698. MODULE_PARM_DESC(user_uartclk,
  1699. "Override UART default or board specific UART clock");