pch_uart.c 40 KB

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