pch_uart.c 38 KB

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