pch_uart.c 47 KB

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