pch_uart.c 40 KB

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