pch_can.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463
  1. /*
  2. * Copyright (C) 1999 - 2010 Intel Corporation.
  3. * Copyright (C) 2010 OKI SEMICONDUCTOR Co., LTD.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; version 2 of the License.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
  17. */
  18. #include <linux/interrupt.h>
  19. #include <linux/delay.h>
  20. #include <linux/io.h>
  21. #include <linux/module.h>
  22. #include <linux/sched.h>
  23. #include <linux/pci.h>
  24. #include <linux/init.h>
  25. #include <linux/kernel.h>
  26. #include <linux/types.h>
  27. #include <linux/errno.h>
  28. #include <linux/netdevice.h>
  29. #include <linux/skbuff.h>
  30. #include <linux/can.h>
  31. #include <linux/can/dev.h>
  32. #include <linux/can/error.h>
  33. #define MAX_MSG_OBJ 32
  34. #define MSG_OBJ_RX 0 /* The receive message object flag. */
  35. #define MSG_OBJ_TX 1 /* The transmit message object flag. */
  36. #define ENABLE 1 /* The enable flag */
  37. #define DISABLE 0 /* The disable flag */
  38. #define CAN_CTRL_INIT 0x0001 /* The INIT bit of CANCONT register. */
  39. #define CAN_CTRL_IE 0x0002 /* The IE bit of CAN control register */
  40. #define CAN_CTRL_IE_SIE_EIE 0x000e
  41. #define CAN_CTRL_CCE 0x0040
  42. #define CAN_CTRL_OPT 0x0080 /* The OPT bit of CANCONT register. */
  43. #define CAN_OPT_SILENT 0x0008 /* The Silent bit of CANOPT reg. */
  44. #define CAN_OPT_LBACK 0x0010 /* The LoopBack bit of CANOPT reg. */
  45. #define CAN_CMASK_RX_TX_SET 0x00f3
  46. #define CAN_CMASK_RX_TX_GET 0x0073
  47. #define CAN_CMASK_ALL 0xff
  48. #define CAN_CMASK_RDWR 0x80
  49. #define CAN_CMASK_ARB 0x20
  50. #define CAN_CMASK_CTRL 0x10
  51. #define CAN_CMASK_MASK 0x40
  52. #define CAN_CMASK_NEWDAT 0x04
  53. #define CAN_CMASK_CLRINTPND 0x08
  54. #define CAN_IF_MCONT_NEWDAT 0x8000
  55. #define CAN_IF_MCONT_INTPND 0x2000
  56. #define CAN_IF_MCONT_UMASK 0x1000
  57. #define CAN_IF_MCONT_TXIE 0x0800
  58. #define CAN_IF_MCONT_RXIE 0x0400
  59. #define CAN_IF_MCONT_RMTEN 0x0200
  60. #define CAN_IF_MCONT_TXRQXT 0x0100
  61. #define CAN_IF_MCONT_EOB 0x0080
  62. #define CAN_IF_MCONT_DLC 0x000f
  63. #define CAN_IF_MCONT_MSGLOST 0x4000
  64. #define CAN_MASK2_MDIR_MXTD 0xc000
  65. #define CAN_ID2_DIR 0x2000
  66. #define CAN_ID_MSGVAL 0x8000
  67. #define CAN_STATUS_INT 0x8000
  68. #define CAN_IF_CREQ_BUSY 0x8000
  69. #define CAN_ID2_XTD 0x4000
  70. #define CAN_REC 0x00007f00
  71. #define CAN_TEC 0x000000ff
  72. #define PCH_RX_OK 0x00000010
  73. #define PCH_TX_OK 0x00000008
  74. #define PCH_BUS_OFF 0x00000080
  75. #define PCH_EWARN 0x00000040
  76. #define PCH_EPASSIV 0x00000020
  77. #define PCH_LEC0 0x00000001
  78. #define PCH_LEC1 0x00000002
  79. #define PCH_LEC2 0x00000004
  80. #define PCH_LEC_ALL (PCH_LEC0 | PCH_LEC1 | PCH_LEC2)
  81. #define PCH_STUF_ERR PCH_LEC0
  82. #define PCH_FORM_ERR PCH_LEC1
  83. #define PCH_ACK_ERR (PCH_LEC0 | PCH_LEC1)
  84. #define PCH_BIT1_ERR PCH_LEC2
  85. #define PCH_BIT0_ERR (PCH_LEC0 | PCH_LEC2)
  86. #define PCH_CRC_ERR (PCH_LEC1 | PCH_LEC2)
  87. /* bit position of certain controller bits. */
  88. #define BIT_BITT_BRP 0
  89. #define BIT_BITT_SJW 6
  90. #define BIT_BITT_TSEG1 8
  91. #define BIT_BITT_TSEG2 12
  92. #define BIT_IF1_MCONT_RXIE 10
  93. #define BIT_IF2_MCONT_TXIE 11
  94. #define BIT_BRPE_BRPE 6
  95. #define BIT_ES_TXERRCNT 0
  96. #define BIT_ES_RXERRCNT 8
  97. #define MSK_BITT_BRP 0x3f
  98. #define MSK_BITT_SJW 0xc0
  99. #define MSK_BITT_TSEG1 0xf00
  100. #define MSK_BITT_TSEG2 0x7000
  101. #define MSK_BRPE_BRPE 0x3c0
  102. #define MSK_BRPE_GET 0x0f
  103. #define MSK_CTRL_IE_SIE_EIE 0x07
  104. #define MSK_MCONT_TXIE 0x08
  105. #define MSK_MCONT_RXIE 0x10
  106. #define PCH_CAN_NO_TX_BUFF 1
  107. #define COUNTER_LIMIT 10
  108. #define PCH_CAN_CLK 50000000 /* 50MHz */
  109. /* Define the number of message object.
  110. * PCH CAN communications are done via Message RAM.
  111. * The Message RAM consists of 32 message objects. */
  112. #define PCH_RX_OBJ_NUM 26 /* 1~ PCH_RX_OBJ_NUM is Rx*/
  113. #define PCH_TX_OBJ_NUM 6 /* PCH_RX_OBJ_NUM is RX ~ Tx*/
  114. #define PCH_OBJ_NUM (PCH_TX_OBJ_NUM + PCH_RX_OBJ_NUM)
  115. #define PCH_FIFO_THRESH 16
  116. enum pch_can_mode {
  117. PCH_CAN_ENABLE,
  118. PCH_CAN_DISABLE,
  119. PCH_CAN_ALL,
  120. PCH_CAN_NONE,
  121. PCH_CAN_STOP,
  122. PCH_CAN_RUN
  123. };
  124. struct pch_can_regs {
  125. u32 cont;
  126. u32 stat;
  127. u32 errc;
  128. u32 bitt;
  129. u32 intr;
  130. u32 opt;
  131. u32 brpe;
  132. u32 reserve1;
  133. u32 if1_creq;
  134. u32 if1_cmask;
  135. u32 if1_mask1;
  136. u32 if1_mask2;
  137. u32 if1_id1;
  138. u32 if1_id2;
  139. u32 if1_mcont;
  140. u32 if1_dataa1;
  141. u32 if1_dataa2;
  142. u32 if1_datab1;
  143. u32 if1_datab2;
  144. u32 reserve2;
  145. u32 reserve3[12];
  146. u32 if2_creq;
  147. u32 if2_cmask;
  148. u32 if2_mask1;
  149. u32 if2_mask2;
  150. u32 if2_id1;
  151. u32 if2_id2;
  152. u32 if2_mcont;
  153. u32 if2_dataa1;
  154. u32 if2_dataa2;
  155. u32 if2_datab1;
  156. u32 if2_datab2;
  157. u32 reserve4;
  158. u32 reserve5[20];
  159. u32 treq1;
  160. u32 treq2;
  161. u32 reserve6[2];
  162. u32 reserve7[56];
  163. u32 reserve8[3];
  164. u32 srst;
  165. };
  166. struct pch_can_priv {
  167. struct can_priv can;
  168. unsigned int can_num;
  169. struct pci_dev *dev;
  170. unsigned int tx_enable[MAX_MSG_OBJ];
  171. unsigned int rx_enable[MAX_MSG_OBJ];
  172. unsigned int rx_link[MAX_MSG_OBJ];
  173. unsigned int int_enables;
  174. unsigned int int_stat;
  175. struct net_device *ndev;
  176. spinlock_t msgif_reg_lock; /* Message Interface Registers Access Lock*/
  177. unsigned int msg_obj[MAX_MSG_OBJ];
  178. struct pch_can_regs __iomem *regs;
  179. struct napi_struct napi;
  180. unsigned int tx_obj; /* Point next Tx Obj index */
  181. unsigned int use_msi;
  182. };
  183. static struct can_bittiming_const pch_can_bittiming_const = {
  184. .name = KBUILD_MODNAME,
  185. .tseg1_min = 1,
  186. .tseg1_max = 16,
  187. .tseg2_min = 1,
  188. .tseg2_max = 8,
  189. .sjw_max = 4,
  190. .brp_min = 1,
  191. .brp_max = 1024, /* 6bit + extended 4bit */
  192. .brp_inc = 1,
  193. };
  194. static DEFINE_PCI_DEVICE_TABLE(pch_pci_tbl) = {
  195. {PCI_VENDOR_ID_INTEL, 0x8818, PCI_ANY_ID, PCI_ANY_ID,},
  196. {0,}
  197. };
  198. MODULE_DEVICE_TABLE(pci, pch_pci_tbl);
  199. static inline void pch_can_bit_set(void __iomem *addr, u32 mask)
  200. {
  201. iowrite32(ioread32(addr) | mask, addr);
  202. }
  203. static inline void pch_can_bit_clear(void __iomem *addr, u32 mask)
  204. {
  205. iowrite32(ioread32(addr) & ~mask, addr);
  206. }
  207. static void pch_can_set_run_mode(struct pch_can_priv *priv,
  208. enum pch_can_mode mode)
  209. {
  210. switch (mode) {
  211. case PCH_CAN_RUN:
  212. pch_can_bit_clear(&priv->regs->cont, CAN_CTRL_INIT);
  213. break;
  214. case PCH_CAN_STOP:
  215. pch_can_bit_set(&priv->regs->cont, CAN_CTRL_INIT);
  216. break;
  217. default:
  218. dev_err(&priv->ndev->dev, "%s -> Invalid Mode.\n", __func__);
  219. break;
  220. }
  221. }
  222. static void pch_can_set_optmode(struct pch_can_priv *priv)
  223. {
  224. u32 reg_val = ioread32(&priv->regs->opt);
  225. if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY)
  226. reg_val |= CAN_OPT_SILENT;
  227. if (priv->can.ctrlmode & CAN_CTRLMODE_LOOPBACK)
  228. reg_val |= CAN_OPT_LBACK;
  229. pch_can_bit_set(&priv->regs->cont, CAN_CTRL_OPT);
  230. iowrite32(reg_val, &priv->regs->opt);
  231. }
  232. static void pch_can_set_int_custom(struct pch_can_priv *priv)
  233. {
  234. /* Clearing the IE, SIE and EIE bits of Can control register. */
  235. pch_can_bit_clear(&priv->regs->cont, CAN_CTRL_IE_SIE_EIE);
  236. /* Appropriately setting them. */
  237. pch_can_bit_set(&priv->regs->cont,
  238. ((priv->int_enables & MSK_CTRL_IE_SIE_EIE) << 1));
  239. }
  240. /* This function retrieves interrupt enabled for the CAN device. */
  241. static void pch_can_get_int_enables(struct pch_can_priv *priv, u32 *enables)
  242. {
  243. /* Obtaining the status of IE, SIE and EIE interrupt bits. */
  244. *enables = ((ioread32(&priv->regs->cont) & CAN_CTRL_IE_SIE_EIE) >> 1);
  245. }
  246. static void pch_can_set_int_enables(struct pch_can_priv *priv,
  247. enum pch_can_mode interrupt_no)
  248. {
  249. switch (interrupt_no) {
  250. case PCH_CAN_ENABLE:
  251. pch_can_bit_set(&priv->regs->cont, CAN_CTRL_IE);
  252. break;
  253. case PCH_CAN_DISABLE:
  254. pch_can_bit_clear(&priv->regs->cont, CAN_CTRL_IE);
  255. break;
  256. case PCH_CAN_ALL:
  257. pch_can_bit_set(&priv->regs->cont, CAN_CTRL_IE_SIE_EIE);
  258. break;
  259. case PCH_CAN_NONE:
  260. pch_can_bit_clear(&priv->regs->cont, CAN_CTRL_IE_SIE_EIE);
  261. break;
  262. default:
  263. dev_err(&priv->ndev->dev, "Invalid interrupt number.\n");
  264. break;
  265. }
  266. }
  267. static void pch_can_check_if_busy(u32 __iomem *creq_addr, u32 num)
  268. {
  269. u32 counter = COUNTER_LIMIT;
  270. u32 ifx_creq;
  271. iowrite32(num, creq_addr);
  272. while (counter) {
  273. ifx_creq = ioread32(creq_addr) & CAN_IF_CREQ_BUSY;
  274. if (!ifx_creq)
  275. break;
  276. counter--;
  277. udelay(1);
  278. }
  279. if (!counter)
  280. pr_err("%s:IF1 BUSY Flag is set forever.\n", __func__);
  281. }
  282. static void pch_can_set_rx_enable(struct pch_can_priv *priv, u32 buff_num,
  283. u32 set)
  284. {
  285. unsigned long flags;
  286. spin_lock_irqsave(&priv->msgif_reg_lock, flags);
  287. /* Reading the receive buffer data from RAM to Interface1 registers */
  288. iowrite32(CAN_CMASK_RX_TX_GET, &priv->regs->if1_cmask);
  289. pch_can_check_if_busy(&priv->regs->if1_creq, buff_num);
  290. /* Setting the IF1MASK1 register to access MsgVal and RxIE bits */
  291. iowrite32(CAN_CMASK_RDWR | CAN_CMASK_ARB | CAN_CMASK_CTRL,
  292. &priv->regs->if1_cmask);
  293. if (set == ENABLE) {
  294. /* Setting the MsgVal and RxIE bits */
  295. pch_can_bit_set(&priv->regs->if1_mcont, CAN_IF_MCONT_RXIE);
  296. pch_can_bit_set(&priv->regs->if1_id2, CAN_ID_MSGVAL);
  297. } else if (set == DISABLE) {
  298. /* Resetting the MsgVal and RxIE bits */
  299. pch_can_bit_clear(&priv->regs->if1_mcont, CAN_IF_MCONT_RXIE);
  300. pch_can_bit_clear(&priv->regs->if1_id2, CAN_ID_MSGVAL);
  301. }
  302. pch_can_check_if_busy(&priv->regs->if1_creq, buff_num);
  303. spin_unlock_irqrestore(&priv->msgif_reg_lock, flags);
  304. }
  305. static void pch_can_rx_enable_all(struct pch_can_priv *priv)
  306. {
  307. int i;
  308. /* Traversing to obtain the object configured as receivers. */
  309. for (i = 0; i < PCH_OBJ_NUM; i++) {
  310. if (priv->msg_obj[i] == MSG_OBJ_RX)
  311. pch_can_set_rx_enable(priv, i + 1, ENABLE);
  312. }
  313. }
  314. static void pch_can_rx_disable_all(struct pch_can_priv *priv)
  315. {
  316. int i;
  317. /* Traversing to obtain the object configured as receivers. */
  318. for (i = 0; i < PCH_OBJ_NUM; i++) {
  319. if (priv->msg_obj[i] == MSG_OBJ_RX)
  320. pch_can_set_rx_enable(priv, i + 1, DISABLE);
  321. }
  322. }
  323. static void pch_can_set_tx_enable(struct pch_can_priv *priv, u32 buff_num,
  324. u32 set)
  325. {
  326. unsigned long flags;
  327. spin_lock_irqsave(&priv->msgif_reg_lock, flags);
  328. /* Reading the Msg buffer from Message RAM to Interface2 registers. */
  329. iowrite32(CAN_CMASK_RX_TX_GET, &priv->regs->if2_cmask);
  330. pch_can_check_if_busy(&priv->regs->if2_creq, buff_num);
  331. /* Setting the IF2CMASK register for accessing the
  332. MsgVal and TxIE bits */
  333. iowrite32(CAN_CMASK_RDWR | CAN_CMASK_ARB | CAN_CMASK_CTRL,
  334. &priv->regs->if2_cmask);
  335. if (set == ENABLE) {
  336. /* Setting the MsgVal and TxIE bits */
  337. pch_can_bit_set(&priv->regs->if2_mcont, CAN_IF_MCONT_TXIE);
  338. pch_can_bit_set(&priv->regs->if2_id2, CAN_ID_MSGVAL);
  339. } else if (set == DISABLE) {
  340. /* Resetting the MsgVal and TxIE bits. */
  341. pch_can_bit_clear(&priv->regs->if2_mcont, CAN_IF_MCONT_TXIE);
  342. pch_can_bit_clear(&priv->regs->if2_id2, CAN_ID_MSGVAL);
  343. }
  344. pch_can_check_if_busy(&priv->regs->if2_creq, buff_num);
  345. spin_unlock_irqrestore(&priv->msgif_reg_lock, flags);
  346. }
  347. static void pch_can_tx_enable_all(struct pch_can_priv *priv)
  348. {
  349. int i;
  350. /* Traversing to obtain the object configured as transmit object. */
  351. for (i = 0; i < PCH_OBJ_NUM; i++) {
  352. if (priv->msg_obj[i] == MSG_OBJ_TX)
  353. pch_can_set_tx_enable(priv, i + 1, ENABLE);
  354. }
  355. }
  356. static void pch_can_tx_disable_all(struct pch_can_priv *priv)
  357. {
  358. int i;
  359. /* Traversing to obtain the object configured as transmit object. */
  360. for (i = 0; i < PCH_OBJ_NUM; i++) {
  361. if (priv->msg_obj[i] == MSG_OBJ_TX)
  362. pch_can_set_tx_enable(priv, i + 1, DISABLE);
  363. }
  364. }
  365. static void pch_can_get_rx_enable(struct pch_can_priv *priv, u32 buff_num,
  366. u32 *enable)
  367. {
  368. unsigned long flags;
  369. spin_lock_irqsave(&priv->msgif_reg_lock, flags);
  370. iowrite32(CAN_CMASK_RX_TX_GET, &priv->regs->if1_cmask);
  371. pch_can_check_if_busy(&priv->regs->if1_creq, buff_num);
  372. if (((ioread32(&priv->regs->if1_id2)) & CAN_ID_MSGVAL) &&
  373. ((ioread32(&priv->regs->if1_mcont)) &
  374. CAN_IF_MCONT_RXIE))
  375. *enable = ENABLE;
  376. else
  377. *enable = DISABLE;
  378. spin_unlock_irqrestore(&priv->msgif_reg_lock, flags);
  379. }
  380. static void pch_can_get_tx_enable(struct pch_can_priv *priv, u32 buff_num,
  381. u32 *enable)
  382. {
  383. unsigned long flags;
  384. spin_lock_irqsave(&priv->msgif_reg_lock, flags);
  385. iowrite32(CAN_CMASK_RX_TX_GET, &priv->regs->if2_cmask);
  386. pch_can_check_if_busy(&priv->regs->if2_creq, buff_num);
  387. if (((ioread32(&priv->regs->if2_id2)) & CAN_ID_MSGVAL) &&
  388. ((ioread32(&priv->regs->if2_mcont)) &
  389. CAN_IF_MCONT_TXIE)) {
  390. *enable = ENABLE;
  391. } else {
  392. *enable = DISABLE;
  393. }
  394. spin_unlock_irqrestore(&priv->msgif_reg_lock, flags);
  395. }
  396. static int pch_can_int_pending(struct pch_can_priv *priv)
  397. {
  398. return ioread32(&priv->regs->intr) & 0xffff;
  399. }
  400. static void pch_can_set_rx_buffer_link(struct pch_can_priv *priv,
  401. u32 buffer_num, u32 set)
  402. {
  403. unsigned long flags;
  404. spin_lock_irqsave(&priv->msgif_reg_lock, flags);
  405. iowrite32(CAN_CMASK_RX_TX_GET, &priv->regs->if1_cmask);
  406. pch_can_check_if_busy(&priv->regs->if1_creq, buffer_num);
  407. iowrite32(CAN_CMASK_RDWR | CAN_CMASK_CTRL, &priv->regs->if1_cmask);
  408. if (set == ENABLE)
  409. pch_can_bit_clear(&priv->regs->if1_mcont, CAN_IF_MCONT_EOB);
  410. else
  411. pch_can_bit_set(&priv->regs->if1_mcont, CAN_IF_MCONT_EOB);
  412. pch_can_check_if_busy(&priv->regs->if1_creq, buffer_num);
  413. spin_unlock_irqrestore(&priv->msgif_reg_lock, flags);
  414. }
  415. static void pch_can_get_rx_buffer_link(struct pch_can_priv *priv,
  416. u32 buffer_num, u32 *link)
  417. {
  418. unsigned long flags;
  419. spin_lock_irqsave(&priv->msgif_reg_lock, flags);
  420. iowrite32(CAN_CMASK_RX_TX_GET, &priv->regs->if1_cmask);
  421. pch_can_check_if_busy(&priv->regs->if1_creq, buffer_num);
  422. if (ioread32(&priv->regs->if1_mcont) & CAN_IF_MCONT_EOB)
  423. *link = DISABLE;
  424. else
  425. *link = ENABLE;
  426. spin_unlock_irqrestore(&priv->msgif_reg_lock, flags);
  427. }
  428. static void pch_can_clear_buffers(struct pch_can_priv *priv)
  429. {
  430. int i;
  431. for (i = 0; i < PCH_RX_OBJ_NUM; i++) {
  432. iowrite32(CAN_CMASK_RX_TX_SET, &priv->regs->if1_cmask);
  433. iowrite32(0xffff, &priv->regs->if1_mask1);
  434. iowrite32(0xffff, &priv->regs->if1_mask2);
  435. iowrite32(0x0, &priv->regs->if1_id1);
  436. iowrite32(0x0, &priv->regs->if1_id2);
  437. iowrite32(0x0, &priv->regs->if1_mcont);
  438. iowrite32(0x0, &priv->regs->if1_dataa1);
  439. iowrite32(0x0, &priv->regs->if1_dataa2);
  440. iowrite32(0x0, &priv->regs->if1_datab1);
  441. iowrite32(0x0, &priv->regs->if1_datab2);
  442. iowrite32(CAN_CMASK_RDWR | CAN_CMASK_MASK |
  443. CAN_CMASK_ARB | CAN_CMASK_CTRL,
  444. &priv->regs->if1_cmask);
  445. pch_can_check_if_busy(&priv->regs->if1_creq, i+1);
  446. }
  447. for (i = i; i < PCH_OBJ_NUM; i++) {
  448. iowrite32(CAN_CMASK_RX_TX_SET, &priv->regs->if2_cmask);
  449. iowrite32(0xffff, &priv->regs->if2_mask1);
  450. iowrite32(0xffff, &priv->regs->if2_mask2);
  451. iowrite32(0x0, &priv->regs->if2_id1);
  452. iowrite32(0x0, &priv->regs->if2_id2);
  453. iowrite32(0x0, &priv->regs->if2_mcont);
  454. iowrite32(0x0, &priv->regs->if2_dataa1);
  455. iowrite32(0x0, &priv->regs->if2_dataa2);
  456. iowrite32(0x0, &priv->regs->if2_datab1);
  457. iowrite32(0x0, &priv->regs->if2_datab2);
  458. iowrite32(CAN_CMASK_RDWR | CAN_CMASK_MASK |
  459. CAN_CMASK_ARB | CAN_CMASK_CTRL,
  460. &priv->regs->if2_cmask);
  461. pch_can_check_if_busy(&priv->regs->if2_creq, i+1);
  462. }
  463. }
  464. static void pch_can_config_rx_tx_buffers(struct pch_can_priv *priv)
  465. {
  466. int i;
  467. unsigned long flags;
  468. spin_lock_irqsave(&priv->msgif_reg_lock, flags);
  469. for (i = 0; i < PCH_OBJ_NUM; i++) {
  470. if (priv->msg_obj[i] == MSG_OBJ_RX) {
  471. iowrite32(CAN_CMASK_RX_TX_GET,
  472. &priv->regs->if1_cmask);
  473. pch_can_check_if_busy(&priv->regs->if1_creq, i+1);
  474. iowrite32(0x0, &priv->regs->if1_id1);
  475. iowrite32(0x0, &priv->regs->if1_id2);
  476. pch_can_bit_set(&priv->regs->if1_mcont,
  477. CAN_IF_MCONT_UMASK);
  478. /* Set FIFO mode set to 0 except last Rx Obj*/
  479. pch_can_bit_clear(&priv->regs->if1_mcont,
  480. CAN_IF_MCONT_EOB);
  481. /* In case FIFO mode, Last EoB of Rx Obj must be 1 */
  482. if (i == (PCH_RX_OBJ_NUM - 1))
  483. pch_can_bit_set(&priv->regs->if1_mcont,
  484. CAN_IF_MCONT_EOB);
  485. iowrite32(0, &priv->regs->if1_mask1);
  486. pch_can_bit_clear(&priv->regs->if1_mask2,
  487. 0x1fff | CAN_MASK2_MDIR_MXTD);
  488. /* Setting CMASK for writing */
  489. iowrite32(CAN_CMASK_RDWR | CAN_CMASK_MASK |
  490. CAN_CMASK_ARB | CAN_CMASK_CTRL,
  491. &priv->regs->if1_cmask);
  492. pch_can_check_if_busy(&priv->regs->if1_creq, i+1);
  493. } else if (priv->msg_obj[i] == MSG_OBJ_TX) {
  494. iowrite32(CAN_CMASK_RX_TX_GET,
  495. &priv->regs->if2_cmask);
  496. pch_can_check_if_busy(&priv->regs->if2_creq, i+1);
  497. /* Resetting DIR bit for reception */
  498. iowrite32(0x0, &priv->regs->if2_id1);
  499. iowrite32(0x0, &priv->regs->if2_id2);
  500. pch_can_bit_set(&priv->regs->if2_id2, CAN_ID2_DIR);
  501. /* Setting EOB bit for transmitter */
  502. iowrite32(CAN_IF_MCONT_EOB, &priv->regs->if2_mcont);
  503. pch_can_bit_set(&priv->regs->if2_mcont,
  504. CAN_IF_MCONT_UMASK);
  505. iowrite32(0, &priv->regs->if2_mask1);
  506. pch_can_bit_clear(&priv->regs->if2_mask2, 0x1fff);
  507. /* Setting CMASK for writing */
  508. iowrite32(CAN_CMASK_RDWR | CAN_CMASK_MASK |
  509. CAN_CMASK_ARB | CAN_CMASK_CTRL,
  510. &priv->regs->if2_cmask);
  511. pch_can_check_if_busy(&priv->regs->if2_creq, i+1);
  512. }
  513. }
  514. spin_unlock_irqrestore(&priv->msgif_reg_lock, flags);
  515. }
  516. static void pch_can_init(struct pch_can_priv *priv)
  517. {
  518. /* Stopping the Can device. */
  519. pch_can_set_run_mode(priv, PCH_CAN_STOP);
  520. /* Clearing all the message object buffers. */
  521. pch_can_clear_buffers(priv);
  522. /* Configuring the respective message object as either rx/tx object. */
  523. pch_can_config_rx_tx_buffers(priv);
  524. /* Enabling the interrupts. */
  525. pch_can_set_int_enables(priv, PCH_CAN_ALL);
  526. }
  527. static void pch_can_release(struct pch_can_priv *priv)
  528. {
  529. /* Stooping the CAN device. */
  530. pch_can_set_run_mode(priv, PCH_CAN_STOP);
  531. /* Disabling the interrupts. */
  532. pch_can_set_int_enables(priv, PCH_CAN_NONE);
  533. /* Disabling all the receive object. */
  534. pch_can_rx_disable_all(priv);
  535. /* Disabling all the transmit object. */
  536. pch_can_tx_disable_all(priv);
  537. }
  538. /* This function clears interrupt(s) from the CAN device. */
  539. static void pch_can_int_clr(struct pch_can_priv *priv, u32 mask)
  540. {
  541. if (mask == CAN_STATUS_INT) {
  542. ioread32(&priv->regs->stat);
  543. return;
  544. }
  545. /* Clear interrupt for transmit object */
  546. if (priv->msg_obj[mask - 1] == MSG_OBJ_TX) {
  547. /* Setting CMASK for clearing interrupts for
  548. frame transmission. */
  549. iowrite32(CAN_CMASK_RDWR | CAN_CMASK_CTRL | CAN_CMASK_ARB,
  550. &priv->regs->if2_cmask);
  551. /* Resetting the ID registers. */
  552. pch_can_bit_set(&priv->regs->if2_id2,
  553. CAN_ID2_DIR | (0x7ff << 2));
  554. iowrite32(0x0, &priv->regs->if2_id1);
  555. /* Claring NewDat, TxRqst & IntPnd */
  556. pch_can_bit_clear(&priv->regs->if2_mcont,
  557. CAN_IF_MCONT_NEWDAT | CAN_IF_MCONT_INTPND |
  558. CAN_IF_MCONT_TXRQXT);
  559. pch_can_check_if_busy(&priv->regs->if2_creq, mask);
  560. } else if (priv->msg_obj[mask - 1] == MSG_OBJ_RX) {
  561. /* Setting CMASK for clearing the reception interrupts. */
  562. iowrite32(CAN_CMASK_RDWR | CAN_CMASK_CTRL | CAN_CMASK_ARB,
  563. &priv->regs->if1_cmask);
  564. /* Clearing the Dir bit. */
  565. pch_can_bit_clear(&priv->regs->if1_id2, CAN_ID2_DIR);
  566. /* Clearing NewDat & IntPnd */
  567. pch_can_bit_clear(&priv->regs->if1_mcont,
  568. CAN_IF_MCONT_NEWDAT | CAN_IF_MCONT_INTPND);
  569. pch_can_check_if_busy(&priv->regs->if1_creq, mask);
  570. }
  571. }
  572. static int pch_can_get_buffer_status(struct pch_can_priv *priv)
  573. {
  574. return (ioread32(&priv->regs->treq1) & 0xffff) |
  575. ((ioread32(&priv->regs->treq2) & 0xffff) << 16);
  576. }
  577. static void pch_can_reset(struct pch_can_priv *priv)
  578. {
  579. /* write to sw reset register */
  580. iowrite32(1, &priv->regs->srst);
  581. iowrite32(0, &priv->regs->srst);
  582. }
  583. static void pch_can_error(struct net_device *ndev, u32 status)
  584. {
  585. struct sk_buff *skb;
  586. struct pch_can_priv *priv = netdev_priv(ndev);
  587. struct can_frame *cf;
  588. u32 errc;
  589. struct net_device_stats *stats = &(priv->ndev->stats);
  590. enum can_state state = priv->can.state;
  591. skb = alloc_can_err_skb(ndev, &cf);
  592. if (!skb)
  593. return;
  594. if (status & PCH_BUS_OFF) {
  595. pch_can_tx_disable_all(priv);
  596. pch_can_rx_disable_all(priv);
  597. state = CAN_STATE_BUS_OFF;
  598. cf->can_id |= CAN_ERR_BUSOFF;
  599. can_bus_off(ndev);
  600. pch_can_set_run_mode(priv, PCH_CAN_RUN);
  601. dev_err(&ndev->dev, "%s -> Bus Off occurres.\n", __func__);
  602. }
  603. /* Warning interrupt. */
  604. if (status & PCH_EWARN) {
  605. state = CAN_STATE_ERROR_WARNING;
  606. priv->can.can_stats.error_warning++;
  607. cf->can_id |= CAN_ERR_CRTL;
  608. errc = ioread32(&priv->regs->errc);
  609. if (((errc & CAN_REC) >> 8) > 96)
  610. cf->data[1] |= CAN_ERR_CRTL_RX_WARNING;
  611. if ((errc & CAN_TEC) > 96)
  612. cf->data[1] |= CAN_ERR_CRTL_TX_WARNING;
  613. dev_warn(&ndev->dev,
  614. "%s -> Error Counter is more than 96.\n", __func__);
  615. }
  616. /* Error passive interrupt. */
  617. if (status & PCH_EPASSIV) {
  618. priv->can.can_stats.error_passive++;
  619. state = CAN_STATE_ERROR_PASSIVE;
  620. cf->can_id |= CAN_ERR_CRTL;
  621. errc = ioread32(&priv->regs->errc);
  622. if (((errc & CAN_REC) >> 8) > 127)
  623. cf->data[1] |= CAN_ERR_CRTL_RX_PASSIVE;
  624. if ((errc & CAN_TEC) > 127)
  625. cf->data[1] |= CAN_ERR_CRTL_TX_PASSIVE;
  626. dev_err(&ndev->dev,
  627. "%s -> CAN controller is ERROR PASSIVE .\n", __func__);
  628. }
  629. if (status & PCH_LEC_ALL) {
  630. priv->can.can_stats.bus_error++;
  631. stats->rx_errors++;
  632. switch (status & PCH_LEC_ALL) {
  633. case PCH_STUF_ERR:
  634. cf->data[2] |= CAN_ERR_PROT_STUFF;
  635. break;
  636. case PCH_FORM_ERR:
  637. cf->data[2] |= CAN_ERR_PROT_FORM;
  638. break;
  639. case PCH_ACK_ERR:
  640. cf->data[2] |= CAN_ERR_PROT_LOC_ACK |
  641. CAN_ERR_PROT_LOC_ACK_DEL;
  642. break;
  643. case PCH_BIT1_ERR:
  644. case PCH_BIT0_ERR:
  645. cf->data[2] |= CAN_ERR_PROT_BIT;
  646. break;
  647. case PCH_CRC_ERR:
  648. cf->data[2] |= CAN_ERR_PROT_LOC_CRC_SEQ |
  649. CAN_ERR_PROT_LOC_CRC_DEL;
  650. break;
  651. default:
  652. iowrite32(status | PCH_LEC_ALL, &priv->regs->stat);
  653. break;
  654. }
  655. }
  656. priv->can.state = state;
  657. netif_rx(skb);
  658. stats->rx_packets++;
  659. stats->rx_bytes += cf->can_dlc;
  660. }
  661. static irqreturn_t pch_can_interrupt(int irq, void *dev_id)
  662. {
  663. struct net_device *ndev = (struct net_device *)dev_id;
  664. struct pch_can_priv *priv = netdev_priv(ndev);
  665. pch_can_set_int_enables(priv, PCH_CAN_NONE);
  666. napi_schedule(&priv->napi);
  667. return IRQ_HANDLED;
  668. }
  669. static int pch_can_rx_normal(struct net_device *ndev, u32 int_stat)
  670. {
  671. u32 reg;
  672. canid_t id;
  673. u32 ide;
  674. u32 rtr;
  675. int i, j, k;
  676. int rcv_pkts = 0;
  677. struct sk_buff *skb;
  678. struct can_frame *cf;
  679. struct pch_can_priv *priv = netdev_priv(ndev);
  680. struct net_device_stats *stats = &(priv->ndev->stats);
  681. /* Reading the messsage object from the Message RAM */
  682. iowrite32(CAN_CMASK_RX_TX_GET, &priv->regs->if1_cmask);
  683. pch_can_check_if_busy(&priv->regs->if1_creq, int_stat);
  684. /* Reading the MCONT register. */
  685. reg = ioread32(&priv->regs->if1_mcont);
  686. reg &= 0xffff;
  687. for (k = int_stat; !(reg & CAN_IF_MCONT_EOB); k++) {
  688. /* If MsgLost bit set. */
  689. if (reg & CAN_IF_MCONT_MSGLOST) {
  690. dev_err(&priv->ndev->dev, "Msg Obj is overwritten.\n");
  691. pch_can_bit_clear(&priv->regs->if1_mcont,
  692. CAN_IF_MCONT_MSGLOST);
  693. iowrite32(CAN_CMASK_RDWR | CAN_CMASK_CTRL,
  694. &priv->regs->if1_cmask);
  695. pch_can_check_if_busy(&priv->regs->if1_creq, k);
  696. skb = alloc_can_err_skb(ndev, &cf);
  697. if (!skb)
  698. return -ENOMEM;
  699. priv->can.can_stats.error_passive++;
  700. priv->can.state = CAN_STATE_ERROR_PASSIVE;
  701. cf->can_id |= CAN_ERR_CRTL;
  702. cf->data[1] |= CAN_ERR_CRTL_RX_OVERFLOW;
  703. cf->data[2] |= CAN_ERR_PROT_OVERLOAD;
  704. stats->rx_packets++;
  705. stats->rx_bytes += cf->can_dlc;
  706. netif_receive_skb(skb);
  707. rcv_pkts++;
  708. goto RX_NEXT;
  709. }
  710. if (!(reg & CAN_IF_MCONT_NEWDAT))
  711. goto RX_NEXT;
  712. skb = alloc_can_skb(priv->ndev, &cf);
  713. if (!skb)
  714. return -ENOMEM;
  715. /* Get Received data */
  716. ide = ((ioread32(&priv->regs->if1_id2)) & CAN_ID2_XTD) >> 14;
  717. if (ide) {
  718. id = (ioread32(&priv->regs->if1_id1) & 0xffff);
  719. id |= (((ioread32(&priv->regs->if1_id2)) &
  720. 0x1fff) << 16);
  721. cf->can_id = (id & CAN_EFF_MASK) | CAN_EFF_FLAG;
  722. } else {
  723. id = (((ioread32(&priv->regs->if1_id2)) &
  724. (CAN_SFF_MASK << 2)) >> 2);
  725. cf->can_id = (id & CAN_SFF_MASK);
  726. }
  727. rtr = (ioread32(&priv->regs->if1_id2) & CAN_ID2_DIR);
  728. if (rtr) {
  729. cf->can_dlc = 0;
  730. cf->can_id |= CAN_RTR_FLAG;
  731. } else {
  732. cf->can_dlc = ((ioread32(&priv->regs->if1_mcont)) &
  733. 0x0f);
  734. }
  735. for (i = 0, j = 0; i < cf->can_dlc; j++) {
  736. reg = ioread32(&priv->regs->if1_dataa1 + j*4);
  737. cf->data[i++] = cpu_to_le32(reg & 0xff);
  738. if (i == cf->can_dlc)
  739. break;
  740. cf->data[i++] = cpu_to_le32((reg >> 8) & 0xff);
  741. }
  742. netif_receive_skb(skb);
  743. rcv_pkts++;
  744. stats->rx_packets++;
  745. stats->rx_bytes += cf->can_dlc;
  746. if (k < PCH_FIFO_THRESH) {
  747. iowrite32(CAN_CMASK_RDWR | CAN_CMASK_CTRL |
  748. CAN_CMASK_ARB, &priv->regs->if1_cmask);
  749. /* Clearing the Dir bit. */
  750. pch_can_bit_clear(&priv->regs->if1_id2, CAN_ID2_DIR);
  751. /* Clearing NewDat & IntPnd */
  752. pch_can_bit_clear(&priv->regs->if1_mcont,
  753. CAN_IF_MCONT_INTPND);
  754. pch_can_check_if_busy(&priv->regs->if1_creq, k);
  755. } else if (k > PCH_FIFO_THRESH) {
  756. pch_can_int_clr(priv, k);
  757. } else if (k == PCH_FIFO_THRESH) {
  758. int cnt;
  759. for (cnt = 0; cnt < PCH_FIFO_THRESH; cnt++)
  760. pch_can_int_clr(priv, cnt+1);
  761. }
  762. RX_NEXT:
  763. /* Reading the messsage object from the Message RAM */
  764. iowrite32(CAN_CMASK_RX_TX_GET, &priv->regs->if1_cmask);
  765. pch_can_check_if_busy(&priv->regs->if1_creq, k + 1);
  766. reg = ioread32(&priv->regs->if1_mcont);
  767. }
  768. return rcv_pkts;
  769. }
  770. static int pch_can_rx_poll(struct napi_struct *napi, int quota)
  771. {
  772. struct net_device *ndev = napi->dev;
  773. struct pch_can_priv *priv = netdev_priv(ndev);
  774. struct net_device_stats *stats = &(priv->ndev->stats);
  775. u32 dlc;
  776. u32 int_stat;
  777. int rcv_pkts = 0;
  778. u32 reg_stat;
  779. unsigned long flags;
  780. int_stat = pch_can_int_pending(priv);
  781. if (!int_stat)
  782. return 0;
  783. INT_STAT:
  784. if (int_stat == CAN_STATUS_INT) {
  785. reg_stat = ioread32(&priv->regs->stat);
  786. if (reg_stat & (PCH_BUS_OFF | PCH_LEC_ALL)) {
  787. if ((reg_stat & PCH_LEC_ALL) != PCH_LEC_ALL)
  788. pch_can_error(ndev, reg_stat);
  789. }
  790. if (reg_stat & PCH_TX_OK) {
  791. spin_lock_irqsave(&priv->msgif_reg_lock, flags);
  792. iowrite32(CAN_CMASK_RX_TX_GET, &priv->regs->if2_cmask);
  793. pch_can_check_if_busy(&priv->regs->if2_creq,
  794. ioread32(&priv->regs->intr));
  795. spin_unlock_irqrestore(&priv->msgif_reg_lock, flags);
  796. pch_can_bit_clear(&priv->regs->stat, PCH_TX_OK);
  797. }
  798. if (reg_stat & PCH_RX_OK)
  799. pch_can_bit_clear(&priv->regs->stat, PCH_RX_OK);
  800. int_stat = pch_can_int_pending(priv);
  801. if (int_stat == CAN_STATUS_INT)
  802. goto INT_STAT;
  803. }
  804. MSG_OBJ:
  805. if ((int_stat >= 1) && (int_stat <= PCH_RX_OBJ_NUM)) {
  806. spin_lock_irqsave(&priv->msgif_reg_lock, flags);
  807. rcv_pkts = pch_can_rx_normal(ndev, int_stat);
  808. spin_unlock_irqrestore(&priv->msgif_reg_lock, flags);
  809. if (rcv_pkts < 0)
  810. return 0;
  811. } else if ((int_stat > PCH_RX_OBJ_NUM) && (int_stat <= PCH_OBJ_NUM)) {
  812. if (priv->msg_obj[int_stat - 1] == MSG_OBJ_TX) {
  813. /* Handle transmission interrupt */
  814. can_get_echo_skb(ndev, int_stat - PCH_RX_OBJ_NUM - 1);
  815. spin_lock_irqsave(&priv->msgif_reg_lock, flags);
  816. iowrite32(CAN_CMASK_RX_TX_GET | CAN_CMASK_CLRINTPND,
  817. &priv->regs->if2_cmask);
  818. dlc = ioread32(&priv->regs->if2_mcont) &
  819. CAN_IF_MCONT_DLC;
  820. pch_can_check_if_busy(&priv->regs->if2_creq, int_stat);
  821. spin_unlock_irqrestore(&priv->msgif_reg_lock, flags);
  822. if (dlc > 8)
  823. dlc = 8;
  824. stats->tx_bytes += dlc;
  825. stats->tx_packets++;
  826. }
  827. }
  828. int_stat = pch_can_int_pending(priv);
  829. if (int_stat == CAN_STATUS_INT)
  830. goto INT_STAT;
  831. else if (int_stat >= 1 && int_stat <= 32)
  832. goto MSG_OBJ;
  833. napi_complete(napi);
  834. pch_can_set_int_enables(priv, PCH_CAN_ALL);
  835. return rcv_pkts;
  836. }
  837. static int pch_set_bittiming(struct net_device *ndev)
  838. {
  839. struct pch_can_priv *priv = netdev_priv(ndev);
  840. const struct can_bittiming *bt = &priv->can.bittiming;
  841. u32 canbit;
  842. u32 bepe;
  843. u32 brp;
  844. /* Setting the CCE bit for accessing the Can Timing register. */
  845. pch_can_bit_set(&priv->regs->cont, CAN_CTRL_CCE);
  846. brp = (bt->tq) / (1000000000/PCH_CAN_CLK) - 1;
  847. canbit = brp & MSK_BITT_BRP;
  848. canbit |= (bt->sjw - 1) << BIT_BITT_SJW;
  849. canbit |= (bt->phase_seg1 + bt->prop_seg - 1) << BIT_BITT_TSEG1;
  850. canbit |= (bt->phase_seg2 - 1) << BIT_BITT_TSEG2;
  851. bepe = (brp & MSK_BRPE_BRPE) >> BIT_BRPE_BRPE;
  852. iowrite32(canbit, &priv->regs->bitt);
  853. iowrite32(bepe, &priv->regs->brpe);
  854. pch_can_bit_clear(&priv->regs->cont, CAN_CTRL_CCE);
  855. return 0;
  856. }
  857. static void pch_can_start(struct net_device *ndev)
  858. {
  859. struct pch_can_priv *priv = netdev_priv(ndev);
  860. if (priv->can.state != CAN_STATE_STOPPED)
  861. pch_can_reset(priv);
  862. pch_set_bittiming(ndev);
  863. pch_can_set_optmode(priv);
  864. pch_can_tx_enable_all(priv);
  865. pch_can_rx_enable_all(priv);
  866. /* Setting the CAN to run mode. */
  867. pch_can_set_run_mode(priv, PCH_CAN_RUN);
  868. priv->can.state = CAN_STATE_ERROR_ACTIVE;
  869. return;
  870. }
  871. static int pch_can_do_set_mode(struct net_device *ndev, enum can_mode mode)
  872. {
  873. int ret = 0;
  874. switch (mode) {
  875. case CAN_MODE_START:
  876. pch_can_start(ndev);
  877. netif_wake_queue(ndev);
  878. break;
  879. default:
  880. ret = -EOPNOTSUPP;
  881. break;
  882. }
  883. return ret;
  884. }
  885. static int pch_can_open(struct net_device *ndev)
  886. {
  887. struct pch_can_priv *priv = netdev_priv(ndev);
  888. int retval;
  889. retval = pci_enable_msi(priv->dev);
  890. if (retval) {
  891. dev_info(&ndev->dev, "PCH CAN opened without MSI\n");
  892. priv->use_msi = 0;
  893. } else {
  894. dev_info(&ndev->dev, "PCH CAN opened with MSI\n");
  895. priv->use_msi = 1;
  896. }
  897. /* Regsitering the interrupt. */
  898. retval = request_irq(priv->dev->irq, pch_can_interrupt, IRQF_SHARED,
  899. ndev->name, ndev);
  900. if (retval) {
  901. dev_err(&ndev->dev, "request_irq failed.\n");
  902. goto req_irq_err;
  903. }
  904. /* Open common can device */
  905. retval = open_candev(ndev);
  906. if (retval) {
  907. dev_err(ndev->dev.parent, "open_candev() failed %d\n", retval);
  908. goto err_open_candev;
  909. }
  910. pch_can_init(priv);
  911. pch_can_start(ndev);
  912. napi_enable(&priv->napi);
  913. netif_start_queue(ndev);
  914. return 0;
  915. err_open_candev:
  916. free_irq(priv->dev->irq, ndev);
  917. req_irq_err:
  918. if (priv->use_msi)
  919. pci_disable_msi(priv->dev);
  920. pch_can_release(priv);
  921. return retval;
  922. }
  923. static int pch_close(struct net_device *ndev)
  924. {
  925. struct pch_can_priv *priv = netdev_priv(ndev);
  926. netif_stop_queue(ndev);
  927. napi_disable(&priv->napi);
  928. pch_can_release(priv);
  929. free_irq(priv->dev->irq, ndev);
  930. if (priv->use_msi)
  931. pci_disable_msi(priv->dev);
  932. close_candev(ndev);
  933. priv->can.state = CAN_STATE_STOPPED;
  934. return 0;
  935. }
  936. static int pch_get_msg_obj_sts(struct net_device *ndev, u32 obj_id)
  937. {
  938. u32 buffer_status = 0;
  939. struct pch_can_priv *priv = netdev_priv(ndev);
  940. /* Getting the message object status. */
  941. buffer_status = (u32) pch_can_get_buffer_status(priv);
  942. return buffer_status & obj_id;
  943. }
  944. static netdev_tx_t pch_xmit(struct sk_buff *skb, struct net_device *ndev)
  945. {
  946. int i, j;
  947. unsigned long flags;
  948. struct pch_can_priv *priv = netdev_priv(ndev);
  949. struct can_frame *cf = (struct can_frame *)skb->data;
  950. int tx_buffer_avail = 0;
  951. if (can_dropped_invalid_skb(ndev, skb))
  952. return NETDEV_TX_OK;
  953. if (priv->tx_obj == (PCH_OBJ_NUM + 1)) { /* Point tail Obj */
  954. while (pch_get_msg_obj_sts(ndev, (((1 << PCH_TX_OBJ_NUM)-1) <<
  955. PCH_RX_OBJ_NUM)))
  956. udelay(500);
  957. priv->tx_obj = PCH_RX_OBJ_NUM + 1; /* Point head of Tx Obj ID */
  958. tx_buffer_avail = priv->tx_obj; /* Point Tail of Tx Obj */
  959. } else {
  960. tx_buffer_avail = priv->tx_obj;
  961. }
  962. priv->tx_obj++;
  963. /* Attaining the lock. */
  964. spin_lock_irqsave(&priv->msgif_reg_lock, flags);
  965. /* Reading the Msg Obj from the Msg RAM to the Interface register. */
  966. iowrite32(CAN_CMASK_RX_TX_GET, &priv->regs->if2_cmask);
  967. pch_can_check_if_busy(&priv->regs->if2_creq, tx_buffer_avail);
  968. /* Setting the CMASK register. */
  969. pch_can_bit_set(&priv->regs->if2_cmask, CAN_CMASK_ALL);
  970. /* If ID extended is set. */
  971. pch_can_bit_clear(&priv->regs->if2_id1, 0xffff);
  972. pch_can_bit_clear(&priv->regs->if2_id2, 0x1fff | CAN_ID2_XTD);
  973. if (cf->can_id & CAN_EFF_FLAG) {
  974. pch_can_bit_set(&priv->regs->if2_id1, cf->can_id & 0xffff);
  975. pch_can_bit_set(&priv->regs->if2_id2,
  976. ((cf->can_id >> 16) & 0x1fff) | CAN_ID2_XTD);
  977. } else {
  978. pch_can_bit_set(&priv->regs->if2_id1, 0);
  979. pch_can_bit_set(&priv->regs->if2_id2,
  980. (cf->can_id & CAN_SFF_MASK) << 2);
  981. }
  982. /* If remote frame has to be transmitted.. */
  983. if (cf->can_id & CAN_RTR_FLAG)
  984. pch_can_bit_clear(&priv->regs->if2_id2, CAN_ID2_DIR);
  985. for (i = 0, j = 0; i < cf->can_dlc; j++) {
  986. iowrite32(le32_to_cpu(cf->data[i++]),
  987. (&priv->regs->if2_dataa1) + j*4);
  988. if (i == cf->can_dlc)
  989. break;
  990. iowrite32(le32_to_cpu(cf->data[i++] << 8),
  991. (&priv->regs->if2_dataa1) + j*4);
  992. }
  993. can_put_echo_skb(skb, ndev, tx_buffer_avail - PCH_RX_OBJ_NUM - 1);
  994. /* Updating the size of the data. */
  995. pch_can_bit_clear(&priv->regs->if2_mcont, 0x0f);
  996. pch_can_bit_set(&priv->regs->if2_mcont, cf->can_dlc);
  997. /* Clearing IntPend, NewDat & TxRqst */
  998. pch_can_bit_clear(&priv->regs->if2_mcont,
  999. CAN_IF_MCONT_NEWDAT | CAN_IF_MCONT_INTPND |
  1000. CAN_IF_MCONT_TXRQXT);
  1001. /* Setting NewDat, TxRqst bits */
  1002. pch_can_bit_set(&priv->regs->if2_mcont,
  1003. CAN_IF_MCONT_NEWDAT | CAN_IF_MCONT_TXRQXT);
  1004. pch_can_check_if_busy(&priv->regs->if2_creq, tx_buffer_avail);
  1005. spin_unlock_irqrestore(&priv->msgif_reg_lock, flags);
  1006. return NETDEV_TX_OK;
  1007. }
  1008. static const struct net_device_ops pch_can_netdev_ops = {
  1009. .ndo_open = pch_can_open,
  1010. .ndo_stop = pch_close,
  1011. .ndo_start_xmit = pch_xmit,
  1012. };
  1013. static void __devexit pch_can_remove(struct pci_dev *pdev)
  1014. {
  1015. struct net_device *ndev = pci_get_drvdata(pdev);
  1016. struct pch_can_priv *priv = netdev_priv(ndev);
  1017. unregister_candev(priv->ndev);
  1018. free_candev(priv->ndev);
  1019. pci_iounmap(pdev, priv->regs);
  1020. pci_release_regions(pdev);
  1021. pci_disable_device(pdev);
  1022. pci_set_drvdata(pdev, NULL);
  1023. pch_can_reset(priv);
  1024. }
  1025. #ifdef CONFIG_PM
  1026. static int pch_can_suspend(struct pci_dev *pdev, pm_message_t state)
  1027. {
  1028. int i; /* Counter variable. */
  1029. int retval; /* Return value. */
  1030. u32 buf_stat; /* Variable for reading the transmit buffer status. */
  1031. u32 counter = 0xFFFFFF;
  1032. struct net_device *dev = pci_get_drvdata(pdev);
  1033. struct pch_can_priv *priv = netdev_priv(dev);
  1034. /* Stop the CAN controller */
  1035. pch_can_set_run_mode(priv, PCH_CAN_STOP);
  1036. /* Indicate that we are aboutto/in suspend */
  1037. priv->can.state = CAN_STATE_SLEEPING;
  1038. /* Waiting for all transmission to complete. */
  1039. while (counter) {
  1040. buf_stat = pch_can_get_buffer_status(priv);
  1041. if (!buf_stat)
  1042. break;
  1043. counter--;
  1044. udelay(1);
  1045. }
  1046. if (!counter)
  1047. dev_err(&pdev->dev, "%s -> Transmission time out.\n", __func__);
  1048. /* Save interrupt configuration and then disable them */
  1049. pch_can_get_int_enables(priv, &(priv->int_enables));
  1050. pch_can_set_int_enables(priv, PCH_CAN_DISABLE);
  1051. /* Save Tx buffer enable state */
  1052. for (i = 0; i < PCH_OBJ_NUM; i++) {
  1053. if (priv->msg_obj[i] == MSG_OBJ_TX)
  1054. pch_can_get_tx_enable(priv, i + 1,
  1055. &(priv->tx_enable[i]));
  1056. }
  1057. /* Disable all Transmit buffers */
  1058. pch_can_tx_disable_all(priv);
  1059. /* Save Rx buffer enable state */
  1060. for (i = 0; i < PCH_OBJ_NUM; i++) {
  1061. if (priv->msg_obj[i] == MSG_OBJ_RX) {
  1062. pch_can_get_rx_enable(priv, i + 1,
  1063. &(priv->rx_enable[i]));
  1064. pch_can_get_rx_buffer_link(priv, i + 1,
  1065. &(priv->rx_link[i]));
  1066. }
  1067. }
  1068. /* Disable all Receive buffers */
  1069. pch_can_rx_disable_all(priv);
  1070. retval = pci_save_state(pdev);
  1071. if (retval) {
  1072. dev_err(&pdev->dev, "pci_save_state failed.\n");
  1073. } else {
  1074. pci_enable_wake(pdev, PCI_D3hot, 0);
  1075. pci_disable_device(pdev);
  1076. pci_set_power_state(pdev, pci_choose_state(pdev, state));
  1077. }
  1078. return retval;
  1079. }
  1080. static int pch_can_resume(struct pci_dev *pdev)
  1081. {
  1082. int i; /* Counter variable. */
  1083. int retval; /* Return variable. */
  1084. struct net_device *dev = pci_get_drvdata(pdev);
  1085. struct pch_can_priv *priv = netdev_priv(dev);
  1086. pci_set_power_state(pdev, PCI_D0);
  1087. pci_restore_state(pdev);
  1088. retval = pci_enable_device(pdev);
  1089. if (retval) {
  1090. dev_err(&pdev->dev, "pci_enable_device failed.\n");
  1091. return retval;
  1092. }
  1093. pci_enable_wake(pdev, PCI_D3hot, 0);
  1094. priv->can.state = CAN_STATE_ERROR_ACTIVE;
  1095. /* Disabling all interrupts. */
  1096. pch_can_set_int_enables(priv, PCH_CAN_DISABLE);
  1097. /* Setting the CAN device in Stop Mode. */
  1098. pch_can_set_run_mode(priv, PCH_CAN_STOP);
  1099. /* Configuring the transmit and receive buffers. */
  1100. pch_can_config_rx_tx_buffers(priv);
  1101. /* Restore the CAN state */
  1102. pch_set_bittiming(dev);
  1103. /* Listen/Active */
  1104. pch_can_set_optmode(priv);
  1105. /* Enabling the transmit buffer. */
  1106. for (i = 0; i < PCH_OBJ_NUM; i++) {
  1107. if (priv->msg_obj[i] == MSG_OBJ_TX) {
  1108. pch_can_set_tx_enable(priv, i + 1,
  1109. priv->tx_enable[i]);
  1110. }
  1111. }
  1112. /* Configuring the receive buffer and enabling them. */
  1113. for (i = 0; i < PCH_OBJ_NUM; i++) {
  1114. if (priv->msg_obj[i] == MSG_OBJ_RX) {
  1115. /* Restore buffer link */
  1116. pch_can_set_rx_buffer_link(priv, i + 1,
  1117. priv->rx_link[i]);
  1118. /* Restore buffer enables */
  1119. pch_can_set_rx_enable(priv, i + 1, priv->rx_enable[i]);
  1120. }
  1121. }
  1122. /* Enable CAN Interrupts */
  1123. pch_can_set_int_custom(priv);
  1124. /* Restore Run Mode */
  1125. pch_can_set_run_mode(priv, PCH_CAN_RUN);
  1126. return retval;
  1127. }
  1128. #else
  1129. #define pch_can_suspend NULL
  1130. #define pch_can_resume NULL
  1131. #endif
  1132. static int pch_can_get_berr_counter(const struct net_device *dev,
  1133. struct can_berr_counter *bec)
  1134. {
  1135. struct pch_can_priv *priv = netdev_priv(dev);
  1136. bec->txerr = ioread32(&priv->regs->errc) & CAN_TEC;
  1137. bec->rxerr = (ioread32(&priv->regs->errc) & CAN_REC) >> 8;
  1138. return 0;
  1139. }
  1140. static int __devinit pch_can_probe(struct pci_dev *pdev,
  1141. const struct pci_device_id *id)
  1142. {
  1143. struct net_device *ndev;
  1144. struct pch_can_priv *priv;
  1145. int rc;
  1146. int index;
  1147. void __iomem *addr;
  1148. rc = pci_enable_device(pdev);
  1149. if (rc) {
  1150. dev_err(&pdev->dev, "Failed pci_enable_device %d\n", rc);
  1151. goto probe_exit_endev;
  1152. }
  1153. rc = pci_request_regions(pdev, KBUILD_MODNAME);
  1154. if (rc) {
  1155. dev_err(&pdev->dev, "Failed pci_request_regions %d\n", rc);
  1156. goto probe_exit_pcireq;
  1157. }
  1158. addr = pci_iomap(pdev, 1, 0);
  1159. if (!addr) {
  1160. rc = -EIO;
  1161. dev_err(&pdev->dev, "Failed pci_iomap\n");
  1162. goto probe_exit_ipmap;
  1163. }
  1164. ndev = alloc_candev(sizeof(struct pch_can_priv), PCH_TX_OBJ_NUM);
  1165. if (!ndev) {
  1166. rc = -ENOMEM;
  1167. dev_err(&pdev->dev, "Failed alloc_candev\n");
  1168. goto probe_exit_alloc_candev;
  1169. }
  1170. priv = netdev_priv(ndev);
  1171. priv->ndev = ndev;
  1172. priv->regs = addr;
  1173. priv->dev = pdev;
  1174. priv->can.bittiming_const = &pch_can_bittiming_const;
  1175. priv->can.do_set_mode = pch_can_do_set_mode;
  1176. priv->can.do_get_berr_counter = pch_can_get_berr_counter;
  1177. priv->can.ctrlmode_supported = CAN_CTRLMODE_LISTENONLY |
  1178. CAN_CTRLMODE_LOOPBACK;
  1179. priv->tx_obj = PCH_RX_OBJ_NUM + 1; /* Point head of Tx Obj */
  1180. ndev->irq = pdev->irq;
  1181. ndev->flags |= IFF_ECHO;
  1182. pci_set_drvdata(pdev, ndev);
  1183. SET_NETDEV_DEV(ndev, &pdev->dev);
  1184. ndev->netdev_ops = &pch_can_netdev_ops;
  1185. priv->can.clock.freq = PCH_CAN_CLK; /* Hz */
  1186. for (index = 0; index < PCH_RX_OBJ_NUM;)
  1187. priv->msg_obj[index++] = MSG_OBJ_RX;
  1188. for (index = index; index < PCH_OBJ_NUM;)
  1189. priv->msg_obj[index++] = MSG_OBJ_TX;
  1190. netif_napi_add(ndev, &priv->napi, pch_can_rx_poll, PCH_RX_OBJ_NUM);
  1191. rc = register_candev(ndev);
  1192. if (rc) {
  1193. dev_err(&pdev->dev, "Failed register_candev %d\n", rc);
  1194. goto probe_exit_reg_candev;
  1195. }
  1196. return 0;
  1197. probe_exit_reg_candev:
  1198. free_candev(ndev);
  1199. probe_exit_alloc_candev:
  1200. pci_iounmap(pdev, addr);
  1201. probe_exit_ipmap:
  1202. pci_release_regions(pdev);
  1203. probe_exit_pcireq:
  1204. pci_disable_device(pdev);
  1205. probe_exit_endev:
  1206. return rc;
  1207. }
  1208. static struct pci_driver pch_can_pci_driver = {
  1209. .name = "pch_can",
  1210. .id_table = pch_pci_tbl,
  1211. .probe = pch_can_probe,
  1212. .remove = __devexit_p(pch_can_remove),
  1213. .suspend = pch_can_suspend,
  1214. .resume = pch_can_resume,
  1215. };
  1216. static int __init pch_can_pci_init(void)
  1217. {
  1218. return pci_register_driver(&pch_can_pci_driver);
  1219. }
  1220. module_init(pch_can_pci_init);
  1221. static void __exit pch_can_pci_exit(void)
  1222. {
  1223. pci_unregister_driver(&pch_can_pci_driver);
  1224. }
  1225. module_exit(pch_can_pci_exit);
  1226. MODULE_DESCRIPTION("Controller Area Network Driver");
  1227. MODULE_LICENSE("GPL v2");
  1228. MODULE_VERSION("0.94");