pch_uart.c 50 KB

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