pch_uart.c 50 KB

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