pch_uart.c 48 KB

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