pch_uart.c 50 KB

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