pch_uart.c 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910
  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. struct tty_struct *tty;
  512. tty = tty_port_tty_get(tport);
  513. if (!tty) {
  514. dev_dbg(priv->port.dev, "%s:tty is busy now", __func__);
  515. return -EBUSY;
  516. }
  517. tty_insert_flip_string(tport, buf, size);
  518. tty_flip_buffer_push(tty);
  519. tty_kref_put(tty);
  520. return 0;
  521. }
  522. static int pop_tx_x(struct eg20t_port *priv, unsigned char *buf)
  523. {
  524. int ret = 0;
  525. struct uart_port *port = &priv->port;
  526. if (port->x_char) {
  527. dev_dbg(priv->port.dev, "%s:X character send %02x (%lu)\n",
  528. __func__, port->x_char, jiffies);
  529. buf[0] = port->x_char;
  530. port->x_char = 0;
  531. ret = 1;
  532. }
  533. return ret;
  534. }
  535. static int dma_push_rx(struct eg20t_port *priv, int size)
  536. {
  537. struct tty_struct *tty;
  538. int room;
  539. struct uart_port *port = &priv->port;
  540. struct tty_port *tport = &port->state->port;
  541. port = &priv->port;
  542. tty = tty_port_tty_get(tport);
  543. if (!tty) {
  544. dev_dbg(priv->port.dev, "%s:tty is busy now", __func__);
  545. return 0;
  546. }
  547. room = tty_buffer_request_room(tport, size);
  548. if (room < size)
  549. dev_warn(port->dev, "Rx overrun: dropping %u bytes\n",
  550. size - room);
  551. if (!room)
  552. return room;
  553. tty_insert_flip_string(tport, sg_virt(&priv->sg_rx), size);
  554. port->icount.rx += room;
  555. tty_kref_put(tty);
  556. return room;
  557. }
  558. static void pch_free_dma(struct uart_port *port)
  559. {
  560. struct eg20t_port *priv;
  561. priv = container_of(port, struct eg20t_port, port);
  562. if (priv->chan_tx) {
  563. dma_release_channel(priv->chan_tx);
  564. priv->chan_tx = NULL;
  565. }
  566. if (priv->chan_rx) {
  567. dma_release_channel(priv->chan_rx);
  568. priv->chan_rx = NULL;
  569. }
  570. if (priv->rx_buf_dma) {
  571. dma_free_coherent(port->dev, port->fifosize, priv->rx_buf_virt,
  572. priv->rx_buf_dma);
  573. priv->rx_buf_virt = NULL;
  574. priv->rx_buf_dma = 0;
  575. }
  576. return;
  577. }
  578. static bool filter(struct dma_chan *chan, void *slave)
  579. {
  580. struct pch_dma_slave *param = slave;
  581. if ((chan->chan_id == param->chan_id) && (param->dma_dev ==
  582. chan->device->dev)) {
  583. chan->private = param;
  584. return true;
  585. } else {
  586. return false;
  587. }
  588. }
  589. static void pch_request_dma(struct uart_port *port)
  590. {
  591. dma_cap_mask_t mask;
  592. struct dma_chan *chan;
  593. struct pci_dev *dma_dev;
  594. struct pch_dma_slave *param;
  595. struct eg20t_port *priv =
  596. container_of(port, struct eg20t_port, port);
  597. dma_cap_zero(mask);
  598. dma_cap_set(DMA_SLAVE, mask);
  599. dma_dev = pci_get_bus_and_slot(priv->pdev->bus->number,
  600. PCI_DEVFN(0xa, 0)); /* Get DMA's dev
  601. information */
  602. /* Set Tx DMA */
  603. param = &priv->param_tx;
  604. param->dma_dev = &dma_dev->dev;
  605. param->chan_id = priv->port.line * 2; /* Tx = 0, 2, 4, ... */
  606. param->tx_reg = port->mapbase + UART_TX;
  607. chan = dma_request_channel(mask, filter, param);
  608. if (!chan) {
  609. dev_err(priv->port.dev, "%s:dma_request_channel FAILS(Tx)\n",
  610. __func__);
  611. return;
  612. }
  613. priv->chan_tx = chan;
  614. /* Set Rx DMA */
  615. param = &priv->param_rx;
  616. param->dma_dev = &dma_dev->dev;
  617. param->chan_id = priv->port.line * 2 + 1; /* Rx = Tx + 1 */
  618. param->rx_reg = port->mapbase + UART_RX;
  619. chan = dma_request_channel(mask, filter, param);
  620. if (!chan) {
  621. dev_err(priv->port.dev, "%s:dma_request_channel FAILS(Rx)\n",
  622. __func__);
  623. dma_release_channel(priv->chan_tx);
  624. priv->chan_tx = NULL;
  625. return;
  626. }
  627. /* Get Consistent memory for DMA */
  628. priv->rx_buf_virt = dma_alloc_coherent(port->dev, port->fifosize,
  629. &priv->rx_buf_dma, GFP_KERNEL);
  630. priv->chan_rx = chan;
  631. }
  632. static void pch_dma_rx_complete(void *arg)
  633. {
  634. struct eg20t_port *priv = arg;
  635. struct uart_port *port = &priv->port;
  636. struct tty_struct *tty = tty_port_tty_get(&port->state->port);
  637. int count;
  638. if (!tty) {
  639. dev_dbg(priv->port.dev, "%s:tty is busy now", __func__);
  640. return;
  641. }
  642. dma_sync_sg_for_cpu(port->dev, &priv->sg_rx, 1, DMA_FROM_DEVICE);
  643. count = dma_push_rx(priv, priv->trigger_level);
  644. if (count)
  645. tty_flip_buffer_push(tty);
  646. tty_kref_put(tty);
  647. async_tx_ack(priv->desc_rx);
  648. pch_uart_hal_enable_interrupt(priv, PCH_UART_HAL_RX_INT |
  649. PCH_UART_HAL_RX_ERR_INT);
  650. }
  651. static void pch_dma_tx_complete(void *arg)
  652. {
  653. struct eg20t_port *priv = arg;
  654. struct uart_port *port = &priv->port;
  655. struct circ_buf *xmit = &port->state->xmit;
  656. struct scatterlist *sg = priv->sg_tx_p;
  657. int i;
  658. for (i = 0; i < priv->nent; i++, sg++) {
  659. xmit->tail += sg_dma_len(sg);
  660. port->icount.tx += sg_dma_len(sg);
  661. }
  662. xmit->tail &= UART_XMIT_SIZE - 1;
  663. async_tx_ack(priv->desc_tx);
  664. dma_unmap_sg(port->dev, sg, priv->nent, DMA_TO_DEVICE);
  665. priv->tx_dma_use = 0;
  666. priv->nent = 0;
  667. kfree(priv->sg_tx_p);
  668. pch_uart_hal_enable_interrupt(priv, PCH_UART_HAL_TX_INT);
  669. }
  670. static int pop_tx(struct eg20t_port *priv, int size)
  671. {
  672. int count = 0;
  673. struct uart_port *port = &priv->port;
  674. struct circ_buf *xmit = &port->state->xmit;
  675. if (uart_tx_stopped(port) || uart_circ_empty(xmit) || count >= size)
  676. goto pop_tx_end;
  677. do {
  678. int cnt_to_end =
  679. CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
  680. int sz = min(size - count, cnt_to_end);
  681. pch_uart_hal_write(priv, &xmit->buf[xmit->tail], sz);
  682. xmit->tail = (xmit->tail + sz) & (UART_XMIT_SIZE - 1);
  683. count += sz;
  684. } while (!uart_circ_empty(xmit) && count < size);
  685. pop_tx_end:
  686. dev_dbg(priv->port.dev, "%d characters. Remained %d characters.(%lu)\n",
  687. count, size - count, jiffies);
  688. return count;
  689. }
  690. static int handle_rx_to(struct eg20t_port *priv)
  691. {
  692. struct pch_uart_buffer *buf;
  693. int rx_size;
  694. int ret;
  695. if (!priv->start_rx) {
  696. pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_RX_INT |
  697. PCH_UART_HAL_RX_ERR_INT);
  698. return 0;
  699. }
  700. buf = &priv->rxbuf;
  701. do {
  702. rx_size = pch_uart_hal_read(priv, buf->buf, buf->size);
  703. ret = push_rx(priv, buf->buf, rx_size);
  704. if (ret)
  705. return 0;
  706. } while (rx_size == buf->size);
  707. return PCH_UART_HANDLED_RX_INT;
  708. }
  709. static int handle_rx(struct eg20t_port *priv)
  710. {
  711. return handle_rx_to(priv);
  712. }
  713. static int dma_handle_rx(struct eg20t_port *priv)
  714. {
  715. struct uart_port *port = &priv->port;
  716. struct dma_async_tx_descriptor *desc;
  717. struct scatterlist *sg;
  718. priv = container_of(port, struct eg20t_port, port);
  719. sg = &priv->sg_rx;
  720. sg_init_table(&priv->sg_rx, 1); /* Initialize SG table */
  721. sg_dma_len(sg) = priv->trigger_level;
  722. sg_set_page(&priv->sg_rx, virt_to_page(priv->rx_buf_virt),
  723. sg_dma_len(sg), (unsigned long)priv->rx_buf_virt &
  724. ~PAGE_MASK);
  725. sg_dma_address(sg) = priv->rx_buf_dma;
  726. desc = dmaengine_prep_slave_sg(priv->chan_rx,
  727. sg, 1, DMA_DEV_TO_MEM,
  728. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  729. if (!desc)
  730. return 0;
  731. priv->desc_rx = desc;
  732. desc->callback = pch_dma_rx_complete;
  733. desc->callback_param = priv;
  734. desc->tx_submit(desc);
  735. dma_async_issue_pending(priv->chan_rx);
  736. return PCH_UART_HANDLED_RX_INT;
  737. }
  738. static unsigned int handle_tx(struct eg20t_port *priv)
  739. {
  740. struct uart_port *port = &priv->port;
  741. struct circ_buf *xmit = &port->state->xmit;
  742. int fifo_size;
  743. int tx_size;
  744. int size;
  745. int tx_empty;
  746. if (!priv->start_tx) {
  747. dev_info(priv->port.dev, "%s:Tx isn't started. (%lu)\n",
  748. __func__, jiffies);
  749. pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_TX_INT);
  750. priv->tx_empty = 1;
  751. return 0;
  752. }
  753. fifo_size = max(priv->fifo_size, 1);
  754. tx_empty = 1;
  755. if (pop_tx_x(priv, xmit->buf)) {
  756. pch_uart_hal_write(priv, xmit->buf, 1);
  757. port->icount.tx++;
  758. tx_empty = 0;
  759. fifo_size--;
  760. }
  761. size = min(xmit->head - xmit->tail, fifo_size);
  762. if (size < 0)
  763. size = fifo_size;
  764. tx_size = pop_tx(priv, size);
  765. if (tx_size > 0) {
  766. port->icount.tx += tx_size;
  767. tx_empty = 0;
  768. }
  769. priv->tx_empty = tx_empty;
  770. if (tx_empty) {
  771. pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_TX_INT);
  772. uart_write_wakeup(port);
  773. }
  774. return PCH_UART_HANDLED_TX_INT;
  775. }
  776. static unsigned int dma_handle_tx(struct eg20t_port *priv)
  777. {
  778. struct uart_port *port = &priv->port;
  779. struct circ_buf *xmit = &port->state->xmit;
  780. struct scatterlist *sg;
  781. int nent;
  782. int fifo_size;
  783. int tx_empty;
  784. struct dma_async_tx_descriptor *desc;
  785. int num;
  786. int i;
  787. int bytes;
  788. int size;
  789. int rem;
  790. if (!priv->start_tx) {
  791. dev_info(priv->port.dev, "%s:Tx isn't started. (%lu)\n",
  792. __func__, jiffies);
  793. pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_TX_INT);
  794. priv->tx_empty = 1;
  795. return 0;
  796. }
  797. if (priv->tx_dma_use) {
  798. dev_dbg(priv->port.dev, "%s:Tx is not completed. (%lu)\n",
  799. __func__, jiffies);
  800. pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_TX_INT);
  801. priv->tx_empty = 1;
  802. return 0;
  803. }
  804. fifo_size = max(priv->fifo_size, 1);
  805. tx_empty = 1;
  806. if (pop_tx_x(priv, xmit->buf)) {
  807. pch_uart_hal_write(priv, xmit->buf, 1);
  808. port->icount.tx++;
  809. tx_empty = 0;
  810. fifo_size--;
  811. }
  812. bytes = min((int)CIRC_CNT(xmit->head, xmit->tail,
  813. UART_XMIT_SIZE), CIRC_CNT_TO_END(xmit->head,
  814. xmit->tail, UART_XMIT_SIZE));
  815. if (!bytes) {
  816. dev_dbg(priv->port.dev, "%s 0 bytes return\n", __func__);
  817. pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_TX_INT);
  818. uart_write_wakeup(port);
  819. return 0;
  820. }
  821. if (bytes > fifo_size) {
  822. num = bytes / fifo_size + 1;
  823. size = fifo_size;
  824. rem = bytes % fifo_size;
  825. } else {
  826. num = 1;
  827. size = bytes;
  828. rem = bytes;
  829. }
  830. dev_dbg(priv->port.dev, "%s num=%d size=%d rem=%d\n",
  831. __func__, num, size, rem);
  832. priv->tx_dma_use = 1;
  833. priv->sg_tx_p = kzalloc(sizeof(struct scatterlist)*num, GFP_ATOMIC);
  834. if (!priv->sg_tx_p) {
  835. dev_err(priv->port.dev, "%s:kzalloc Failed\n", __func__);
  836. return 0;
  837. }
  838. sg_init_table(priv->sg_tx_p, num); /* Initialize SG table */
  839. sg = priv->sg_tx_p;
  840. for (i = 0; i < num; i++, sg++) {
  841. if (i == (num - 1))
  842. sg_set_page(sg, virt_to_page(xmit->buf),
  843. rem, fifo_size * i);
  844. else
  845. sg_set_page(sg, virt_to_page(xmit->buf),
  846. size, fifo_size * i);
  847. }
  848. sg = priv->sg_tx_p;
  849. nent = dma_map_sg(port->dev, sg, num, DMA_TO_DEVICE);
  850. if (!nent) {
  851. dev_err(priv->port.dev, "%s:dma_map_sg Failed\n", __func__);
  852. return 0;
  853. }
  854. priv->nent = nent;
  855. for (i = 0; i < nent; i++, sg++) {
  856. sg->offset = (xmit->tail & (UART_XMIT_SIZE - 1)) +
  857. fifo_size * i;
  858. sg_dma_address(sg) = (sg_dma_address(sg) &
  859. ~(UART_XMIT_SIZE - 1)) + sg->offset;
  860. if (i == (nent - 1))
  861. sg_dma_len(sg) = rem;
  862. else
  863. sg_dma_len(sg) = size;
  864. }
  865. desc = dmaengine_prep_slave_sg(priv->chan_tx,
  866. priv->sg_tx_p, nent, DMA_MEM_TO_DEV,
  867. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  868. if (!desc) {
  869. dev_err(priv->port.dev, "%s:device_prep_slave_sg Failed\n",
  870. __func__);
  871. return 0;
  872. }
  873. dma_sync_sg_for_device(port->dev, priv->sg_tx_p, nent, DMA_TO_DEVICE);
  874. priv->desc_tx = desc;
  875. desc->callback = pch_dma_tx_complete;
  876. desc->callback_param = priv;
  877. desc->tx_submit(desc);
  878. dma_async_issue_pending(priv->chan_tx);
  879. return PCH_UART_HANDLED_TX_INT;
  880. }
  881. static void pch_uart_err_ir(struct eg20t_port *priv, unsigned int lsr)
  882. {
  883. u8 fcr = ioread8(priv->membase + UART_FCR);
  884. /* Reset FIFO */
  885. fcr |= UART_FCR_CLEAR_RCVR;
  886. iowrite8(fcr, priv->membase + UART_FCR);
  887. if (lsr & PCH_UART_LSR_ERR)
  888. dev_err(&priv->pdev->dev, "Error data in FIFO\n");
  889. if (lsr & UART_LSR_FE)
  890. dev_err(&priv->pdev->dev, "Framing Error\n");
  891. if (lsr & UART_LSR_PE)
  892. dev_err(&priv->pdev->dev, "Parity Error\n");
  893. if (lsr & UART_LSR_OE)
  894. dev_err(&priv->pdev->dev, "Overrun Error\n");
  895. }
  896. static irqreturn_t pch_uart_interrupt(int irq, void *dev_id)
  897. {
  898. struct eg20t_port *priv = dev_id;
  899. unsigned int handled;
  900. u8 lsr;
  901. int ret = 0;
  902. unsigned char iid;
  903. unsigned long flags;
  904. int next = 1;
  905. u8 msr;
  906. spin_lock_irqsave(&priv->lock, flags);
  907. handled = 0;
  908. while (next) {
  909. iid = pch_uart_hal_get_iid(priv);
  910. if (iid & PCH_UART_IIR_IP) /* No Interrupt */
  911. break;
  912. switch (iid) {
  913. case PCH_UART_IID_RLS: /* Receiver Line Status */
  914. lsr = pch_uart_hal_get_line_status(priv);
  915. if (lsr & (PCH_UART_LSR_ERR | UART_LSR_FE |
  916. UART_LSR_PE | UART_LSR_OE)) {
  917. pch_uart_err_ir(priv, lsr);
  918. ret = PCH_UART_HANDLED_RX_ERR_INT;
  919. } else {
  920. ret = PCH_UART_HANDLED_LS_INT;
  921. }
  922. break;
  923. case PCH_UART_IID_RDR: /* Received Data Ready */
  924. if (priv->use_dma) {
  925. pch_uart_hal_disable_interrupt(priv,
  926. PCH_UART_HAL_RX_INT |
  927. PCH_UART_HAL_RX_ERR_INT);
  928. ret = dma_handle_rx(priv);
  929. if (!ret)
  930. pch_uart_hal_enable_interrupt(priv,
  931. PCH_UART_HAL_RX_INT |
  932. PCH_UART_HAL_RX_ERR_INT);
  933. } else {
  934. ret = handle_rx(priv);
  935. }
  936. break;
  937. case PCH_UART_IID_RDR_TO: /* Received Data Ready
  938. (FIFO Timeout) */
  939. ret = handle_rx_to(priv);
  940. break;
  941. case PCH_UART_IID_THRE: /* Transmitter Holding Register
  942. Empty */
  943. if (priv->use_dma)
  944. ret = dma_handle_tx(priv);
  945. else
  946. ret = handle_tx(priv);
  947. break;
  948. case PCH_UART_IID_MS: /* Modem Status */
  949. msr = pch_uart_hal_get_modem(priv);
  950. next = 0; /* MS ir prioirty is the lowest. So, MS ir
  951. means final interrupt */
  952. if ((msr & UART_MSR_ANY_DELTA) == 0)
  953. break;
  954. ret |= PCH_UART_HANDLED_MS_INT;
  955. break;
  956. default: /* Never junp to this label */
  957. dev_err(priv->port.dev, "%s:iid=%02x (%lu)\n", __func__,
  958. iid, jiffies);
  959. ret = -1;
  960. next = 0;
  961. break;
  962. }
  963. handled |= (unsigned int)ret;
  964. }
  965. spin_unlock_irqrestore(&priv->lock, flags);
  966. return IRQ_RETVAL(handled);
  967. }
  968. /* This function tests whether the transmitter fifo and shifter for the port
  969. described by 'port' is empty. */
  970. static unsigned int pch_uart_tx_empty(struct uart_port *port)
  971. {
  972. struct eg20t_port *priv;
  973. priv = container_of(port, struct eg20t_port, port);
  974. if (priv->tx_empty)
  975. return TIOCSER_TEMT;
  976. else
  977. return 0;
  978. }
  979. /* Returns the current state of modem control inputs. */
  980. static unsigned int pch_uart_get_mctrl(struct uart_port *port)
  981. {
  982. struct eg20t_port *priv;
  983. u8 modem;
  984. unsigned int ret = 0;
  985. priv = container_of(port, struct eg20t_port, port);
  986. modem = pch_uart_hal_get_modem(priv);
  987. if (modem & UART_MSR_DCD)
  988. ret |= TIOCM_CAR;
  989. if (modem & UART_MSR_RI)
  990. ret |= TIOCM_RNG;
  991. if (modem & UART_MSR_DSR)
  992. ret |= TIOCM_DSR;
  993. if (modem & UART_MSR_CTS)
  994. ret |= TIOCM_CTS;
  995. return ret;
  996. }
  997. static void pch_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
  998. {
  999. u32 mcr = 0;
  1000. struct eg20t_port *priv = container_of(port, struct eg20t_port, port);
  1001. if (mctrl & TIOCM_DTR)
  1002. mcr |= UART_MCR_DTR;
  1003. if (mctrl & TIOCM_RTS)
  1004. mcr |= UART_MCR_RTS;
  1005. if (mctrl & TIOCM_LOOP)
  1006. mcr |= UART_MCR_LOOP;
  1007. if (priv->mcr & UART_MCR_AFE)
  1008. mcr |= UART_MCR_AFE;
  1009. if (mctrl)
  1010. iowrite8(mcr, priv->membase + UART_MCR);
  1011. }
  1012. static void pch_uart_stop_tx(struct uart_port *port)
  1013. {
  1014. struct eg20t_port *priv;
  1015. priv = container_of(port, struct eg20t_port, port);
  1016. priv->start_tx = 0;
  1017. priv->tx_dma_use = 0;
  1018. }
  1019. static void pch_uart_start_tx(struct uart_port *port)
  1020. {
  1021. struct eg20t_port *priv;
  1022. priv = container_of(port, struct eg20t_port, port);
  1023. if (priv->use_dma) {
  1024. if (priv->tx_dma_use) {
  1025. dev_dbg(priv->port.dev, "%s : Tx DMA is NOT empty.\n",
  1026. __func__);
  1027. return;
  1028. }
  1029. }
  1030. priv->start_tx = 1;
  1031. pch_uart_hal_enable_interrupt(priv, PCH_UART_HAL_TX_INT);
  1032. }
  1033. static void pch_uart_stop_rx(struct uart_port *port)
  1034. {
  1035. struct eg20t_port *priv;
  1036. priv = container_of(port, struct eg20t_port, port);
  1037. priv->start_rx = 0;
  1038. pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_RX_INT |
  1039. PCH_UART_HAL_RX_ERR_INT);
  1040. }
  1041. /* Enable the modem status interrupts. */
  1042. static void pch_uart_enable_ms(struct uart_port *port)
  1043. {
  1044. struct eg20t_port *priv;
  1045. priv = container_of(port, struct eg20t_port, port);
  1046. pch_uart_hal_enable_interrupt(priv, PCH_UART_HAL_MS_INT);
  1047. }
  1048. /* Control the transmission of a break signal. */
  1049. static void pch_uart_break_ctl(struct uart_port *port, int ctl)
  1050. {
  1051. struct eg20t_port *priv;
  1052. unsigned long flags;
  1053. priv = container_of(port, struct eg20t_port, port);
  1054. spin_lock_irqsave(&priv->lock, flags);
  1055. pch_uart_hal_set_break(priv, ctl);
  1056. spin_unlock_irqrestore(&priv->lock, flags);
  1057. }
  1058. /* Grab any interrupt resources and initialise any low level driver state. */
  1059. static int pch_uart_startup(struct uart_port *port)
  1060. {
  1061. struct eg20t_port *priv;
  1062. int ret;
  1063. int fifo_size;
  1064. int trigger_level;
  1065. priv = container_of(port, struct eg20t_port, port);
  1066. priv->tx_empty = 1;
  1067. if (port->uartclk)
  1068. priv->uartclk = port->uartclk;
  1069. else
  1070. port->uartclk = priv->uartclk;
  1071. pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_ALL_INT);
  1072. ret = pch_uart_hal_set_line(priv, default_baud,
  1073. PCH_UART_HAL_PARITY_NONE, PCH_UART_HAL_8BIT,
  1074. PCH_UART_HAL_STB1);
  1075. if (ret)
  1076. return ret;
  1077. switch (priv->fifo_size) {
  1078. case 256:
  1079. fifo_size = PCH_UART_HAL_FIFO256;
  1080. break;
  1081. case 64:
  1082. fifo_size = PCH_UART_HAL_FIFO64;
  1083. break;
  1084. case 16:
  1085. fifo_size = PCH_UART_HAL_FIFO16;
  1086. break;
  1087. case 1:
  1088. default:
  1089. fifo_size = PCH_UART_HAL_FIFO_DIS;
  1090. break;
  1091. }
  1092. switch (priv->trigger) {
  1093. case PCH_UART_HAL_TRIGGER1:
  1094. trigger_level = 1;
  1095. break;
  1096. case PCH_UART_HAL_TRIGGER_L:
  1097. trigger_level = priv->fifo_size / 4;
  1098. break;
  1099. case PCH_UART_HAL_TRIGGER_M:
  1100. trigger_level = priv->fifo_size / 2;
  1101. break;
  1102. case PCH_UART_HAL_TRIGGER_H:
  1103. default:
  1104. trigger_level = priv->fifo_size - (priv->fifo_size / 8);
  1105. break;
  1106. }
  1107. priv->trigger_level = trigger_level;
  1108. ret = pch_uart_hal_set_fifo(priv, PCH_UART_HAL_DMA_MODE0,
  1109. fifo_size, priv->trigger);
  1110. if (ret < 0)
  1111. return ret;
  1112. ret = request_irq(priv->port.irq, pch_uart_interrupt, IRQF_SHARED,
  1113. KBUILD_MODNAME, priv);
  1114. if (ret < 0)
  1115. return ret;
  1116. if (priv->use_dma)
  1117. pch_request_dma(port);
  1118. priv->start_rx = 1;
  1119. pch_uart_hal_enable_interrupt(priv, PCH_UART_HAL_RX_INT |
  1120. PCH_UART_HAL_RX_ERR_INT);
  1121. uart_update_timeout(port, CS8, default_baud);
  1122. return 0;
  1123. }
  1124. static void pch_uart_shutdown(struct uart_port *port)
  1125. {
  1126. struct eg20t_port *priv;
  1127. int ret;
  1128. priv = container_of(port, struct eg20t_port, port);
  1129. pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_ALL_INT);
  1130. pch_uart_hal_fifo_reset(priv, PCH_UART_HAL_CLR_ALL_FIFO);
  1131. ret = pch_uart_hal_set_fifo(priv, PCH_UART_HAL_DMA_MODE0,
  1132. PCH_UART_HAL_FIFO_DIS, PCH_UART_HAL_TRIGGER1);
  1133. if (ret)
  1134. dev_err(priv->port.dev,
  1135. "pch_uart_hal_set_fifo Failed(ret=%d)\n", ret);
  1136. pch_free_dma(port);
  1137. free_irq(priv->port.irq, priv);
  1138. }
  1139. /* Change the port parameters, including word length, parity, stop
  1140. *bits. Update read_status_mask and ignore_status_mask to indicate
  1141. *the types of events we are interested in receiving. */
  1142. static void pch_uart_set_termios(struct uart_port *port,
  1143. struct ktermios *termios, struct ktermios *old)
  1144. {
  1145. int baud;
  1146. int rtn;
  1147. unsigned int parity, bits, stb;
  1148. struct eg20t_port *priv;
  1149. unsigned long flags;
  1150. priv = container_of(port, struct eg20t_port, port);
  1151. switch (termios->c_cflag & CSIZE) {
  1152. case CS5:
  1153. bits = PCH_UART_HAL_5BIT;
  1154. break;
  1155. case CS6:
  1156. bits = PCH_UART_HAL_6BIT;
  1157. break;
  1158. case CS7:
  1159. bits = PCH_UART_HAL_7BIT;
  1160. break;
  1161. default: /* CS8 */
  1162. bits = PCH_UART_HAL_8BIT;
  1163. break;
  1164. }
  1165. if (termios->c_cflag & CSTOPB)
  1166. stb = PCH_UART_HAL_STB2;
  1167. else
  1168. stb = PCH_UART_HAL_STB1;
  1169. if (termios->c_cflag & PARENB) {
  1170. if (termios->c_cflag & PARODD)
  1171. parity = PCH_UART_HAL_PARITY_ODD;
  1172. else
  1173. parity = PCH_UART_HAL_PARITY_EVEN;
  1174. } else
  1175. parity = PCH_UART_HAL_PARITY_NONE;
  1176. /* Only UART0 has auto hardware flow function */
  1177. if ((termios->c_cflag & CRTSCTS) && (priv->fifo_size == 256))
  1178. priv->mcr |= UART_MCR_AFE;
  1179. else
  1180. priv->mcr &= ~UART_MCR_AFE;
  1181. termios->c_cflag &= ~CMSPAR; /* Mark/Space parity is not supported */
  1182. baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 16);
  1183. spin_lock_irqsave(&priv->lock, flags);
  1184. spin_lock(&port->lock);
  1185. uart_update_timeout(port, termios->c_cflag, baud);
  1186. rtn = pch_uart_hal_set_line(priv, baud, parity, bits, stb);
  1187. if (rtn)
  1188. goto out;
  1189. pch_uart_set_mctrl(&priv->port, priv->port.mctrl);
  1190. /* Don't rewrite B0 */
  1191. if (tty_termios_baud_rate(termios))
  1192. tty_termios_encode_baud_rate(termios, baud, baud);
  1193. out:
  1194. spin_unlock(&port->lock);
  1195. spin_unlock_irqrestore(&priv->lock, flags);
  1196. }
  1197. static const char *pch_uart_type(struct uart_port *port)
  1198. {
  1199. return KBUILD_MODNAME;
  1200. }
  1201. static void pch_uart_release_port(struct uart_port *port)
  1202. {
  1203. struct eg20t_port *priv;
  1204. priv = container_of(port, struct eg20t_port, port);
  1205. pci_iounmap(priv->pdev, priv->membase);
  1206. pci_release_regions(priv->pdev);
  1207. }
  1208. static int pch_uart_request_port(struct uart_port *port)
  1209. {
  1210. struct eg20t_port *priv;
  1211. int ret;
  1212. void __iomem *membase;
  1213. priv = container_of(port, struct eg20t_port, port);
  1214. ret = pci_request_regions(priv->pdev, KBUILD_MODNAME);
  1215. if (ret < 0)
  1216. return -EBUSY;
  1217. membase = pci_iomap(priv->pdev, 1, 0);
  1218. if (!membase) {
  1219. pci_release_regions(priv->pdev);
  1220. return -EBUSY;
  1221. }
  1222. priv->membase = port->membase = membase;
  1223. return 0;
  1224. }
  1225. static void pch_uart_config_port(struct uart_port *port, int type)
  1226. {
  1227. struct eg20t_port *priv;
  1228. priv = container_of(port, struct eg20t_port, port);
  1229. if (type & UART_CONFIG_TYPE) {
  1230. port->type = priv->port_type;
  1231. pch_uart_request_port(port);
  1232. }
  1233. }
  1234. static int pch_uart_verify_port(struct uart_port *port,
  1235. struct serial_struct *serinfo)
  1236. {
  1237. struct eg20t_port *priv;
  1238. priv = container_of(port, struct eg20t_port, port);
  1239. if (serinfo->flags & UPF_LOW_LATENCY) {
  1240. dev_info(priv->port.dev,
  1241. "PCH UART : Use PIO Mode (without DMA)\n");
  1242. priv->use_dma = 0;
  1243. serinfo->flags &= ~UPF_LOW_LATENCY;
  1244. } else {
  1245. #ifndef CONFIG_PCH_DMA
  1246. dev_err(priv->port.dev, "%s : PCH DMA is not Loaded.\n",
  1247. __func__);
  1248. return -EOPNOTSUPP;
  1249. #endif
  1250. dev_info(priv->port.dev, "PCH UART : Use DMA Mode\n");
  1251. if (!priv->use_dma)
  1252. pch_request_dma(port);
  1253. priv->use_dma = 1;
  1254. }
  1255. return 0;
  1256. }
  1257. static struct uart_ops pch_uart_ops = {
  1258. .tx_empty = pch_uart_tx_empty,
  1259. .set_mctrl = pch_uart_set_mctrl,
  1260. .get_mctrl = pch_uart_get_mctrl,
  1261. .stop_tx = pch_uart_stop_tx,
  1262. .start_tx = pch_uart_start_tx,
  1263. .stop_rx = pch_uart_stop_rx,
  1264. .enable_ms = pch_uart_enable_ms,
  1265. .break_ctl = pch_uart_break_ctl,
  1266. .startup = pch_uart_startup,
  1267. .shutdown = pch_uart_shutdown,
  1268. .set_termios = pch_uart_set_termios,
  1269. /* .pm = pch_uart_pm, Not supported yet */
  1270. /* .set_wake = pch_uart_set_wake, Not supported yet */
  1271. .type = pch_uart_type,
  1272. .release_port = pch_uart_release_port,
  1273. .request_port = pch_uart_request_port,
  1274. .config_port = pch_uart_config_port,
  1275. .verify_port = pch_uart_verify_port
  1276. };
  1277. #ifdef CONFIG_SERIAL_PCH_UART_CONSOLE
  1278. /*
  1279. * Wait for transmitter & holding register to empty
  1280. */
  1281. static void wait_for_xmitr(struct eg20t_port *up, int bits)
  1282. {
  1283. unsigned int status, tmout = 10000;
  1284. /* Wait up to 10ms for the character(s) to be sent. */
  1285. for (;;) {
  1286. status = ioread8(up->membase + UART_LSR);
  1287. if ((status & bits) == bits)
  1288. break;
  1289. if (--tmout == 0)
  1290. break;
  1291. udelay(1);
  1292. }
  1293. /* Wait up to 1s for flow control if necessary */
  1294. if (up->port.flags & UPF_CONS_FLOW) {
  1295. unsigned int tmout;
  1296. for (tmout = 1000000; tmout; tmout--) {
  1297. unsigned int msr = ioread8(up->membase + UART_MSR);
  1298. if (msr & UART_MSR_CTS)
  1299. break;
  1300. udelay(1);
  1301. touch_nmi_watchdog();
  1302. }
  1303. }
  1304. }
  1305. static void pch_console_putchar(struct uart_port *port, int ch)
  1306. {
  1307. struct eg20t_port *priv =
  1308. container_of(port, struct eg20t_port, port);
  1309. wait_for_xmitr(priv, UART_LSR_THRE);
  1310. iowrite8(ch, priv->membase + PCH_UART_THR);
  1311. }
  1312. /*
  1313. * Print a string to the serial port trying not to disturb
  1314. * any possible real use of the port...
  1315. *
  1316. * The console_lock must be held when we get here.
  1317. */
  1318. static void
  1319. pch_console_write(struct console *co, const char *s, unsigned int count)
  1320. {
  1321. struct eg20t_port *priv;
  1322. unsigned long flags;
  1323. int priv_locked = 1;
  1324. int port_locked = 1;
  1325. u8 ier;
  1326. priv = pch_uart_ports[co->index];
  1327. touch_nmi_watchdog();
  1328. local_irq_save(flags);
  1329. if (priv->port.sysrq) {
  1330. spin_lock(&priv->lock);
  1331. /* serial8250_handle_port() already took the port lock */
  1332. port_locked = 0;
  1333. } else if (oops_in_progress) {
  1334. priv_locked = spin_trylock(&priv->lock);
  1335. port_locked = spin_trylock(&priv->port.lock);
  1336. } else {
  1337. spin_lock(&priv->lock);
  1338. spin_lock(&priv->port.lock);
  1339. }
  1340. /*
  1341. * First save the IER then disable the interrupts
  1342. */
  1343. ier = ioread8(priv->membase + UART_IER);
  1344. pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_ALL_INT);
  1345. uart_console_write(&priv->port, s, count, pch_console_putchar);
  1346. /*
  1347. * Finally, wait for transmitter to become empty
  1348. * and restore the IER
  1349. */
  1350. wait_for_xmitr(priv, BOTH_EMPTY);
  1351. iowrite8(ier, priv->membase + UART_IER);
  1352. if (port_locked)
  1353. spin_unlock(&priv->port.lock);
  1354. if (priv_locked)
  1355. spin_unlock(&priv->lock);
  1356. local_irq_restore(flags);
  1357. }
  1358. static int __init pch_console_setup(struct console *co, char *options)
  1359. {
  1360. struct uart_port *port;
  1361. int baud = default_baud;
  1362. int bits = 8;
  1363. int parity = 'n';
  1364. int flow = 'n';
  1365. /*
  1366. * Check whether an invalid uart number has been specified, and
  1367. * if so, search for the first available port that does have
  1368. * console support.
  1369. */
  1370. if (co->index >= PCH_UART_NR)
  1371. co->index = 0;
  1372. port = &pch_uart_ports[co->index]->port;
  1373. if (!port || (!port->iobase && !port->membase))
  1374. return -ENODEV;
  1375. port->uartclk = pch_uart_get_uartclk();
  1376. if (options)
  1377. uart_parse_options(options, &baud, &parity, &bits, &flow);
  1378. return uart_set_options(port, co, baud, parity, bits, flow);
  1379. }
  1380. static struct uart_driver pch_uart_driver;
  1381. static struct console pch_console = {
  1382. .name = PCH_UART_DRIVER_DEVICE,
  1383. .write = pch_console_write,
  1384. .device = uart_console_device,
  1385. .setup = pch_console_setup,
  1386. .flags = CON_PRINTBUFFER | CON_ANYTIME,
  1387. .index = -1,
  1388. .data = &pch_uart_driver,
  1389. };
  1390. #define PCH_CONSOLE (&pch_console)
  1391. #else
  1392. #define PCH_CONSOLE NULL
  1393. #endif
  1394. static struct uart_driver pch_uart_driver = {
  1395. .owner = THIS_MODULE,
  1396. .driver_name = KBUILD_MODNAME,
  1397. .dev_name = PCH_UART_DRIVER_DEVICE,
  1398. .major = 0,
  1399. .minor = 0,
  1400. .nr = PCH_UART_NR,
  1401. .cons = PCH_CONSOLE,
  1402. };
  1403. static struct eg20t_port *pch_uart_init_port(struct pci_dev *pdev,
  1404. const struct pci_device_id *id)
  1405. {
  1406. struct eg20t_port *priv;
  1407. int ret;
  1408. unsigned int iobase;
  1409. unsigned int mapbase;
  1410. unsigned char *rxbuf;
  1411. int fifosize;
  1412. int port_type;
  1413. struct pch_uart_driver_data *board;
  1414. char name[32]; /* for debugfs file name */
  1415. board = &drv_dat[id->driver_data];
  1416. port_type = board->port_type;
  1417. priv = kzalloc(sizeof(struct eg20t_port), GFP_KERNEL);
  1418. if (priv == NULL)
  1419. goto init_port_alloc_err;
  1420. rxbuf = (unsigned char *)__get_free_page(GFP_KERNEL);
  1421. if (!rxbuf)
  1422. goto init_port_free_txbuf;
  1423. switch (port_type) {
  1424. case PORT_UNKNOWN:
  1425. fifosize = 256; /* EG20T/ML7213: UART0 */
  1426. break;
  1427. case PORT_8250:
  1428. fifosize = 64; /* EG20T:UART1~3 ML7213: UART1~2*/
  1429. break;
  1430. default:
  1431. dev_err(&pdev->dev, "Invalid Port Type(=%d)\n", port_type);
  1432. goto init_port_hal_free;
  1433. }
  1434. pci_enable_msi(pdev);
  1435. pci_set_master(pdev);
  1436. spin_lock_init(&priv->lock);
  1437. iobase = pci_resource_start(pdev, 0);
  1438. mapbase = pci_resource_start(pdev, 1);
  1439. priv->mapbase = mapbase;
  1440. priv->iobase = iobase;
  1441. priv->pdev = pdev;
  1442. priv->tx_empty = 1;
  1443. priv->rxbuf.buf = rxbuf;
  1444. priv->rxbuf.size = PAGE_SIZE;
  1445. priv->fifo_size = fifosize;
  1446. priv->uartclk = pch_uart_get_uartclk();
  1447. priv->port_type = PORT_MAX_8250 + port_type + 1;
  1448. priv->port.dev = &pdev->dev;
  1449. priv->port.iobase = iobase;
  1450. priv->port.membase = NULL;
  1451. priv->port.mapbase = mapbase;
  1452. priv->port.irq = pdev->irq;
  1453. priv->port.iotype = UPIO_PORT;
  1454. priv->port.ops = &pch_uart_ops;
  1455. priv->port.flags = UPF_BOOT_AUTOCONF;
  1456. priv->port.fifosize = fifosize;
  1457. priv->port.line = board->line_no;
  1458. priv->trigger = PCH_UART_HAL_TRIGGER_M;
  1459. spin_lock_init(&priv->port.lock);
  1460. pci_set_drvdata(pdev, priv);
  1461. priv->trigger_level = 1;
  1462. priv->fcr = 0;
  1463. #ifdef CONFIG_SERIAL_PCH_UART_CONSOLE
  1464. pch_uart_ports[board->line_no] = priv;
  1465. #endif
  1466. ret = uart_add_one_port(&pch_uart_driver, &priv->port);
  1467. if (ret < 0)
  1468. goto init_port_hal_free;
  1469. #ifdef CONFIG_DEBUG_FS
  1470. snprintf(name, sizeof(name), "uart%d_regs", board->line_no);
  1471. priv->debugfs = debugfs_create_file(name, S_IFREG | S_IRUGO,
  1472. NULL, priv, &port_regs_ops);
  1473. #endif
  1474. return priv;
  1475. init_port_hal_free:
  1476. #ifdef CONFIG_SERIAL_PCH_UART_CONSOLE
  1477. pch_uart_ports[board->line_no] = NULL;
  1478. #endif
  1479. free_page((unsigned long)rxbuf);
  1480. init_port_free_txbuf:
  1481. kfree(priv);
  1482. init_port_alloc_err:
  1483. return NULL;
  1484. }
  1485. static void pch_uart_exit_port(struct eg20t_port *priv)
  1486. {
  1487. #ifdef CONFIG_DEBUG_FS
  1488. if (priv->debugfs)
  1489. debugfs_remove(priv->debugfs);
  1490. #endif
  1491. uart_remove_one_port(&pch_uart_driver, &priv->port);
  1492. pci_set_drvdata(priv->pdev, NULL);
  1493. free_page((unsigned long)priv->rxbuf.buf);
  1494. }
  1495. static void pch_uart_pci_remove(struct pci_dev *pdev)
  1496. {
  1497. struct eg20t_port *priv = pci_get_drvdata(pdev);
  1498. pci_disable_msi(pdev);
  1499. #ifdef CONFIG_SERIAL_PCH_UART_CONSOLE
  1500. pch_uart_ports[priv->port.line] = NULL;
  1501. #endif
  1502. pch_uart_exit_port(priv);
  1503. pci_disable_device(pdev);
  1504. kfree(priv);
  1505. return;
  1506. }
  1507. #ifdef CONFIG_PM
  1508. static int pch_uart_pci_suspend(struct pci_dev *pdev, pm_message_t state)
  1509. {
  1510. struct eg20t_port *priv = pci_get_drvdata(pdev);
  1511. uart_suspend_port(&pch_uart_driver, &priv->port);
  1512. pci_save_state(pdev);
  1513. pci_set_power_state(pdev, pci_choose_state(pdev, state));
  1514. return 0;
  1515. }
  1516. static int pch_uart_pci_resume(struct pci_dev *pdev)
  1517. {
  1518. struct eg20t_port *priv = pci_get_drvdata(pdev);
  1519. int ret;
  1520. pci_set_power_state(pdev, PCI_D0);
  1521. pci_restore_state(pdev);
  1522. ret = pci_enable_device(pdev);
  1523. if (ret) {
  1524. dev_err(&pdev->dev,
  1525. "%s-pci_enable_device failed(ret=%d) ", __func__, ret);
  1526. return ret;
  1527. }
  1528. uart_resume_port(&pch_uart_driver, &priv->port);
  1529. return 0;
  1530. }
  1531. #else
  1532. #define pch_uart_pci_suspend NULL
  1533. #define pch_uart_pci_resume NULL
  1534. #endif
  1535. static DEFINE_PCI_DEVICE_TABLE(pch_uart_pci_id) = {
  1536. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x8811),
  1537. .driver_data = pch_et20t_uart0},
  1538. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x8812),
  1539. .driver_data = pch_et20t_uart1},
  1540. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x8813),
  1541. .driver_data = pch_et20t_uart2},
  1542. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x8814),
  1543. .driver_data = pch_et20t_uart3},
  1544. {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x8027),
  1545. .driver_data = pch_ml7213_uart0},
  1546. {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x8028),
  1547. .driver_data = pch_ml7213_uart1},
  1548. {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x8029),
  1549. .driver_data = pch_ml7213_uart2},
  1550. {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x800C),
  1551. .driver_data = pch_ml7223_uart0},
  1552. {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x800D),
  1553. .driver_data = pch_ml7223_uart1},
  1554. {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x8811),
  1555. .driver_data = pch_ml7831_uart0},
  1556. {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x8812),
  1557. .driver_data = pch_ml7831_uart1},
  1558. {0,},
  1559. };
  1560. static int pch_uart_pci_probe(struct pci_dev *pdev,
  1561. const struct pci_device_id *id)
  1562. {
  1563. int ret;
  1564. struct eg20t_port *priv;
  1565. ret = pci_enable_device(pdev);
  1566. if (ret < 0)
  1567. goto probe_error;
  1568. priv = pch_uart_init_port(pdev, id);
  1569. if (!priv) {
  1570. ret = -EBUSY;
  1571. goto probe_disable_device;
  1572. }
  1573. pci_set_drvdata(pdev, priv);
  1574. return ret;
  1575. probe_disable_device:
  1576. pci_disable_msi(pdev);
  1577. pci_disable_device(pdev);
  1578. probe_error:
  1579. return ret;
  1580. }
  1581. static struct pci_driver pch_uart_pci_driver = {
  1582. .name = "pch_uart",
  1583. .id_table = pch_uart_pci_id,
  1584. .probe = pch_uart_pci_probe,
  1585. .remove = pch_uart_pci_remove,
  1586. .suspend = pch_uart_pci_suspend,
  1587. .resume = pch_uart_pci_resume,
  1588. };
  1589. static int __init pch_uart_module_init(void)
  1590. {
  1591. int ret;
  1592. /* register as UART driver */
  1593. ret = uart_register_driver(&pch_uart_driver);
  1594. if (ret < 0)
  1595. return ret;
  1596. /* register as PCI driver */
  1597. ret = pci_register_driver(&pch_uart_pci_driver);
  1598. if (ret < 0)
  1599. uart_unregister_driver(&pch_uart_driver);
  1600. return ret;
  1601. }
  1602. module_init(pch_uart_module_init);
  1603. static void __exit pch_uart_module_exit(void)
  1604. {
  1605. pci_unregister_driver(&pch_uart_pci_driver);
  1606. uart_unregister_driver(&pch_uart_driver);
  1607. }
  1608. module_exit(pch_uart_module_exit);
  1609. MODULE_LICENSE("GPL v2");
  1610. MODULE_DESCRIPTION("Intel EG20T PCH UART PCI Driver");
  1611. module_param(default_baud, uint, S_IRUGO);
  1612. MODULE_PARM_DESC(default_baud,
  1613. "Default BAUD for initial driver state and console (default 9600)");
  1614. module_param(user_uartclk, uint, S_IRUGO);
  1615. MODULE_PARM_DESC(user_uartclk,
  1616. "Override UART default or board specific UART clock");