pch_uart.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562
  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 mcr;
  195. unsigned int use_dma;
  196. unsigned int use_dma_flag;
  197. struct dma_async_tx_descriptor *desc_tx;
  198. struct dma_async_tx_descriptor *desc_rx;
  199. struct pch_dma_slave param_tx;
  200. struct pch_dma_slave param_rx;
  201. struct dma_chan *chan_tx;
  202. struct dma_chan *chan_rx;
  203. struct scatterlist *sg_tx_p;
  204. int nent;
  205. struct scatterlist sg_rx;
  206. int tx_dma_use;
  207. void *rx_buf_virt;
  208. dma_addr_t rx_buf_dma;
  209. };
  210. static unsigned int default_baud = 9600;
  211. static const int trigger_level_256[4] = { 1, 64, 128, 224 };
  212. static const int trigger_level_64[4] = { 1, 16, 32, 56 };
  213. static const int trigger_level_16[4] = { 1, 4, 8, 14 };
  214. static const int trigger_level_1[4] = { 1, 1, 1, 1 };
  215. static void pch_uart_hal_request(struct pci_dev *pdev, int fifosize,
  216. int base_baud)
  217. {
  218. struct eg20t_port *priv = pci_get_drvdata(pdev);
  219. priv->trigger_level = 1;
  220. priv->fcr = 0;
  221. }
  222. static unsigned int get_msr(struct eg20t_port *priv, void __iomem *base)
  223. {
  224. unsigned int msr = ioread8(base + UART_MSR);
  225. priv->dmsr |= msr & PCH_UART_MSR_DELTA;
  226. return msr;
  227. }
  228. static void pch_uart_hal_enable_interrupt(struct eg20t_port *priv,
  229. unsigned int flag)
  230. {
  231. u8 ier = ioread8(priv->membase + UART_IER);
  232. ier |= flag & PCH_UART_IER_MASK;
  233. iowrite8(ier, priv->membase + UART_IER);
  234. }
  235. static void pch_uart_hal_disable_interrupt(struct eg20t_port *priv,
  236. unsigned int flag)
  237. {
  238. u8 ier = ioread8(priv->membase + UART_IER);
  239. ier &= ~(flag & PCH_UART_IER_MASK);
  240. iowrite8(ier, priv->membase + UART_IER);
  241. }
  242. static int pch_uart_hal_set_line(struct eg20t_port *priv, int baud,
  243. unsigned int parity, unsigned int bits,
  244. unsigned int stb)
  245. {
  246. unsigned int dll, dlm, lcr;
  247. int div;
  248. div = DIV_ROUND(priv->base_baud / 16, baud);
  249. if (div < 0 || USHRT_MAX <= div) {
  250. dev_err(priv->port.dev, "Invalid Baud(div=0x%x)\n", div);
  251. return -EINVAL;
  252. }
  253. dll = (unsigned int)div & 0x00FFU;
  254. dlm = ((unsigned int)div >> 8) & 0x00FFU;
  255. if (parity & ~(PCH_UART_LCR_PEN | PCH_UART_LCR_EPS | PCH_UART_LCR_SP)) {
  256. dev_err(priv->port.dev, "Invalid parity(0x%x)\n", parity);
  257. return -EINVAL;
  258. }
  259. if (bits & ~PCH_UART_LCR_WLS) {
  260. dev_err(priv->port.dev, "Invalid bits(0x%x)\n", bits);
  261. return -EINVAL;
  262. }
  263. if (stb & ~PCH_UART_LCR_STB) {
  264. dev_err(priv->port.dev, "Invalid STB(0x%x)\n", stb);
  265. return -EINVAL;
  266. }
  267. lcr = parity;
  268. lcr |= bits;
  269. lcr |= stb;
  270. dev_dbg(priv->port.dev, "%s:baud = %d, div = %04x, lcr = %02x (%lu)\n",
  271. __func__, baud, div, lcr, jiffies);
  272. iowrite8(PCH_UART_LCR_DLAB, priv->membase + UART_LCR);
  273. iowrite8(dll, priv->membase + PCH_UART_DLL);
  274. iowrite8(dlm, priv->membase + PCH_UART_DLM);
  275. iowrite8(lcr, priv->membase + UART_LCR);
  276. return 0;
  277. }
  278. static int pch_uart_hal_fifo_reset(struct eg20t_port *priv,
  279. unsigned int flag)
  280. {
  281. if (flag & ~(PCH_UART_FCR_TFR | PCH_UART_FCR_RFR)) {
  282. dev_err(priv->port.dev, "%s:Invalid flag(0x%x)\n",
  283. __func__, flag);
  284. return -EINVAL;
  285. }
  286. iowrite8(PCH_UART_FCR_FIFOE | priv->fcr, priv->membase + UART_FCR);
  287. iowrite8(PCH_UART_FCR_FIFOE | priv->fcr | flag,
  288. priv->membase + UART_FCR);
  289. iowrite8(priv->fcr, priv->membase + UART_FCR);
  290. return 0;
  291. }
  292. static int pch_uart_hal_set_fifo(struct eg20t_port *priv,
  293. unsigned int dmamode,
  294. unsigned int fifo_size, unsigned int trigger)
  295. {
  296. u8 fcr;
  297. if (dmamode & ~PCH_UART_FCR_DMS) {
  298. dev_err(priv->port.dev, "%s:Invalid DMA Mode(0x%x)\n",
  299. __func__, dmamode);
  300. return -EINVAL;
  301. }
  302. if (fifo_size & ~(PCH_UART_FCR_FIFOE | PCH_UART_FCR_FIFO256)) {
  303. dev_err(priv->port.dev, "%s:Invalid FIFO SIZE(0x%x)\n",
  304. __func__, fifo_size);
  305. return -EINVAL;
  306. }
  307. if (trigger & ~PCH_UART_FCR_RFTL) {
  308. dev_err(priv->port.dev, "%s:Invalid TRIGGER(0x%x)\n",
  309. __func__, trigger);
  310. return -EINVAL;
  311. }
  312. switch (priv->fifo_size) {
  313. case 256:
  314. priv->trigger_level =
  315. trigger_level_256[trigger >> PCH_UART_FCR_RFTL_SHIFT];
  316. break;
  317. case 64:
  318. priv->trigger_level =
  319. trigger_level_64[trigger >> PCH_UART_FCR_RFTL_SHIFT];
  320. break;
  321. case 16:
  322. priv->trigger_level =
  323. trigger_level_16[trigger >> PCH_UART_FCR_RFTL_SHIFT];
  324. break;
  325. default:
  326. priv->trigger_level =
  327. trigger_level_1[trigger >> PCH_UART_FCR_RFTL_SHIFT];
  328. break;
  329. }
  330. fcr =
  331. dmamode | fifo_size | trigger | PCH_UART_FCR_RFR | PCH_UART_FCR_TFR;
  332. iowrite8(PCH_UART_FCR_FIFOE, priv->membase + UART_FCR);
  333. iowrite8(PCH_UART_FCR_FIFOE | PCH_UART_FCR_RFR | PCH_UART_FCR_TFR,
  334. priv->membase + UART_FCR);
  335. iowrite8(fcr, priv->membase + UART_FCR);
  336. priv->fcr = fcr;
  337. return 0;
  338. }
  339. static u8 pch_uart_hal_get_modem(struct eg20t_port *priv)
  340. {
  341. priv->dmsr = 0;
  342. return get_msr(priv, priv->membase);
  343. }
  344. static void pch_uart_hal_write(struct eg20t_port *priv,
  345. const unsigned char *buf, int tx_size)
  346. {
  347. int i;
  348. unsigned int thr;
  349. for (i = 0; i < tx_size;) {
  350. thr = buf[i++];
  351. iowrite8(thr, priv->membase + PCH_UART_THR);
  352. }
  353. }
  354. static int pch_uart_hal_read(struct eg20t_port *priv, unsigned char *buf,
  355. int rx_size)
  356. {
  357. int i;
  358. u8 rbr, lsr;
  359. lsr = ioread8(priv->membase + UART_LSR);
  360. for (i = 0, lsr = ioread8(priv->membase + UART_LSR);
  361. i < rx_size && lsr & UART_LSR_DR;
  362. lsr = ioread8(priv->membase + UART_LSR)) {
  363. rbr = ioread8(priv->membase + PCH_UART_RBR);
  364. buf[i++] = rbr;
  365. }
  366. return i;
  367. }
  368. static unsigned int pch_uart_hal_get_iid(struct eg20t_port *priv)
  369. {
  370. unsigned int iir;
  371. int ret;
  372. iir = ioread8(priv->membase + UART_IIR);
  373. ret = (iir & (PCH_UART_IIR_IID | PCH_UART_IIR_TOI | PCH_UART_IIR_IP));
  374. return ret;
  375. }
  376. static u8 pch_uart_hal_get_line_status(struct eg20t_port *priv)
  377. {
  378. return ioread8(priv->membase + UART_LSR);
  379. }
  380. static void pch_uart_hal_set_break(struct eg20t_port *priv, int on)
  381. {
  382. unsigned int lcr;
  383. lcr = ioread8(priv->membase + UART_LCR);
  384. if (on)
  385. lcr |= PCH_UART_LCR_SB;
  386. else
  387. lcr &= ~PCH_UART_LCR_SB;
  388. iowrite8(lcr, priv->membase + UART_LCR);
  389. }
  390. static int push_rx(struct eg20t_port *priv, const unsigned char *buf,
  391. int size)
  392. {
  393. struct uart_port *port;
  394. struct tty_struct *tty;
  395. port = &priv->port;
  396. tty = tty_port_tty_get(&port->state->port);
  397. if (!tty) {
  398. dev_dbg(priv->port.dev, "%s:tty is busy now", __func__);
  399. return -EBUSY;
  400. }
  401. tty_insert_flip_string(tty, buf, size);
  402. tty_flip_buffer_push(tty);
  403. tty_kref_put(tty);
  404. return 0;
  405. }
  406. static int pop_tx_x(struct eg20t_port *priv, unsigned char *buf)
  407. {
  408. int ret;
  409. struct uart_port *port = &priv->port;
  410. if (port->x_char) {
  411. dev_dbg(priv->port.dev, "%s:X character send %02x (%lu)\n",
  412. __func__, port->x_char, jiffies);
  413. buf[0] = port->x_char;
  414. port->x_char = 0;
  415. ret = 1;
  416. } else {
  417. ret = 0;
  418. }
  419. return ret;
  420. }
  421. static int dma_push_rx(struct eg20t_port *priv, int size)
  422. {
  423. struct tty_struct *tty;
  424. int room;
  425. struct uart_port *port = &priv->port;
  426. port = &priv->port;
  427. tty = tty_port_tty_get(&port->state->port);
  428. if (!tty) {
  429. dev_dbg(priv->port.dev, "%s:tty is busy now", __func__);
  430. return 0;
  431. }
  432. room = tty_buffer_request_room(tty, size);
  433. if (room < size)
  434. dev_warn(port->dev, "Rx overrun: dropping %u bytes\n",
  435. size - room);
  436. if (!room)
  437. return room;
  438. tty_insert_flip_string(tty, sg_virt(&priv->sg_rx), size);
  439. port->icount.rx += room;
  440. tty_kref_put(tty);
  441. return room;
  442. }
  443. static void pch_free_dma(struct uart_port *port)
  444. {
  445. struct eg20t_port *priv;
  446. priv = container_of(port, struct eg20t_port, port);
  447. if (priv->chan_tx) {
  448. dma_release_channel(priv->chan_tx);
  449. priv->chan_tx = NULL;
  450. }
  451. if (priv->chan_rx) {
  452. dma_release_channel(priv->chan_rx);
  453. priv->chan_rx = NULL;
  454. }
  455. if (sg_dma_address(&priv->sg_rx))
  456. dma_free_coherent(port->dev, port->fifosize,
  457. sg_virt(&priv->sg_rx),
  458. sg_dma_address(&priv->sg_rx));
  459. return;
  460. }
  461. static bool filter(struct dma_chan *chan, void *slave)
  462. {
  463. struct pch_dma_slave *param = slave;
  464. if ((chan->chan_id == param->chan_id) && (param->dma_dev ==
  465. chan->device->dev)) {
  466. chan->private = param;
  467. return true;
  468. } else {
  469. return false;
  470. }
  471. }
  472. static void pch_request_dma(struct uart_port *port)
  473. {
  474. dma_cap_mask_t mask;
  475. struct dma_chan *chan;
  476. struct pci_dev *dma_dev;
  477. struct pch_dma_slave *param;
  478. struct eg20t_port *priv =
  479. container_of(port, struct eg20t_port, port);
  480. dma_cap_zero(mask);
  481. dma_cap_set(DMA_SLAVE, mask);
  482. dma_dev = pci_get_bus_and_slot(2, PCI_DEVFN(0xa, 0)); /* Get DMA's dev
  483. information */
  484. /* Set Tx DMA */
  485. param = &priv->param_tx;
  486. param->dma_dev = &dma_dev->dev;
  487. param->chan_id = priv->port.line;
  488. param->tx_reg = port->mapbase + UART_TX;
  489. chan = dma_request_channel(mask, filter, param);
  490. if (!chan) {
  491. dev_err(priv->port.dev, "%s:dma_request_channel FAILS(Tx)\n",
  492. __func__);
  493. return;
  494. }
  495. priv->chan_tx = chan;
  496. /* Set Rx DMA */
  497. param = &priv->param_rx;
  498. param->dma_dev = &dma_dev->dev;
  499. param->chan_id = priv->port.line + 1; /* Rx = Tx + 1 */
  500. param->rx_reg = port->mapbase + UART_RX;
  501. chan = dma_request_channel(mask, filter, param);
  502. if (!chan) {
  503. dev_err(priv->port.dev, "%s:dma_request_channel FAILS(Rx)\n",
  504. __func__);
  505. dma_release_channel(priv->chan_tx);
  506. return;
  507. }
  508. /* Get Consistent memory for DMA */
  509. priv->rx_buf_virt = dma_alloc_coherent(port->dev, port->fifosize,
  510. &priv->rx_buf_dma, GFP_KERNEL);
  511. priv->chan_rx = chan;
  512. }
  513. static void pch_dma_rx_complete(void *arg)
  514. {
  515. struct eg20t_port *priv = arg;
  516. struct uart_port *port = &priv->port;
  517. struct tty_struct *tty = tty_port_tty_get(&port->state->port);
  518. int count;
  519. if (!tty) {
  520. dev_dbg(priv->port.dev, "%s:tty is busy now", __func__);
  521. return;
  522. }
  523. dma_sync_sg_for_cpu(port->dev, &priv->sg_rx, 1, DMA_FROM_DEVICE);
  524. count = dma_push_rx(priv, priv->trigger_level);
  525. if (count)
  526. tty_flip_buffer_push(tty);
  527. tty_kref_put(tty);
  528. async_tx_ack(priv->desc_rx);
  529. pch_uart_hal_enable_interrupt(priv, PCH_UART_HAL_RX_INT);
  530. }
  531. static void pch_dma_tx_complete(void *arg)
  532. {
  533. struct eg20t_port *priv = arg;
  534. struct uart_port *port = &priv->port;
  535. struct circ_buf *xmit = &port->state->xmit;
  536. struct scatterlist *sg = priv->sg_tx_p;
  537. int i;
  538. for (i = 0; i < priv->nent; i++, sg++) {
  539. xmit->tail += sg_dma_len(sg);
  540. port->icount.tx += sg_dma_len(sg);
  541. }
  542. xmit->tail &= UART_XMIT_SIZE - 1;
  543. async_tx_ack(priv->desc_tx);
  544. dma_unmap_sg(port->dev, sg, priv->nent, DMA_TO_DEVICE);
  545. priv->tx_dma_use = 0;
  546. priv->nent = 0;
  547. kfree(priv->sg_tx_p);
  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. if (priv->tx_dma_use) {
  677. dev_dbg(priv->port.dev, "%s:Tx is not completed. (%lu)\n",
  678. __func__, jiffies);
  679. pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_TX_INT);
  680. priv->tx_empty = 1;
  681. return 0;
  682. }
  683. fifo_size = max(priv->fifo_size, 1);
  684. tx_empty = 1;
  685. if (pop_tx_x(priv, xmit->buf)) {
  686. pch_uart_hal_write(priv, xmit->buf, 1);
  687. port->icount.tx++;
  688. tx_empty = 0;
  689. fifo_size--;
  690. }
  691. bytes = min((int)CIRC_CNT(xmit->head, xmit->tail,
  692. UART_XMIT_SIZE), CIRC_CNT_TO_END(xmit->head,
  693. xmit->tail, UART_XMIT_SIZE));
  694. if (!bytes) {
  695. dev_dbg(priv->port.dev, "%s 0 bytes return\n", __func__);
  696. pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_TX_INT);
  697. uart_write_wakeup(port);
  698. return 0;
  699. }
  700. if (bytes > fifo_size) {
  701. num = bytes / fifo_size + 1;
  702. size = fifo_size;
  703. rem = bytes % fifo_size;
  704. } else {
  705. num = 1;
  706. size = bytes;
  707. rem = bytes;
  708. }
  709. dev_dbg(priv->port.dev, "%s num=%d size=%d rem=%d\n",
  710. __func__, num, size, rem);
  711. priv->tx_dma_use = 1;
  712. priv->sg_tx_p = kzalloc(sizeof(struct scatterlist)*num, GFP_ATOMIC);
  713. sg_init_table(priv->sg_tx_p, num); /* Initialize SG table */
  714. sg = priv->sg_tx_p;
  715. for (i = 0; i < num; i++, sg++) {
  716. if (i == (num - 1))
  717. sg_set_page(sg, virt_to_page(xmit->buf),
  718. rem, fifo_size * i);
  719. else
  720. sg_set_page(sg, virt_to_page(xmit->buf),
  721. size, fifo_size * i);
  722. }
  723. sg = priv->sg_tx_p;
  724. nent = dma_map_sg(port->dev, sg, num, DMA_TO_DEVICE);
  725. if (!nent) {
  726. dev_err(priv->port.dev, "%s:dma_map_sg Failed\n", __func__);
  727. return 0;
  728. }
  729. priv->nent = nent;
  730. for (i = 0; i < nent; i++, sg++) {
  731. sg->offset = (xmit->tail & (UART_XMIT_SIZE - 1)) +
  732. fifo_size * i;
  733. sg_dma_address(sg) = (sg_dma_address(sg) &
  734. ~(UART_XMIT_SIZE - 1)) + sg->offset;
  735. if (i == (nent - 1))
  736. sg_dma_len(sg) = rem;
  737. else
  738. sg_dma_len(sg) = size;
  739. }
  740. desc = priv->chan_tx->device->device_prep_slave_sg(priv->chan_tx,
  741. priv->sg_tx_p, nent, DMA_TO_DEVICE,
  742. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  743. if (!desc) {
  744. dev_err(priv->port.dev, "%s:device_prep_slave_sg Failed\n",
  745. __func__);
  746. return 0;
  747. }
  748. dma_sync_sg_for_device(port->dev, priv->sg_tx_p, nent, DMA_TO_DEVICE);
  749. priv->desc_tx = desc;
  750. desc->callback = pch_dma_tx_complete;
  751. desc->callback_param = priv;
  752. desc->tx_submit(desc);
  753. dma_async_issue_pending(priv->chan_tx);
  754. return PCH_UART_HANDLED_TX_INT;
  755. }
  756. static void pch_uart_err_ir(struct eg20t_port *priv, unsigned int lsr)
  757. {
  758. u8 fcr = ioread8(priv->membase + UART_FCR);
  759. /* Reset FIFO */
  760. fcr |= UART_FCR_CLEAR_RCVR;
  761. iowrite8(fcr, priv->membase + UART_FCR);
  762. if (lsr & PCH_UART_LSR_ERR)
  763. dev_err(&priv->pdev->dev, "Error data in FIFO\n");
  764. if (lsr & UART_LSR_FE)
  765. dev_err(&priv->pdev->dev, "Framing Error\n");
  766. if (lsr & UART_LSR_PE)
  767. dev_err(&priv->pdev->dev, "Parity Error\n");
  768. if (lsr & UART_LSR_OE)
  769. dev_err(&priv->pdev->dev, "Overrun Error\n");
  770. }
  771. static irqreturn_t pch_uart_interrupt(int irq, void *dev_id)
  772. {
  773. struct eg20t_port *priv = dev_id;
  774. unsigned int handled;
  775. u8 lsr;
  776. int ret = 0;
  777. unsigned int iid;
  778. unsigned long flags;
  779. spin_lock_irqsave(&priv->port.lock, flags);
  780. handled = 0;
  781. while ((iid = pch_uart_hal_get_iid(priv)) > 1) {
  782. switch (iid) {
  783. case PCH_UART_IID_RLS: /* Receiver Line Status */
  784. lsr = pch_uart_hal_get_line_status(priv);
  785. if (lsr & (PCH_UART_LSR_ERR | UART_LSR_FE |
  786. UART_LSR_PE | UART_LSR_OE)) {
  787. pch_uart_err_ir(priv, lsr);
  788. ret = PCH_UART_HANDLED_RX_ERR_INT;
  789. }
  790. break;
  791. case PCH_UART_IID_RDR: /* Received Data Ready */
  792. if (priv->use_dma) {
  793. pch_uart_hal_disable_interrupt(priv,
  794. PCH_UART_HAL_RX_INT);
  795. ret = dma_handle_rx(priv);
  796. if (!ret)
  797. pch_uart_hal_enable_interrupt(priv,
  798. PCH_UART_HAL_RX_INT);
  799. } else {
  800. ret = handle_rx(priv);
  801. }
  802. break;
  803. case PCH_UART_IID_RDR_TO: /* Received Data Ready
  804. (FIFO Timeout) */
  805. ret = handle_rx_to(priv);
  806. break;
  807. case PCH_UART_IID_THRE: /* Transmitter Holding Register
  808. Empty */
  809. if (priv->use_dma)
  810. ret = dma_handle_tx(priv);
  811. else
  812. ret = handle_tx(priv);
  813. break;
  814. case PCH_UART_IID_MS: /* Modem Status */
  815. ret = PCH_UART_HANDLED_MS_INT;
  816. break;
  817. default: /* Never junp to this label */
  818. dev_err(priv->port.dev, "%s:iid=%d (%lu)\n", __func__,
  819. iid, jiffies);
  820. ret = -1;
  821. break;
  822. }
  823. handled |= (unsigned int)ret;
  824. }
  825. if (handled == 0 && iid <= 1) {
  826. if (priv->int_dis_flag)
  827. priv->int_dis_flag = 0;
  828. }
  829. spin_unlock_irqrestore(&priv->port.lock, flags);
  830. return IRQ_RETVAL(handled);
  831. }
  832. /* This function tests whether the transmitter fifo and shifter for the port
  833. described by 'port' is empty. */
  834. static unsigned int pch_uart_tx_empty(struct uart_port *port)
  835. {
  836. struct eg20t_port *priv;
  837. int ret;
  838. priv = container_of(port, struct eg20t_port, port);
  839. if (priv->tx_empty)
  840. ret = TIOCSER_TEMT;
  841. else
  842. ret = 0;
  843. return ret;
  844. }
  845. /* Returns the current state of modem control inputs. */
  846. static unsigned int pch_uart_get_mctrl(struct uart_port *port)
  847. {
  848. struct eg20t_port *priv;
  849. u8 modem;
  850. unsigned int ret = 0;
  851. priv = container_of(port, struct eg20t_port, port);
  852. modem = pch_uart_hal_get_modem(priv);
  853. if (modem & UART_MSR_DCD)
  854. ret |= TIOCM_CAR;
  855. if (modem & UART_MSR_RI)
  856. ret |= TIOCM_RNG;
  857. if (modem & UART_MSR_DSR)
  858. ret |= TIOCM_DSR;
  859. if (modem & UART_MSR_CTS)
  860. ret |= TIOCM_CTS;
  861. return ret;
  862. }
  863. static void pch_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
  864. {
  865. u32 mcr = 0;
  866. struct eg20t_port *priv = container_of(port, struct eg20t_port, port);
  867. if (mctrl & TIOCM_DTR)
  868. mcr |= UART_MCR_DTR;
  869. if (mctrl & TIOCM_RTS)
  870. mcr |= UART_MCR_RTS;
  871. if (mctrl & TIOCM_LOOP)
  872. mcr |= UART_MCR_LOOP;
  873. if (priv->mcr & UART_MCR_AFE)
  874. mcr |= UART_MCR_AFE;
  875. if (mctrl)
  876. iowrite8(mcr, priv->membase + UART_MCR);
  877. }
  878. static void pch_uart_stop_tx(struct uart_port *port)
  879. {
  880. struct eg20t_port *priv;
  881. priv = container_of(port, struct eg20t_port, port);
  882. priv->start_tx = 0;
  883. priv->tx_dma_use = 0;
  884. }
  885. static void pch_uart_start_tx(struct uart_port *port)
  886. {
  887. struct eg20t_port *priv;
  888. priv = container_of(port, struct eg20t_port, port);
  889. if (priv->use_dma) {
  890. if (priv->tx_dma_use) {
  891. dev_dbg(priv->port.dev, "%s : Tx DMA is NOT empty.\n",
  892. __func__);
  893. return;
  894. }
  895. }
  896. priv->start_tx = 1;
  897. pch_uart_hal_enable_interrupt(priv, PCH_UART_HAL_TX_INT);
  898. }
  899. static void pch_uart_stop_rx(struct uart_port *port)
  900. {
  901. struct eg20t_port *priv;
  902. priv = container_of(port, struct eg20t_port, port);
  903. priv->start_rx = 0;
  904. pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_RX_INT);
  905. priv->int_dis_flag = 1;
  906. }
  907. /* Enable the modem status interrupts. */
  908. static void pch_uart_enable_ms(struct uart_port *port)
  909. {
  910. struct eg20t_port *priv;
  911. priv = container_of(port, struct eg20t_port, port);
  912. pch_uart_hal_enable_interrupt(priv, PCH_UART_HAL_MS_INT);
  913. }
  914. /* Control the transmission of a break signal. */
  915. static void pch_uart_break_ctl(struct uart_port *port, int ctl)
  916. {
  917. struct eg20t_port *priv;
  918. unsigned long flags;
  919. priv = container_of(port, struct eg20t_port, port);
  920. spin_lock_irqsave(&port->lock, flags);
  921. pch_uart_hal_set_break(priv, ctl);
  922. spin_unlock_irqrestore(&port->lock, flags);
  923. }
  924. /* Grab any interrupt resources and initialise any low level driver state. */
  925. static int pch_uart_startup(struct uart_port *port)
  926. {
  927. struct eg20t_port *priv;
  928. int ret;
  929. int fifo_size;
  930. int trigger_level;
  931. priv = container_of(port, struct eg20t_port, port);
  932. priv->tx_empty = 1;
  933. if (port->uartclk)
  934. priv->base_baud = port->uartclk;
  935. else
  936. port->uartclk = priv->base_baud;
  937. pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_ALL_INT);
  938. ret = pch_uart_hal_set_line(priv, default_baud,
  939. PCH_UART_HAL_PARITY_NONE, PCH_UART_HAL_8BIT,
  940. PCH_UART_HAL_STB1);
  941. if (ret)
  942. return ret;
  943. switch (priv->fifo_size) {
  944. case 256:
  945. fifo_size = PCH_UART_HAL_FIFO256;
  946. break;
  947. case 64:
  948. fifo_size = PCH_UART_HAL_FIFO64;
  949. break;
  950. case 16:
  951. fifo_size = PCH_UART_HAL_FIFO16;
  952. case 1:
  953. default:
  954. fifo_size = PCH_UART_HAL_FIFO_DIS;
  955. break;
  956. }
  957. switch (priv->trigger) {
  958. case PCH_UART_HAL_TRIGGER1:
  959. trigger_level = 1;
  960. break;
  961. case PCH_UART_HAL_TRIGGER_L:
  962. trigger_level = priv->fifo_size / 4;
  963. break;
  964. case PCH_UART_HAL_TRIGGER_M:
  965. trigger_level = priv->fifo_size / 2;
  966. break;
  967. case PCH_UART_HAL_TRIGGER_H:
  968. default:
  969. trigger_level = priv->fifo_size - (priv->fifo_size / 8);
  970. break;
  971. }
  972. priv->trigger_level = trigger_level;
  973. ret = pch_uart_hal_set_fifo(priv, PCH_UART_HAL_DMA_MODE0,
  974. fifo_size, priv->trigger);
  975. if (ret < 0)
  976. return ret;
  977. ret = request_irq(priv->port.irq, pch_uart_interrupt, IRQF_SHARED,
  978. KBUILD_MODNAME, priv);
  979. if (ret < 0)
  980. return ret;
  981. if (priv->use_dma)
  982. pch_request_dma(port);
  983. priv->start_rx = 1;
  984. pch_uart_hal_enable_interrupt(priv, PCH_UART_HAL_RX_INT);
  985. uart_update_timeout(port, CS8, default_baud);
  986. return 0;
  987. }
  988. static void pch_uart_shutdown(struct uart_port *port)
  989. {
  990. struct eg20t_port *priv;
  991. int ret;
  992. priv = container_of(port, struct eg20t_port, port);
  993. pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_ALL_INT);
  994. pch_uart_hal_fifo_reset(priv, PCH_UART_HAL_CLR_ALL_FIFO);
  995. ret = pch_uart_hal_set_fifo(priv, PCH_UART_HAL_DMA_MODE0,
  996. PCH_UART_HAL_FIFO_DIS, PCH_UART_HAL_TRIGGER1);
  997. if (ret)
  998. dev_err(priv->port.dev,
  999. "pch_uart_hal_set_fifo Failed(ret=%d)\n", ret);
  1000. if (priv->use_dma_flag)
  1001. pch_free_dma(port);
  1002. free_irq(priv->port.irq, priv);
  1003. }
  1004. /* Change the port parameters, including word length, parity, stop
  1005. *bits. Update read_status_mask and ignore_status_mask to indicate
  1006. *the types of events we are interested in receiving. */
  1007. static void pch_uart_set_termios(struct uart_port *port,
  1008. struct ktermios *termios, struct ktermios *old)
  1009. {
  1010. int baud;
  1011. int rtn;
  1012. unsigned int parity, bits, stb;
  1013. struct eg20t_port *priv;
  1014. unsigned long flags;
  1015. priv = container_of(port, struct eg20t_port, port);
  1016. switch (termios->c_cflag & CSIZE) {
  1017. case CS5:
  1018. bits = PCH_UART_HAL_5BIT;
  1019. break;
  1020. case CS6:
  1021. bits = PCH_UART_HAL_6BIT;
  1022. break;
  1023. case CS7:
  1024. bits = PCH_UART_HAL_7BIT;
  1025. break;
  1026. default: /* CS8 */
  1027. bits = PCH_UART_HAL_8BIT;
  1028. break;
  1029. }
  1030. if (termios->c_cflag & CSTOPB)
  1031. stb = PCH_UART_HAL_STB2;
  1032. else
  1033. stb = PCH_UART_HAL_STB1;
  1034. if (termios->c_cflag & PARENB) {
  1035. if (!(termios->c_cflag & PARODD))
  1036. parity = PCH_UART_HAL_PARITY_ODD;
  1037. else
  1038. parity = PCH_UART_HAL_PARITY_EVEN;
  1039. } else {
  1040. parity = PCH_UART_HAL_PARITY_NONE;
  1041. }
  1042. /* Only UART0 has auto hardware flow function */
  1043. if ((termios->c_cflag & CRTSCTS) && (priv->fifo_size == 256))
  1044. priv->mcr |= UART_MCR_AFE;
  1045. else
  1046. priv->mcr &= ~UART_MCR_AFE;
  1047. termios->c_cflag &= ~CMSPAR; /* Mark/Space parity is not supported */
  1048. baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 16);
  1049. spin_lock_irqsave(&port->lock, flags);
  1050. uart_update_timeout(port, termios->c_cflag, baud);
  1051. rtn = pch_uart_hal_set_line(priv, baud, parity, bits, stb);
  1052. if (rtn)
  1053. goto out;
  1054. /* Don't rewrite B0 */
  1055. if (tty_termios_baud_rate(termios))
  1056. tty_termios_encode_baud_rate(termios, baud, baud);
  1057. out:
  1058. spin_unlock_irqrestore(&port->lock, flags);
  1059. }
  1060. static const char *pch_uart_type(struct uart_port *port)
  1061. {
  1062. return KBUILD_MODNAME;
  1063. }
  1064. static void pch_uart_release_port(struct uart_port *port)
  1065. {
  1066. struct eg20t_port *priv;
  1067. priv = container_of(port, struct eg20t_port, port);
  1068. pci_iounmap(priv->pdev, priv->membase);
  1069. pci_release_regions(priv->pdev);
  1070. }
  1071. static int pch_uart_request_port(struct uart_port *port)
  1072. {
  1073. struct eg20t_port *priv;
  1074. int ret;
  1075. void __iomem *membase;
  1076. priv = container_of(port, struct eg20t_port, port);
  1077. ret = pci_request_regions(priv->pdev, KBUILD_MODNAME);
  1078. if (ret < 0)
  1079. return -EBUSY;
  1080. membase = pci_iomap(priv->pdev, 1, 0);
  1081. if (!membase) {
  1082. pci_release_regions(priv->pdev);
  1083. return -EBUSY;
  1084. }
  1085. priv->membase = port->membase = membase;
  1086. return 0;
  1087. }
  1088. static void pch_uart_config_port(struct uart_port *port, int type)
  1089. {
  1090. struct eg20t_port *priv;
  1091. priv = container_of(port, struct eg20t_port, port);
  1092. if (type & UART_CONFIG_TYPE) {
  1093. port->type = priv->port_type;
  1094. pch_uart_request_port(port);
  1095. }
  1096. }
  1097. static int pch_uart_verify_port(struct uart_port *port,
  1098. struct serial_struct *serinfo)
  1099. {
  1100. struct eg20t_port *priv;
  1101. priv = container_of(port, struct eg20t_port, port);
  1102. if (serinfo->flags & UPF_LOW_LATENCY) {
  1103. dev_info(priv->port.dev,
  1104. "PCH UART : Use PIO Mode (without DMA)\n");
  1105. priv->use_dma = 0;
  1106. serinfo->flags &= ~UPF_LOW_LATENCY;
  1107. } else {
  1108. #ifndef CONFIG_PCH_DMA
  1109. dev_err(priv->port.dev, "%s : PCH DMA is not Loaded.\n",
  1110. __func__);
  1111. return -EOPNOTSUPP;
  1112. #endif
  1113. priv->use_dma = 1;
  1114. priv->use_dma_flag = 1;
  1115. dev_info(priv->port.dev, "PCH UART : Use DMA Mode\n");
  1116. }
  1117. return 0;
  1118. }
  1119. static struct uart_ops pch_uart_ops = {
  1120. .tx_empty = pch_uart_tx_empty,
  1121. .set_mctrl = pch_uart_set_mctrl,
  1122. .get_mctrl = pch_uart_get_mctrl,
  1123. .stop_tx = pch_uart_stop_tx,
  1124. .start_tx = pch_uart_start_tx,
  1125. .stop_rx = pch_uart_stop_rx,
  1126. .enable_ms = pch_uart_enable_ms,
  1127. .break_ctl = pch_uart_break_ctl,
  1128. .startup = pch_uart_startup,
  1129. .shutdown = pch_uart_shutdown,
  1130. .set_termios = pch_uart_set_termios,
  1131. /* .pm = pch_uart_pm, Not supported yet */
  1132. /* .set_wake = pch_uart_set_wake, Not supported yet */
  1133. .type = pch_uart_type,
  1134. .release_port = pch_uart_release_port,
  1135. .request_port = pch_uart_request_port,
  1136. .config_port = pch_uart_config_port,
  1137. .verify_port = pch_uart_verify_port
  1138. };
  1139. static struct uart_driver pch_uart_driver = {
  1140. .owner = THIS_MODULE,
  1141. .driver_name = KBUILD_MODNAME,
  1142. .dev_name = PCH_UART_DRIVER_DEVICE,
  1143. .major = 0,
  1144. .minor = 0,
  1145. .nr = PCH_UART_NR,
  1146. };
  1147. static struct eg20t_port *pch_uart_init_port(struct pci_dev *pdev,
  1148. const struct pci_device_id *id)
  1149. {
  1150. struct eg20t_port *priv;
  1151. int ret;
  1152. unsigned int iobase;
  1153. unsigned int mapbase;
  1154. unsigned char *rxbuf;
  1155. int fifosize, base_baud;
  1156. static int num;
  1157. int port_type = id->driver_data;
  1158. priv = kzalloc(sizeof(struct eg20t_port), GFP_KERNEL);
  1159. if (priv == NULL)
  1160. goto init_port_alloc_err;
  1161. rxbuf = (unsigned char *)__get_free_page(GFP_KERNEL);
  1162. if (!rxbuf)
  1163. goto init_port_free_txbuf;
  1164. switch (port_type) {
  1165. case PORT_UNKNOWN:
  1166. fifosize = 256; /* EG20T/ML7213: UART0 */
  1167. base_baud = 1843200; /* 1.8432MHz */
  1168. break;
  1169. case PORT_8250:
  1170. fifosize = 64; /* EG20T:UART1~3 ML7213: UART1~2*/
  1171. base_baud = 1843200; /* 1.8432MHz */
  1172. break;
  1173. default:
  1174. dev_err(&pdev->dev, "Invalid Port Type(=%d)\n", port_type);
  1175. goto init_port_hal_free;
  1176. }
  1177. iobase = pci_resource_start(pdev, 0);
  1178. mapbase = pci_resource_start(pdev, 1);
  1179. priv->mapbase = mapbase;
  1180. priv->iobase = iobase;
  1181. priv->pdev = pdev;
  1182. priv->tx_empty = 1;
  1183. priv->rxbuf.buf = rxbuf;
  1184. priv->rxbuf.size = PAGE_SIZE;
  1185. priv->fifo_size = fifosize;
  1186. priv->base_baud = base_baud;
  1187. priv->port_type = PORT_MAX_8250 + port_type + 1;
  1188. priv->port.dev = &pdev->dev;
  1189. priv->port.iobase = iobase;
  1190. priv->port.membase = NULL;
  1191. priv->port.mapbase = mapbase;
  1192. priv->port.irq = pdev->irq;
  1193. priv->port.iotype = UPIO_PORT;
  1194. priv->port.ops = &pch_uart_ops;
  1195. priv->port.flags = UPF_BOOT_AUTOCONF;
  1196. priv->port.fifosize = fifosize;
  1197. priv->port.line = num++;
  1198. priv->trigger = PCH_UART_HAL_TRIGGER_M;
  1199. spin_lock_init(&priv->port.lock);
  1200. pci_set_drvdata(pdev, priv);
  1201. pch_uart_hal_request(pdev, fifosize, base_baud);
  1202. ret = uart_add_one_port(&pch_uart_driver, &priv->port);
  1203. if (ret < 0)
  1204. goto init_port_hal_free;
  1205. return priv;
  1206. init_port_hal_free:
  1207. free_page((unsigned long)rxbuf);
  1208. init_port_free_txbuf:
  1209. kfree(priv);
  1210. init_port_alloc_err:
  1211. return NULL;
  1212. }
  1213. static void pch_uart_exit_port(struct eg20t_port *priv)
  1214. {
  1215. uart_remove_one_port(&pch_uart_driver, &priv->port);
  1216. pci_set_drvdata(priv->pdev, NULL);
  1217. free_page((unsigned long)priv->rxbuf.buf);
  1218. }
  1219. static void pch_uart_pci_remove(struct pci_dev *pdev)
  1220. {
  1221. struct eg20t_port *priv;
  1222. priv = (struct eg20t_port *)pci_get_drvdata(pdev);
  1223. pch_uart_exit_port(priv);
  1224. pci_disable_device(pdev);
  1225. kfree(priv);
  1226. return;
  1227. }
  1228. #ifdef CONFIG_PM
  1229. static int pch_uart_pci_suspend(struct pci_dev *pdev, pm_message_t state)
  1230. {
  1231. struct eg20t_port *priv = pci_get_drvdata(pdev);
  1232. uart_suspend_port(&pch_uart_driver, &priv->port);
  1233. pci_save_state(pdev);
  1234. pci_set_power_state(pdev, pci_choose_state(pdev, state));
  1235. return 0;
  1236. }
  1237. static int pch_uart_pci_resume(struct pci_dev *pdev)
  1238. {
  1239. struct eg20t_port *priv = pci_get_drvdata(pdev);
  1240. int ret;
  1241. pci_set_power_state(pdev, PCI_D0);
  1242. pci_restore_state(pdev);
  1243. ret = pci_enable_device(pdev);
  1244. if (ret) {
  1245. dev_err(&pdev->dev,
  1246. "%s-pci_enable_device failed(ret=%d) ", __func__, ret);
  1247. return ret;
  1248. }
  1249. uart_resume_port(&pch_uart_driver, &priv->port);
  1250. return 0;
  1251. }
  1252. #else
  1253. #define pch_uart_pci_suspend NULL
  1254. #define pch_uart_pci_resume NULL
  1255. #endif
  1256. static DEFINE_PCI_DEVICE_TABLE(pch_uart_pci_id) = {
  1257. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x8811),
  1258. .driver_data = PCH_UART_8LINE},
  1259. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x8812),
  1260. .driver_data = PCH_UART_2LINE},
  1261. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x8813),
  1262. .driver_data = PCH_UART_2LINE},
  1263. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x8814),
  1264. .driver_data = PCH_UART_2LINE},
  1265. {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x8027),
  1266. .driver_data = PCH_UART_8LINE},
  1267. {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x8028),
  1268. .driver_data = PCH_UART_2LINE},
  1269. {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x8029),
  1270. .driver_data = PCH_UART_2LINE},
  1271. {0,},
  1272. };
  1273. static int __devinit pch_uart_pci_probe(struct pci_dev *pdev,
  1274. const struct pci_device_id *id)
  1275. {
  1276. int ret;
  1277. struct eg20t_port *priv;
  1278. ret = pci_enable_device(pdev);
  1279. if (ret < 0)
  1280. goto probe_error;
  1281. priv = pch_uart_init_port(pdev, id);
  1282. if (!priv) {
  1283. ret = -EBUSY;
  1284. goto probe_disable_device;
  1285. }
  1286. pci_set_drvdata(pdev, priv);
  1287. return ret;
  1288. probe_disable_device:
  1289. pci_disable_device(pdev);
  1290. probe_error:
  1291. return ret;
  1292. }
  1293. static struct pci_driver pch_uart_pci_driver = {
  1294. .name = "pch_uart",
  1295. .id_table = pch_uart_pci_id,
  1296. .probe = pch_uart_pci_probe,
  1297. .remove = __devexit_p(pch_uart_pci_remove),
  1298. .suspend = pch_uart_pci_suspend,
  1299. .resume = pch_uart_pci_resume,
  1300. };
  1301. static int __init pch_uart_module_init(void)
  1302. {
  1303. int ret;
  1304. /* register as UART driver */
  1305. ret = uart_register_driver(&pch_uart_driver);
  1306. if (ret < 0)
  1307. return ret;
  1308. /* register as PCI driver */
  1309. ret = pci_register_driver(&pch_uart_pci_driver);
  1310. if (ret < 0)
  1311. uart_unregister_driver(&pch_uart_driver);
  1312. return ret;
  1313. }
  1314. module_init(pch_uart_module_init);
  1315. static void __exit pch_uart_module_exit(void)
  1316. {
  1317. pci_unregister_driver(&pch_uart_pci_driver);
  1318. uart_unregister_driver(&pch_uart_driver);
  1319. }
  1320. module_exit(pch_uart_module_exit);
  1321. MODULE_LICENSE("GPL v2");
  1322. MODULE_DESCRIPTION("Intel EG20T PCH UART PCI Driver");
  1323. module_param(default_baud, uint, S_IRUGO);