pch_uart.c 49 KB

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